File: http_client_intercept.py

package info (click to toggle)
python-wsgi-intercept 1.13.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 560 kB
  • sloc: python: 1,390; makefile: 56; sh: 5
file content (37 lines) | stat: -rw-r--r-- 992 bytes parent folder | download
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