Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding proxy support, fixes #5 #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 48 additions & 13 deletions dynect/DynectDNS.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -77,13 +78,56 @@ 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.
"""
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,
Expand All @@ -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):
"""
Expand Down
2 changes: 1 addition & 1 deletion dynect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
version = '0.1'
version_info = (0, 1)

from DynectDNS import DynectRest
from .DynectDNS import DynectRest
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from distutils.core import setup

# sync with __init__.py
version = '0.3'
version = '0.5'

setup(
name="DynectDNS",
Expand All @@ -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',
],
)