Skip to content

Commit 0d0d5c5

Browse files
Thomas Monjalonstephenfin
authored andcommitted
pwclient: Support proxy configuration
The environment variables http_proxy and https_proxy can be used to configure the HTTP transport. The TCP connection is made with the proxy host, whereas the original host is maintained in the HTTP POST URI via "handler" in "send_request". The send_request() method of xmlrpclib has a different signature and behaviour in Python 2 and 3. Fixes #47 Signed-off-by: Thomas Monjalon <thomas.monjalon@6wind.com> Reviewed-by: Stephen Finucane <stephen@that.guru> (cherry picked from commit 53fe8b1)
1 parent 3141f74 commit 0d0d5c5

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

patchwork/bin/pwclient

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,43 @@ class Transport(xmlrpclib.SafeTransport):
107107
def __init__(self, url):
108108
xmlrpclib.SafeTransport.__init__(self)
109109
self.credentials = None
110+
self.host = None
111+
self.proxy = None
112+
self.scheme = url.split('://', 1)[0]
110113
self.https = url.startswith('https')
114+
if self.https:
115+
self.proxy = os.environ.get('https_proxy')
116+
else:
117+
self.proxy = os.environ.get('http_proxy')
118+
if self.proxy:
119+
self.https = self.proxy.startswith('https')
111120

112121
def set_credentials(self, username=None, password=None):
113122
self.credentials = '%s:%s' % (username, password)
114123

115124
def make_connection(self, host):
125+
self.host = host
126+
if self.proxy:
127+
host = self.proxy.split('://', 1)[-1]
116128
if self.credentials:
117129
host = '@'.join([self.credentials, host])
118130
if self.https:
119131
return xmlrpclib.SafeTransport.make_connection(self, host)
120132
else:
121133
return xmlrpclib.Transport.make_connection(self, host)
122134

135+
if sys.version_info[0] == 2:
136+
137+
def send_request(self, connection, handler, request_body):
138+
handler = '%s://%s%s' % (self.scheme, self.host, handler)
139+
xmlrpclib.Transport.send_request(self, connection, handler, request_body)
140+
141+
else: # Python 3
142+
143+
def send_request(self, host, handler, request_body, debug):
144+
handler = '%s://%s%s' % (self.scheme, host, handler)
145+
return xmlrpclib.Transport.send_request(self, host, handler, request_body, debug)
146+
123147

124148
def project_id_by_name(rpc, linkname):
125149
"""Given a project short name, look up the Project ID."""

0 commit comments

Comments
 (0)