From a9daada50e421f7260a64442fbae1bd8fd9a9d54 Mon Sep 17 00:00:00 2001 From: Maxwel Leite Date: Mon, 14 Apr 2014 21:40:09 -0300 Subject: [PATCH] Support to work behind a proxy Simple modification to make work behind a http proxy (without authentication). The trick is get environment variable http_proxy to make work. --- cloudprint/rest.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/cloudprint/rest.py b/cloudprint/rest.py index af907b7..676aaf0 100644 --- a/cloudprint/rest.py +++ b/cloudprint/rest.py @@ -5,6 +5,8 @@ import UserDict import UserList import UserString +import os +from urlparse import urlparse class REST: class RESTException(Exception): @@ -41,10 +43,22 @@ def __repr__(self): def __init__(self, host, auth=None, debug=False): proto, host = host.split('://') - if proto == 'https': - self._conn = httplib.HTTPSConnection(host) + + http_proxy = os.environ.get('http_proxy') + if http_proxy is not None: + proxy= urlparse(http_proxy) + if proto == 'https': + c = httplib.HTTPSConnection(proxy.hostname, proxy.port) + else: + c = httplib.HTTPConnection(proxy.hostname, proxy.port) + c.set_tunnel(host) + self._conn = c else: - self._conn = httplib.HTTPConnection(host) + if proto == 'https': + self._conn = httplib.HTTPSConnection(host) + else: + self._conn = httplib.HTTPConnection(host) + self.debug = debug if debug: self._conn.set_debuglevel(10)