I am looking for a library or function call in python or an associated library that would let me feed in a raw stream of text data representing an HTTP req/res and that would spit out that information is some sort of meaningful form like a dictionary or list. I do not want to use some built in class or create a bunch of new objects, in my program I am receiving in some raw data and that is just what I've got to work with. Is there already a solution out there for this, or do I have to write an HTTP parser myself?
Edit: Let me clarify what exactly I'm looking to do. I'm looking for something that would take a string like:
GET /index.html HTTP/1.1 \r\n
Host:www.stackoverflow.com \r\n
User-Agent:Firefox \r\n
etc.
And send me back something encapsulating the method, HTTP version, headers and all the rest.
There is a pure python HTTP parser that is shipped as a fallback implementation for the C/Cython optimized implementation of the http-parser project.
Here is the pure python version:
Here the source of the C version and Cython wrapper:
http://docs.python.org/library/httplib.html I believe this is the library you are looking for. A little change in name for python 3 but otherwise good to go.
I'd start by looking at WebOb. I think the cgi module in the standard library also has an HTTP parser.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With