简体   繁体   English

如何在Python 3中解码HTTP请求标头和正文?

[英]how to decode HTTP request headers and body in Python 3?

i am writing an experimental asynchronous web server. 我正在编写一个实验性的异步Web服务器。 i am wondering about the standard / 'best' way to decode HTTP requests in python? 我想知道在python中解码HTTP请求的标准/“最佳”方法吗?

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)? 是否有一个很好的通用教程,如何做到这一点以及要注意的事项(尤其是有关编码和浏览器的细节)?

This worked for me: 这对我有用:

import StringIO, httplib

ucode_data = unicode( your_raw_data ,"utf-8")
str = StringIO.StringIO( ucode_data )
http_header = httplib.HTTPMessage(str,0)
http_header.readheaders()

print http_header.__dict__

but it does not decode the request (eg, GET /index.html HTTP/1.2) - it will decode the rest for you though 但它不会解码请求(例如GET /index.html HTTP / 1.2)-尽管它将为您解码其余内容

See 看到

20.10.4. 20.10.4。 HTTPMessage Objects HTTPMessage对象

An http.client.HTTPMessage instance holds the headers from an HTTP response. http.client.HTTPMessage实例保存HTTP响应的标头。 It is implemented using the email.message.Message class. 它是使用email.message.Message类实现的。

http://docs.python.org/py3k/library/http.client.html#httpmessage-objects 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). 您应该能够将HTTPMessage用作独立类,而无需调用urllib(或任何等效的Python 3)。

Don't deal with sockets; 不要处理套接字; abstract! 抽象! Try httplib2 . 试试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. 它是用于Python 2和3的完整HTTP库,并且非常直观,尽管您必须下载并安装它。 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; Dive Into Python 3涵盖了关于安装和使用httplib2的非常好的一章 ,以及为什么它比其他替代方法(包括标准库)更好。 I recommend you read that. 我建议您阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM