File: http_client.rst

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 (45 lines) | stat: -rw-r--r-- 1,239 bytes parent folder | download | duplicates (7)
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
41
42
43
44
45
http_client_intercept
=====================

.. automodule:: wsgi_intercept.http_client_intercept

.. warning::

   This intercept will fail to install if you access access HTTPConnection or
   HTTPSConnection before the intercept is installed. For example, do not use
   "from http.client import HTTPConnection". Instead, "import http.client" and
   reference http.client.HTTPConnection after the intercept is installed.

Example:

.. testcode:: 

    try:
        import http.client as http_lib
    except ImportError:
        import httplib as http_lib
    from wsgi_intercept import (
        http_client_intercept, add_wsgi_intercept, remove_wsgi_intercept
    )


    def app(environ, start_response):
        start_response('200 OK', [('Content-Type', 'text/plain')])
        return [b'Whee']


    def make_app():
        return app


    host, port = 'localhost', 80
    http_client_intercept.install()
    add_wsgi_intercept(host, port, make_app)
    HTTPConnection = http_lib.HTTPConnection
    client = HTTPConnection(host)
    client.request('GET', '/')
    response = client.getresponse()
    content = response.read()
    assert content == b'Whee'
    remove_wsgi_intercept(host, port)
    http_client_intercept.uninstall()