Python - BaseHTTPServer 保持活动状态不起作用

Nit*_*olu 2 python basehttpserver

我有一个基于 BaseHTTPServer 的简单服务器的以下代码。

class myHandler(BaseHTTPRequestHandler):
    #Handler for the GET requests
    def do_GET(self):
        # Parse the query_str
        query_str = self.path.strip().lower()
        if query_str.startswith("/download?"):
            query_str = query_str[10:]
            opts = urlparse.parse_qs(query_str)

            # Send the html message and download file
            self.protocol_version = 'HTTP/1.1'
            self.send_response(200)
            self.send_header("Content-type", 'text/html')
            self.send_header("Content-length", 1)
            self.end_headers()
            self.wfile.write("0")

            # Some code to do some processing
            # ...
            # -----------

            self.wfile.write("1")
Run Code Online (Sandbox Code Playgroud)

我原本期望 HTML 页面显示“1”,但它显示“0”。如何通过 keepalived 更新响应?

Mar*_*hio 11

我相信您将self.protocol_version设置为“HTTP/1.1”为时已晚。您正在 do_GET() 方法中执行此操作,此时您的请求处理程序已经实例化,并且服务器已经检查了该实例的 protocol_version 属性。

最好将其设置在班级上:

class myHandler(BaseHTTPRequestHandler):
    protocol_version = 'HTTP/1.1'
Run Code Online (Sandbox Code Playgroud)