User guide#
Installation#
Install the package from GitHub repository git+https://github.com/pbabinca/http_client_pretty_headers.git using your preferred Python package manager:
python3 -m pip install git+https://github.com/pbabinca/http_client_pretty_headers.git
uv pip install git+https://github.com/pbabinca/http_client_pretty_headers.git
Quick start#
Import the
http.clientand get a connection to the server:
import http.client
conn = http.client.HTTPSConnection("httpbin.org")
Call
http.client.HTTPConnection.set_debuglevel()to get any output with HTTP headers:
conn.set_debuglevel(1)
Activate pretty headers with
activate_httpclient_pretty():
from http_client_pretty_headers import activate_httpclient_pretty
activate_httpclient_pretty()
Use the connection as usual:
conn.request("HEAD", "/")
resp = conn.getresponse()
Deactivate pretty headers if necessary:
from http_client_pretty_headers import deactivate_httpclient_pretty
deactivate_httpclient_pretty()
This approach is easiest to use in large code base but it might be problematic if there are many places with HTTP calls that you are not interested in. Use Context manager to limit the scope of pretty headers.
Context manager interface of pretty headers#
For smaller section of code use context manager. Follow steps 1-2 from quick start:
import http.client
conn = http.client.HTTPSConnection("httpbin.org")
conn.set_debuglevel(1)
Import the pretty headers class and wrap any code with the context manager to get pretty headers:
from http_client_pretty_headers import HttpClientPrettyHeaders
with HttpClientPrettyHeaders():
conn.request("HEAD", "/")
resp = conn.getresponse()
Note that only http.client.HTTPConnection.request() and http.client.HTTPConnection.getresponse() methods need to be wrapped in the context manager.
Advanced manual replacement of print function#
You could also explicitly replace print function of http.client module on your own. Follow steps 1-2 from quick start:
import http.client
conn = http.client.HTTPSConnection("httpbin.org")
conn.set_debuglevel(1)
Import the pretty headers class and replace the print function:
from http_client_pretty_headers import HttpClientPrettyHeaders
http.client.print = HttpClientPrettyHeaders().httpclient_print_func_closure()
From now on use http.client as usual. For example:
conn.request("HEAD", "/")
resp = conn.getresponse()
Output to logging objects#
If your code base uses logging objects, you could also use logging interface of pretty headers.
Follow steps 1-2 from quick start:
import http.client
conn = http.client.HTTPSConnection("httpbin.org")
conn.set_debuglevel(1)
If you haven’t created a logger create one. By default it needs to have DEBUG log level. For example:
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
Pass the logger to
LoggingOutputclass.
from http_client_pretty_headers import LoggingOutput
logger_output = LoggingOutput(logger)
Pass the logger_output to either
activate_httpclient_pretty()orHttpClientPrettyHeaderscontext manager.from http_client_pretty_headers import activate_httpclient_pretty activate_httpclient_pretty(logger_output) conn.request("HEAD", "/") resp = conn.getresponse()
or
from http_client_pretty_headers import HttpClientPrettyHeaders with HttpClientPrettyHeaders(logger_output): conn.request("HEAD", "/") resp = conn.getresponse()