diff options
author | Folker Bernitt <fbernitt@thoughtworks.com> | 2015-04-07 11:22:38 +0200 |
---|---|---|
committer | Folker Bernitt <fbernitt@thoughtworks.com> | 2015-04-07 11:22:38 +0200 |
commit | 9ab762583a28f4afe6a4d005b1043b5a5405c9a3 (patch) | |
tree | 58e3a7a2a316d68eb29096dd66da8330d3cdc042 /service/pixelated/support | |
parent | 98ec21fedd2b27c6f28be993006380446f6b4abe (diff) |
Only patch debian requests if it's installed.
Diffstat (limited to 'service/pixelated/support')
-rw-r--r-- | service/pixelated/support/ext_requests_urllib3.py | 105 |
1 files changed, 54 insertions, 51 deletions
diff --git a/service/pixelated/support/ext_requests_urllib3.py b/service/pixelated/support/ext_requests_urllib3.py index 9de75788..c4ec2438 100644 --- a/service/pixelated/support/ext_requests_urllib3.py +++ b/service/pixelated/support/ext_requests_urllib3.py @@ -18,62 +18,65 @@ import requests if requests.__version__ == '2.0.0': - import requests.packages.urllib3.connectionpool - from socket import error as SocketError, timeout as SocketTimeout - from requests.packages.urllib3.packages.ssl_match_hostname import CertificateError, match_hostname - import socket - import ssl + try: + import requests.packages.urllib3.connectionpool + from socket import error as SocketError, timeout as SocketTimeout + from requests.packages.urllib3.packages.ssl_match_hostname import CertificateError, match_hostname + import socket + import ssl - from requests.packages.urllib3.exceptions import ( - ClosedPoolError, - ConnectTimeoutError, - EmptyPoolError, - HostChangedError, - MaxRetryError, - SSLError, - ReadTimeoutError, - ProxyError, - ) + from requests.packages.urllib3.exceptions import ( + ClosedPoolError, + ConnectTimeoutError, + EmptyPoolError, + HostChangedError, + MaxRetryError, + SSLError, + ReadTimeoutError, + ProxyError, + ) - from requests.packages.urllib3.util import ( - assert_fingerprint, - get_host, - is_connection_dropped, - resolve_cert_reqs, - resolve_ssl_version, - ssl_wrap_socket, - Timeout, - ) + from requests.packages.urllib3.util import ( + assert_fingerprint, + get_host, + is_connection_dropped, + resolve_cert_reqs, + resolve_ssl_version, + ssl_wrap_socket, + Timeout, + ) - def patched_connect(self): - # Add certificate verification - try: - sock = socket.create_connection(address=(self.host, self.port), timeout=self.timeout) - except SocketTimeout: - raise ConnectTimeoutError(self, "Connection to %s timed out. (connect timeout=%s)" % (self.host, self.timeout)) + def patched_connect(self): + # Add certificate verification + try: + sock = socket.create_connection(address=(self.host, self.port), timeout=self.timeout) + except SocketTimeout: + raise ConnectTimeoutError(self, "Connection to %s timed out. (connect timeout=%s)" % (self.host, self.timeout)) - resolved_cert_reqs = resolve_cert_reqs(self.cert_reqs) - resolved_ssl_version = resolve_ssl_version(self.ssl_version) + resolved_cert_reqs = resolve_cert_reqs(self.cert_reqs) + resolved_ssl_version = resolve_ssl_version(self.ssl_version) - if self._tunnel_host: - self.sock = sock - # Calls self._set_hostport(), so self.host is - # self._tunnel_host below. - self._tunnel() + if self._tunnel_host: + self.sock = sock + # Calls self._set_hostport(), so self.host is + # self._tunnel_host below. + self._tunnel() - # Wrap socket using verification with the root certs in - # trusted_root_certs - self.sock = ssl_wrap_socket(sock, self.key_file, self.cert_file, - cert_reqs=resolved_cert_reqs, - ca_certs=self.ca_certs, - server_hostname=self.host, - ssl_version=resolved_ssl_version) + # Wrap socket using verification with the root certs in + # trusted_root_certs + self.sock = ssl_wrap_socket(sock, self.key_file, self.cert_file, + cert_reqs=resolved_cert_reqs, + ca_certs=self.ca_certs, + server_hostname=self.host, + ssl_version=resolved_ssl_version) - if self.assert_fingerprint: - assert_fingerprint(self.sock.getpeercert(binary_form=True), - self.assert_fingerprint) - elif resolved_cert_reqs != ssl.CERT_NONE and self.assert_hostname is not False: - match_hostname(self.sock.getpeercert(), - self.assert_hostname or self.host) + if self.assert_fingerprint: + assert_fingerprint(self.sock.getpeercert(binary_form=True), + self.assert_fingerprint) + elif resolved_cert_reqs != ssl.CERT_NONE and self.assert_hostname is not False: + match_hostname(self.sock.getpeercert(), + self.assert_hostname or self.host) - requests.packages.urllib3.connectionpool.VerifiedHTTPSConnection.connect = patched_connect + requests.packages.urllib3.connectionpool.VerifiedHTTPSConnection.connect = patched_connect + except ImportError: + pass # The patch is specific for the debian package. Ignore it if it can't be found |