diff --git a/dynect/DynectDNS.py b/dynect/DynectDNS.py index eee3261..68b00bf 100644 --- a/dynect/DynectDNS.py +++ b/dynect/DynectDNS.py @@ -17,6 +17,7 @@ sys.exit("Could not find json or simplejson libraries.") if sys.version_info[0] == 2: + from httplib import HTTPConnection, HTTPSConnection from urllib import pathname2url @@ -48,7 +49,7 @@ class DynectRest(object): """ def __init__( - self, host='api.dynect.net', port=443, ssl=True, api_version="current" + self, host='api.dynect.net', port=443, ssl=True, api_version="current", proxy_host=None, proxy_port=None ): """ Basic initializer method @@ -77,6 +78,17 @@ def __init__( self._valid_methods = set(('DELETE', 'GET', 'POST', 'PUT')) + # Add support for proxy connections + self.proxy_host = proxy_host + self.proxy_port = proxy_port + self.use_proxy = False + self.proxied_root = None + + if proxy_host is not None and proxy_port is not None: + self.use_proxy = True + scheme = 'https' if self.ssl else 'http' + self.proxied_root = "{}://{}".format(scheme, self.host) + def _debug(self, msg): """ Debug output. @@ -84,6 +96,38 @@ def _debug(self, msg): if self.verbose: sys.stderr.write(msg) + def _connect(self): + if self.ssl: + msg = "Establishing SSL connection to %s:%s\n" % ( + self.host, self.port + ) + self._debug(msg) + self._conn = HTTPSConnection(self.host, self.port) + + else: + msg = "Establishing unencrypted connection to %s:%s\n" % ( + self.host, self.port + ) + self._debug(msg) + self._conn = HTTPConnection(self.host, self.port) + + def _proxy_connect(self): + if self.ssl: + msg = "Establishing SSL connection to %s:%s\n" % ( + self.host, self.port + ) + self._debug(msg) + self._conn = HTTPSConnection(self.proxy_host, self.proxy_port) + + else: + msg = "Establishing SSL connection to %s:%s\n" % ( + self.proxy_host, self.proxy_port + ) + self._debug(msg) + self._conn = HTTPConnection(self.proxy_host, self.proxy_port) + + self._conn.set_tunnel(self.host, self.port) + def connect(self): """ Establishes a connection to the REST API server as defined by the host, @@ -102,19 +146,10 @@ def connect(self): self._conn = None - if self.ssl: - msg = "Establishing SSL connection to %s:%s\n" % ( - self.host, self.port - ) - self._debug(msg) - self._conn = HTTPSConnection(self.host, self.port) - + if self.use_proxy: + self._proxy_connect() else: - msg = "Establishing unencrypted connection to %s:%s\n" % ( - self.host, self.port - ) - self._debug(msg) - self._conn = HTTPConnection(self.host, self.port) + self._connect() def execute(self, uri, method, args = None): """ diff --git a/dynect/__init__.py b/dynect/__init__.py index e27a057..477ae8b 100644 --- a/dynect/__init__.py +++ b/dynect/__init__.py @@ -2,4 +2,4 @@ version = '0.1' version_info = (0, 1) -from DynectDNS import DynectRest +from .DynectDNS import DynectRest diff --git a/setup.py b/setup.py index 908f83e..a0c3736 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from distutils.core import setup # sync with __init__.py -version = '0.3' +version = '0.5' setup( name="DynectDNS", @@ -18,6 +18,6 @@ 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 3', 'Topic :: Internet :: Name Service (DNS)', - 'Topic :: Software Development :: Libraries', + 'Topic :: Software Development :: Libraries', ], )