[Python-ideas] Providing a public API for creating and parsing HTTP messages

Derek Maciel derekamaciel at gmail.com
Tue Apr 17 02:21:08 EDT 2018

Hello all,

If this is not the appropriate place for this type of proposal please let
me know.

The modules http.client and http.server both do a wonderful job when
implementing HTTP clients and servers, respectively. However, occasionally
there may be a need to create and parse HTTP messages themselves without
needing to implement a client or server. While http.client and http.server
clearly have solved this problem for its own uses, there is little to no
public API exposed to a user wishing to do the same. For instance, the
documentation for http.client.HTTPMessage [1] simply states that it extends
from email.message.Message in order to parse the messages.

[1]: https://docs.python.org/3/library/http.client.html#httpmessage-objects

While this need may admittedly be a niche one, it is a shame that one
cannot simply use the functionality already implemented for http.client and
http.server for this purpose. Therefore, I am proposing this module be
improved such that the creation and parsing of HTTP messages be "pulled out
of" the existing code into HTTPRequest and HTTPResponse classes with a
public API for this purpose. The exact API is subject to change of course,
but it could work like:

>>> r = http.client.HTTPRequest("POST", "/foo", "Hello world!")
>>> r.method
>>> r.body
'Hello world!'
>>> print(r)
POST /foo HTTP/1.1
Content-Length: 12

Hello world!

If this idea is not completely a bad one, I am very interested in
discussing in further detail how I (or perhaps someone else) could begin to
implement these changes.

Thank you for reading.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180417/c0c3a2f5/attachment.html>

More information about the Python-ideas mailing list