python解析http request

2023-01-31 05:01:39 python request 解析

如果通过抓包工具抓到一个raw Http request,想要解析它,可以使用如下方法。

编写一个继承自python内置基类BaseHTTPRequestHandler的类,代码如下:

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

然后通过这个类的实例就可以访问解析后的数据了

# Using this new class is really easy!

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"

要想知道HTTPRequest有哪些属性,可以查看Python文档对“BaseHTTPServer.BaseHTTPRequestHandler”的说明。

欢迎扫描下方二维码关注我的微信公众号致新(zhixin991),那里有我的读书笔记,计算机科普和杂谈


相关文章