Python 是否有用于解析 HTTP 请求和响应的模块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2115410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:50:25  来源:igfitidea点击:

Does Python have a module for parsing HTTP requests and responses?

pythonhttpparsing

提问by Jeff Hammerbacher

httplib (now http.client) and friends all have conn.getresponse() and an HTTPResponse class, but the server-side operations of conn.getrequest() and an HTTPRequest class seem to be lacking.

httplib(现在是http.client)和朋友们都有conn.getresponse() 和一个HTTPResponse 类,但是conn.getrequest() 和一个HTTPRequest 类的服务器端操作似乎缺乏。

I understand that BaseHTTPServer and BaseHTTPRequestHandler can perform this functionality, but they don't expose these methods for use outside of the module.

我知道 BaseHTTPServer 和 BaseHTTPRequestHandler 可以执行此功能,但它们不会公开这些方法以供在模块外部使用。

Essentially what I want is BaseHTTPRequestHandler#parse_request to be a static method that returns an HTTPRequest object rather than populating member variables.

基本上我想要的是 BaseHTTPRequestHandler#parse_request 是一个静态方法,它返回一个 HTTPRequest 对象而不是填充成员变量。

回答by Brandon Rhodes

Jeff, to enable parsing I create a small nine-line subclass of the base HTTP request handler:

Jeff,为了启用解析,我创建了基本 HTTP 请求处理程序的一个九行小子类:

from BaseHTTPServer import BaseHTTPRequestHandler
from StringIO import StringIO

class HTTPRequest(BaseHTTPRequestHandler):
    def __init__(self, request_text):
        self.rfile = StringIO(request_text)
        self.raw_requestline = self.rfile.readline()
        self.error_code = self.error_message = None
        self.parse_request()

    def send_error(self, code, message):
        self.error_code = code
        self.error_message = message

You can now take a string with the text of an HTTP request inside and parse it by instantiating this class:

您现在可以获取一个包含 HTTP 请求文本的字符串,并通过实例化此类来解析它:

# Simply instantiate this class with the request text

request = HTTPRequest(request_text)

print request.error_code       # None  (check this first)
print request.command          # "GET"
print request.path             # "/who/ken/trust.html"
print request.request_version  # "HTTP/1.1"
print len(request.headers)     # 3
print request.headers.keys()   # ['accept-charset', 'host', 'accept']
print request.headers['host']  # "cm.bell-labs.com"

# Parsing can result in an error code and message

request = HTTPRequest('GET\r\nHeader: Value\r\n\r\n')

print request.error_code     # 400
print request.error_message  # "Bad request syntax ('GET')"

回答by S.Lott

For server-side processing you want to look at something like wsgiref.

对于服务器端处理,您需要查看类似wsgiref 的内容

The WSGI standard parses the request into a simple dictionary with all of the relevant headers and elements.

WSGI 标准将请求解析为包含所有相关标头和元素的简单字典。

回答by Tom Willis

You would probably find WebObuseful. Frameworks like Pylons, Turbogears, and Bfg use it as part of their api. It does operate under the assumption that you are working under WSGI though.

您可能会发现WebOb很有用。Pylons、Turbogears 和 Bfg 等框架将其用作其 api 的一部分。它确实在假设您在 WSGI 下工作的情况下运行。