views:

96

answers:

2

i am writing an experimental asynchronous web server. i am wondering about the standard / 'best' way to decode HTTP requests in python?

basically what reading from the socket gives me is a bytes representation of the incoming request raw data; how can i turn these into standard datatypes like dictionaries, lists of values, and so on? is there a good general tutorial how to do this and what to be on the watchout for (especially regarding encodings and browser specifics)?

A: 

See

20.10.4. HTTPMessage Objects

An http.client.HTTPMessage instance holds the headers from an HTTP response. It is implemented using the email.message.Message class.

http://docs.python.org/py3k/library/http.client.html#httpmessage-objects

You should be able to use the HTTPMessage as a standalone class without invoking urllib (or whatever Python 3 equivalent).

Wai Yip Tung
+1  A: 

Don't deal with sockets; abstract! Try httplib2. It's a complete HTTP library for Python 2 and 3, and it is very intuitive, although you have to download and install it. Read its usage example for a quick introduction.

Dive Into Python 3 includes a very good chapter on installing and using httplib2, and why it's better than other alternatives, including the standard library; I recommend you read that.

Beau Martínez