diff options
-rwxr-xr-x | lorry-controller-webapp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/lorry-controller-webapp b/lorry-controller-webapp index 43ec37a..6161e37 100755 --- a/lorry-controller-webapp +++ b/lorry-controller-webapp @@ -135,6 +135,8 @@ class WEBAPP(cliapp.Application): def process_args(self, args): self.settings.require('statedb') + self.setup_proxy() + templates = self.load_templates() webapp = bottle.Bottle() @@ -205,5 +207,38 @@ class WEBAPP(cliapp.Application): quiet=True, debug=True) + def setup_proxy(self): + """Tell urllib2 to use a proxy for http action by lorry-controller. + + Load the proxy information from the JSON file given by proxy_def, then + set urllib2's url opener to open urls via an authenticated proxy. + + """ + + config_filename = os.path.join( + self.settings['configuration-directory'], 'proxy.conf') + + if not os.path.exists(config_filename): + return + + with open(config_filename, 'r') as f: + proxy = json.load(f) + + # 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) + + # create a ProxyHandler + proxies = {'http_proxy': 'http://%s@%s' % (user, url), + 'https_proxy': 'https://%s@%s' % (user, url)} + proxy_handler = urllib2.ProxyHandler(proxies) + + # install an opener to use the proxy + opener = urllib2.build_opener(proxy_handler) + urllib2.install_opener(opener) + WEBAPP().run() |