File: urllib_intercept.py

package info (click to toggle)
python-wsgi-intercept 0.8.1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 288 kB
  • ctags: 201
  • sloc: python: 710; makefile: 180
file content (40 lines) | stat: -rw-r--r-- 1,068 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
38
39
40
"""Intercept HTTP connections that use urllib.request (Py3) aka urllib2 (Python 2).
"""
try:
    import urllib.request as url_lib
except ImportError:
    import urllib2 as url_lib

from . import WSGI_HTTPConnection, WSGI_HTTPSConnection


class WSGI_HTTPHandler(url_lib.HTTPHandler):
    """
    Override the default HTTPHandler class with one that uses the
    WSGI_HTTPConnection class to open HTTP URLs.
    """
    def http_open(self, req):
        return self.do_open(WSGI_HTTPConnection, req)


class WSGI_HTTPSHandler(url_lib.HTTPSHandler):
    """
    Override the default HTTPSHandler class with one that uses the
    WSGI_HTTPConnection class to open HTTPS URLs.
    """
    def https_open(self, req):
        return self.do_open(WSGI_HTTPSConnection, req)


def install_opener():
    handlers = [WSGI_HTTPHandler()]
    if WSGI_HTTPSHandler is not None:
        handlers.append(WSGI_HTTPSHandler())
    opener = url_lib.build_opener(*handlers)
    url_lib.install_opener(opener)

    return opener


def uninstall_opener():
    url_lib.install_opener(None)