[英]I'm using http.server with python3 and I want to store a request as a variable
我有這個代碼
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.