Dedupe the urllib3 monkey patching code

requests vendorizes urllib3 which makes monkey patching it while also
wanting to monkey patch proper urllib3 a bit hairy. Here we use a
factory to do the overrides for us. Hat tip to @FND for helping
with the thinking through of this.
This commit is contained in:
Chris Dent 2016-03-18 13:43:34 +00:00
parent 8e02d4e1fe
commit c464f2ec7a
3 changed files with 55 additions and 74 deletions

View File

@ -0,0 +1,45 @@
"""Common code of urllib3 and requests intercepts."""
import os
import sys
from . import WSGI_HTTPConnection, WSGI_HTTPSConnection, wsgi_fake_socket
wsgi_fake_socket.settimeout = lambda self, timeout: None
def make_urllib3_override(HTTPConnectionPool, HTTPSConnectionPool,
HTTPConnection, HTTPSConnection):
class HTTP_WSGIInterceptor(WSGI_HTTPConnection, HTTPConnection):
def __init__(self, *args, **kwargs):
if 'strict' in kwargs and sys.version_info > (3, 0):
kwargs.pop('strict')
WSGI_HTTPConnection.__init__(self, *args, **kwargs)
HTTPConnection.__init__(self, *args, **kwargs)
class HTTPS_WSGIInterceptor(WSGI_HTTPSConnection, HTTPSConnection):
is_verified = True
def __init__(self, *args, **kwargs):
if 'strict' in kwargs and sys.version_info > (3, 0):
kwargs.pop('strict')
WSGI_HTTPSConnection.__init__(self, *args, **kwargs)
HTTPSConnection.__init__(self, *args, **kwargs)
def install():
if 'http_proxy' in os.environ or 'https_proxy' in os.environ:
raise RuntimeError(
'http_proxy or https_proxy set in environment, please unset')
HTTPConnectionPool.ConnectionCls = HTTP_WSGIInterceptor
HTTPSConnectionPool.ConnectionCls = HTTPS_WSGIInterceptor
def uninstall():
HTTPConnectionPool.ConnectionCls = HTTPConnection
HTTPSConnectionPool.ConnectionCls = HTTPSConnection
return install, uninstall

View File

@ -2,45 +2,14 @@
`requests <http://docs.python-requests.org/en/latest/>`_.
"""
import os
import sys
from . import WSGI_HTTPConnection, WSGI_HTTPSConnection, wsgi_fake_socket
from requests.packages.urllib3.connectionpool import (HTTPConnectionPool,
HTTPSConnectionPool)
from requests.packages.urllib3.connection import (HTTPConnection,
HTTPSConnection)
from ._urllib3 import make_urllib3_override
wsgi_fake_socket.settimeout = lambda self, timeout: None
class HTTP_WSGIInterceptor(WSGI_HTTPConnection, HTTPConnection):
def __init__(self, *args, **kwargs):
if 'strict' in kwargs and sys.version_info > (3, 0):
kwargs.pop('strict')
WSGI_HTTPConnection.__init__(self, *args, **kwargs)
HTTPConnection.__init__(self, *args, **kwargs)
class HTTPS_WSGIInterceptor(WSGI_HTTPSConnection, HTTPSConnection):
is_verified = True
def __init__(self, *args, **kwargs):
if 'strict' in kwargs and sys.version_info > (3, 0):
kwargs.pop('strict')
WSGI_HTTPSConnection.__init__(self, *args, **kwargs)
HTTPSConnection.__init__(self, *args, **kwargs)
def install():
if 'http_proxy' in os.environ or 'https_proxy' in os.environ:
raise RuntimeError(
'http_proxy or https_proxy set in environment, please unset')
HTTPConnectionPool.ConnectionCls = HTTP_WSGIInterceptor
HTTPSConnectionPool.ConnectionCls = HTTPS_WSGIInterceptor
def uninstall():
HTTPConnectionPool.ConnectionCls = HTTPConnection
HTTPSConnectionPool.ConnectionCls = HTTPSConnection
install, uninstall = make_urllib3_override(HTTPConnectionPool,
HTTPSConnectionPool,
HTTPConnection,
HTTPSConnection)

View File

@ -2,45 +2,12 @@
`urllib3 <https://urllib3.readthedocs.org/>`_.
"""
import os
import sys
# TODO: This is a a total dupe of requests. Need to remove
# duplication.
from . import WSGI_HTTPConnection, WSGI_HTTPSConnection, wsgi_fake_socket
from urllib3.connectionpool import HTTPConnectionPool, HTTPSConnectionPool
from urllib3.connection import HTTPConnection, HTTPSConnection
from ._urllib3 import make_urllib3_override
wsgi_fake_socket.settimeout = lambda self, timeout: None
class HTTP_WSGIInterceptor(WSGI_HTTPConnection, HTTPConnection):
def __init__(self, *args, **kwargs):
if 'strict' in kwargs and sys.version_info > (3, 0):
kwargs.pop('strict')
WSGI_HTTPConnection.__init__(self, *args, **kwargs)
HTTPConnection.__init__(self, *args, **kwargs)
class HTTPS_WSGIInterceptor(WSGI_HTTPSConnection, HTTPSConnection):
is_verified = True
def __init__(self, *args, **kwargs):
if 'strict' in kwargs and sys.version_info > (3, 0):
kwargs.pop('strict')
WSGI_HTTPSConnection.__init__(self, *args, **kwargs)
HTTPSConnection.__init__(self, *args, **kwargs)
def install():
if 'http_proxy' in os.environ or 'https_proxy' in os.environ:
raise RuntimeError(
'http_proxy or https_proxy set in environment, please unset')
HTTPConnectionPool.ConnectionCls = HTTP_WSGIInterceptor
HTTPSConnectionPool.ConnectionCls = HTTPS_WSGIInterceptor
def uninstall():
HTTPConnectionPool.ConnectionCls = HTTPConnection
HTTPSConnectionPool.ConnectionCls = HTTPSConnection
install, uninstall = make_urllib3_override(HTTPConnectionPool,
HTTPSConnectionPool,
HTTPConnection,
HTTPSConnection)