我有这个代码
httpd = HTTPServer(('127.0.0.1', 8000),SimpleHTTPRequestHandler)
httpd.handle_request()
httpd.handle_request() 服务一个请求,然后按预期终止服务器。我想将此请求捕获为变量,以便稍后解析它。就像是
Request_Variable = httpd.handle_request()
*上面的代码不起作用。但我正在寻找类似的东西谢谢
我有这个代码
httpd = HTTPServer(('127.0.0.1', 8000),SimpleHTTPRequestHandler)
httpd.handle_request()
httpd.handle_request() 服务一个请求,然后按预期终止服务器。我想将此请求捕获为变量,以便稍后解析它。就像是
Request_Variable = httpd.handle_request()
*上面的代码不起作用。但我正在寻找类似的东西谢谢
您可以扩展BaseHTTPRequestHandler
并实现您自己的do_GET
(resp. do_POST
) 方法,该方法在服务器接收到 GET (resp. POST) 请求时调用。
查看文档以了解BaseHTTPRequestHandler
您可以使用对象的哪些实例变量。变量path
、headers
和可能是您感兴趣的rfile
。wfile
from http.server import BaseHTTPRequestHandler, HTTPServer
class MyRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
print(self.path)
def do_POST(self):
content_length = int(self.headers.get('Content-Length'))
print(self.rfile.read(content_length))
httpd = HTTPServer(('127.0.0.1', 8000), MyRequestHandler)
httpd.handle_request()
# make your GET/POST request