Display headers of HTTP protocol client in a readable way
Project description
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.
Why
There are couple of implementations that alters Requests headers output. I haven't found a solution that would make this possible for http.client as well. On the top of that majority of that code is available without proper license. Finally I haven't found any package published on PyPI that could be used directly as a dependency. List of these implementations in no particular order:
- Debugging HTTPS clients written with Python requests by Romain JACQUET on his blog.
- StackOverflow response to question How to make httplib debugger infomation into logger debug level by Mallikarjunarao Kosuri
User guide
See User guide for more details.
Contributing
See CONTRIBUTING.md for more details.
Changelog
See CHANGELOG.md for more details.
License
Code of Pretty headers for http.client is distributed under the terms of the MIT license. Documentation is distributed under the terms of the Creative Commons Attribution 4.0 International License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file http_client_pretty_headers-0.1.0.tar.gz
.
File metadata
- Download URL: http_client_pretty_headers-0.1.0.tar.gz
- Upload date:
- Size: 22.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 60c72414913a8df177651033fcdd6c56e278e449eec04adfd4c928062bb0a7be |
|
MD5 | 9486f81fcf0b6d5122b43eeded172000 |
|
BLAKE2b-256 | 8eca8d802115e69ed72acb229b3eb550859c506179159ecf8258f788d5fa296f |
File details
Details for the file http_client_pretty_headers-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: http_client_pretty_headers-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6324e79e1b1639faebd3e3291a0fdb2df116e43e695328fb22ce576a4d050613 |
|
MD5 | b64fd92c5333ef2a48fe20d514872cef |
|
BLAKE2b-256 | 5740b2ae17933ffb4b312fe68005078c35848b0f3a49e18f5d9e18f1f4d698d9 |