Python HTTP服务器保持连接活动

时间:2018-05-01 16:23:07

标签: python-3.x http http-headers httpserver

我正在尝试测试用C编写的HTTP客户端,它将HTTP POST请求发送到我计算机上的本地服务器。我在我的POST请求中添加了标题keep-alive,在我的计算机上运行的python3 HTTP服务器上看起来像这样:

<ip-address-1> - - [29/Apr/2018 18:27:49] "POST /html HTTP/1.1" 200 -
Host: <ip-address-2>
Content-Type: application/json
Content-Length: 168
Connection: Keep-Alive
Keep-Alive: timeout=5, max=100


INFO:root:POST request,
Body:
{
"field": "abc",
"time": "2018-04-29T01:27:50.322000Z" 
}

HTTP服务器POST处理程序如下所示:

class S(BaseHTTPRequestHandler):
    def _set_response(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.send_header("Connection", "keep-alive")
        self.send_header("keep-alive", "timeout=5, max=30")
        self.end_headers()

    def do_POST(self):
        content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
        post_data = self.rfile.read(content_length) # <--- Gets the data itself
        print(self.headers)
        logging.info("POST request,\nBody:\n%s\n", post_data.decode('utf-8'))

        self._set_response()
        self.wfile.write("POST request for {}".format(self.path).encode('utf-8'))

def run(server_class=HTTPServer, handler_class=S, port=8080):
    logging.basicConfig(level=logging.INFO)
    server_address = ('', port)
    httpd = server_class(server_address, handler_class)
    logging.info('Starting httpd...\n')
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass
    httpd.server_close()
    logging.info('Stopping httpd...\n')

我在客户端看到的标题响应是:

HTTP/1.0 200 OK
Server: BaseHTTP/0.6 Python/3.5.2
Date: Tue, 29 April 2018 16:07:42 GMT
Content-type: text/html
Connection: keep-alive
keep-alive: timeout=5, max=30

我仍然最终得到断开连接回调,所以我的问题是如何从服务器端设置keep-alive连接参数?

1 个答案:

答案 0 :(得分:2)

默认情况下,BaseHTTPRequestHandler会发出HTTP / 1.0响应,您可以在HTTP/1.0 200 OK中看到。保持活动响应需要HTTP/1.1,如the doc(或for v3)所示:

  

<强> PROTOCOL_VERSION

     

指定响应中使用的HTTP协议版本。如果设置为“HTTP / 1.1”,则服务器将允许HTTP持久连接;   但是,您的服务器必须包含准确的Content-Length   header(使用send_header())在其对客户端的所有响应中。对于   向后兼容性,设置默认为“HTTP / 1.0”。

然后,正如您在报价中所看到的,您还必须为您的回复设置正确的内容长度。

请注意,目前您发送没有正文的回复,您应该使用204(无内容)代码并添加Content-length: 0标题,或添加一个小正文(在Content-Length中使用正确的字节数,警告,这不是一个字符计数器,这是一个字节计数器,在ascii7中几乎相同但不与其他编码相同。)