Pretty headers for http.client#
Pretty headers for http.client is a library extends standard library http.client with a print function which prints HTTP request headers on new lines and consistently prefixes response HTTP headers. Example of usage and its output:
>>> from http_client_pretty_headers import activate_httpclient_pretty
>>> activate_httpclient_pretty()
>>> from http.client import HTTPSConnection
>>> conn = HTTPSConnection("httpbin.org")
>>> conn.set_debuglevel(1)
>>> conn.request("HEAD", "/")
send: HEAD / HTTP/1.1
send: Host: httpbin.org
send: Accept-Encoding: identity
>>> resp = conn.getresponse()
reply: HTTP/1.1 200 OK
reply: Date: Tue, 08 Oct 2024 10:00:00 GMT
reply: Content-Type: text/html; charset=utf-8
reply: Content-Length: 9593
reply: Connection: keep-alive
reply: Server: gunicorn/19.9.0
reply: Access-Control-Allow-Origin: *
reply: Access-Control-Allow-Credentials: true
>>>
Original output from http.client
>>> from http.client import HTTPSConnection
>>> conn = HTTPSConnection("httpbin.org")
>>> conn.set_debuglevel(1)
>>> conn.request("HEAD", "/")
send: b'HEAD / HTTP/1.1\r\nHost: httpbin.org\r\nAccept-Encoding: identity\r\n\r\n'
>>> resp = conn.getresponse()
reply: 'HTTP/1.1 200 OK\r\n'
header: Date: Tue, 08 Oct 2024 10:00:00 GMT
header: Content-Type: text/html; charset=utf-8
header: Content-Length: 9593
header: Connection: keep-alive
header: Server: gunicorn/19.9.0
header: Access-Control-Allow-Origin: *
header: Access-Control-Allow-Credentials: true
>>>
This library could be also used with urllib3 because it uses http.client as its backend. While http.client prints headers with print function, urllib3 uses logging module. See this example how to use logging library with Pretty headers for http.client:
>>> import logging
>>> logging.basicConfig()
>>>
>>> import http.client
>>> http.client.HTTPConnection.debuglevel = 1
>>>
>>> import urllib3
>>> logging.getLogger("urllib3").setLevel(logging.DEBUG)
>>>
>>> from http_client_pretty_headers import activate_httpclient_pretty, LoggingOutput
>>> logger = logging.getLogger()
>>> logger.setLevel(logging.DEBUG)
>>> output = LoggingOutput(logger_obj=logger)
>>> activate_httpclient_pretty(output)
>>>
>>> resp = urllib3.request("HEAD", "https://httpbin.org/")
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): httpbin.org:443
DEBUG:root:send: HEAD / HTTP/1.1
DEBUG:root:send: Host: httpbin.org
DEBUG:root:send: Accept-Encoding: identity
DEBUG:root:send: User-Agent: python-urllib3/2.2.3
DEBUG:root:reply: HTTP/1.1 200 OK
DEBUG:root:reply: Date: Wed, 09 Oct 2024 10:03:00 GMT
DEBUG:root:reply: Content-Type: text/html; charset=utf-8
DEBUG:root:reply: Content-Length: 9593
DEBUG:root:reply: Connection: keep-alive
DEBUG:root:reply: Server: gunicorn/19.9.0
DEBUG:root:reply: Access-Control-Allow-Origin: *
DEBUG:root:reply: Access-Control-Allow-Credentials: true
DEBUG:urllib3.connectionpool:https://httpbin.org:443 "HEAD / HTTP/11" 200 0
>>>
And, finally, the library could be also used with Requests because it uses urllib3, which in turn uses http.client. Requests, on its own, does not emit any logs nor use print. It is up to you which of the approaches above you use.
If you decide to use logging with requests, you should use getLogger("urllib3") (as above) instead of getLogger("requests.packages.urllib3") which is still commonly found on the internet. The use of later has been discouraged since around 2015, and no longer works since requests 2.16.0 released on 2017-05-26 when urlib3 has been devendored from the library.
User guide#
For installation, guick guide and usage see User Guide.
API Reference#
For details on the API see API Reference.
Developer’s guide#
For details about the code structure see Developer’s guide.
Contributing#
If you would like to contribute, please see Contributing guide for more details.
Changelog#
List of changes in the project are available in Changelog.