1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
"""Intercept HTTP connections that use httplib (Py2) or http.client (Py3).
"""
import http.client as http_lib
from . import WSGI_HTTPConnection, WSGI_HTTPSConnection
from http.client import (
HTTPConnection as OriginalHTTPConnection,
HTTPSConnection as OriginalHTTPSConnection
)
class HTTP_WSGIInterceptor(WSGI_HTTPConnection, http_lib.HTTPConnection):
pass
class HTTPS_WSGIInterceptor(WSGI_HTTPSConnection, http_lib.HTTPSConnection,
HTTP_WSGIInterceptor):
def __init__(self, host, **kwargs):
self.host = host
try:
self.port = kwargs['port']
except KeyError:
self.port = None
HTTP_WSGIInterceptor.__init__(self, host, **kwargs)
def install():
http_lib.HTTPConnection = HTTP_WSGIInterceptor
http_lib.HTTPSConnection = HTTPS_WSGIInterceptor
def uninstall():
http_lib.HTTPConnection = OriginalHTTPConnection
http_lib.HTTPSConnection = OriginalHTTPSConnection
|