Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse Raw HTTP in Python

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.

like image 783
themaestro Avatar asked Jul 09 '10 15:07

themaestro


3 Answers

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:

  • https://github.com/benoitc/http-parser/blob/master/http_parser/pyparser.py

Here the source of the C version and Cython wrapper:

  • https://github.com/benoitc/http-parser/blob/master/http_parser/http_parser.c
  • https://github.com/benoitc/http-parser/blob/master/http_parser/parser.pyx
like image 103
ogrisel Avatar answered Nov 10 '22 11:11

ogrisel


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.

like image 39
Gabriel Avatar answered Nov 10 '22 09:11

Gabriel


I'd start by looking at WebOb. I think the cgi module in the standard library also has an HTTP parser.

like image 1
Marius Gedminas Avatar answered Nov 10 '22 11:11

Marius Gedminas