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

source_address not supported in python 2.6 httplib #195

Open
nkeuning opened this issue Jun 14, 2015 · 4 comments
Open

source_address not supported in python 2.6 httplib #195

nkeuning opened this issue Jun 14, 2015 · 4 comments

Comments

@nkeuning
Copy link

File "/usr/lib/python2.6/site-packages/libtaxii/clients.py", line 341, in call_taxii_service2
response = urllib2.urlopen(req)
File "/usr/lib64/python2.6/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib64/python2.6/urllib2.py", line 391, in open
response = self._open(req, data)
File "/usr/lib64/python2.6/urllib2.py", line 409, in _open
'_open', req)
File "/usr/lib64/python2.6/urllib2.py", line 369, in _call_chain
result = func(*args)
File "/usr/lib/python2.6/site-packages/libtaxii/clients.py", line 367, in https_open
return self.do_open(self.get_connection, req)
File "/usr/lib64/python2.6/urllib2.py", line 1136, in do_open
h = http_class(host, timeout=req.timeout) # will parse host:port
File "/usr/lib/python2.6/site-packages/libtaxii/clients.py", line 374, in get_connection
ca_certs=self.ca_certs)
File "/usr/lib/python2.6/site-packages/libtaxii/clients.py", line 404, in init
source_address)
TypeError: init() takes at most 7 arguments (8 given)

@nkeuning
Copy link
Author

Potential fix for clients.py
class VerifiableHTTPSConnection(httplib.HTTPSConnection):

"""
The default httplib HTTPSConnection does not verify certificates.
This class extends HTTPSConnection and requires certificate verification.
Borrowed from http://thejosephturner.com/blog/2011/03/19/https-certificate-verification-in-python-with-urllib2/
"""

def __init__(self, host, port=None, key_file=None, cert_file=None,
             strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
             source_address=None, verify_server=False, ca_certs=None):

    self.python_version = '%s.%s' % (version_info[0],version_info[1])

    if self.python_version == '2.6':
        httplib.HTTPSConnection.__init__(self, host, port, key_file,
                                     cert_file, strict, timeout)
    else:
        httplib.HTTPSConnection.__init__(self, host, port, key_file,
                                     cert_file, strict, timeout,
                                     source_address)

    if verify_server:
        self.cert_reqs = ssl.CERT_REQUIRED
    else:
        self.cert_reqs = ssl.CERT_NONE
    self.ca_certs = ca_certs

def connect(self):
    # overrides the version in httplib so that we do
    # certificate verification
    if self.python_version == '2.6':
        sock = socket.create_connection((self.host, self.port),
                                    self.timeout)
    else:
        sock = socket.create_connection((self.host, self.port),
                                    self.timeout,
                                    self.source_address)
    if self._tunnel_host:
        self.sock = sock
        self._tunnel()

    self.sock = ssl.wrap_socket(sock,
                                keyfile=self.key_file,
                                certfile=self.cert_file,
                                cert_reqs=self.cert_reqs,
                                ca_certs=self.ca_certs)

@nkeuning
Copy link
Author

For reference httplib document stating source_address added in python 2.7
https://docs.python.org/2/library/httplib.html

@rjprins
Copy link
Contributor

rjprins commented Jul 28, 2015

I encountered the same issue in the context of making libtaxii compatible with Python 3.
See the pull request: #197

@MarkDavidson
Copy link
Contributor

@rjprins, @nkeuning,

Was this issue solved in #197? If so, can this be closed?

-Mark

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants