diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2014-11-24 11:52:31 +0000 |
---|---|---|
committer | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2014-11-24 11:52:31 +0000 |
commit | f4a75c61c2d2cf7f7adc81051bcd70ff78145780 (patch) | |
tree | dcd0df8302c5424c9fff2cfaa5ae691355083001 /lorrycontroller | |
parent | 689174532cc4920d2ef96bcebeb8a1adaf985804 (diff) | |
download | lorry-controller-f4a75c61c2d2cf7f7adc81051bcd70ff78145780.tar.gz |
Support HTTP(S) proxies which don't require username and password
Previously the proxy.conf file had to contain 'username' and 'password'
fields, which meant that it didn't work for some HTTP proxy setups.
Diffstat (limited to 'lorrycontroller')
-rw-r--r-- | lorrycontroller/proxy.py | 35 |
1 files changed, 25 insertions, 10 deletions
diff --git a/lorrycontroller/proxy.py b/lorrycontroller/proxy.py index 44749c9..53d0667 100644 --- a/lorrycontroller/proxy.py +++ b/lorrycontroller/proxy.py @@ -20,6 +20,21 @@ import urllib import urllib2 +def build_proxy_url(protocol, proxy_config): + """Build a proxy URL from data in our proxy configuration format.""" + + hostname = urllib.quote(proxy_config['hostname']) + url = '%s:%s' % (hostname, proxy_config['port']) + + if 'username' not in proxy_config: + return '%s://%s/' % (protocol, url) + elif 'password' not in proxy_config: + return '%s://%s@%s/' % (protocol, proxy_config['username'], url) + else: + return '%s://%s:%s@%s/' % (protocol, proxy_config['username'], + proxy_config['password'], url) + + def setup_proxy(config_filename): """Tell urllib2 to use a proxy for http action by lorry-controller. @@ -32,18 +47,18 @@ def setup_proxy(config_filename): return with open(config_filename, 'r') as f: - proxy = json.load(f) - + proxy_config = json.load(f) + + http_proxy_url = build_proxy_url('http', proxy_config) + https_proxy_url = build_proxy_url('https', proxy_config) + # set the required environment variables - hostname = urllib.quote(proxy['hostname']) - user = '%s:%s' % (proxy['username'], proxy['password']) - url = '%s:%s' % (hostname, proxy['port']) - os.environ['http_proxy'] = 'http://%s@%s' % (user, url) - os.environ['https_proxy'] = 'https://%s@%s' % (user, url) - + os.environ['http_proxy'] = http_proxy_url + os.environ['https_proxy'] = https_proxy_url + # create a ProxyHandler - proxies = {'http_proxy': 'http://%s@%s' % (user, url), - 'https_proxy': 'https://%s@%s' % (user, url)} + proxies = {'http_proxy': http_proxy_url, + 'https_proxy': https_proxy_url} proxy_handler = urllib2.ProxyHandler(proxies) # install an opener to use the proxy |