我有一个恼人的问题。我有这个简单的服务器代码(比方说):
#!/usr/bin/env python3
import wsgiref.simple_server
def my_func(env, start_response):
start_response('200 OK', [])
return [''.encode()]
server = wsgiref.simple_server.make_server(
'0.0.0.0',
19891,
my_func,
)
server.serve_forever()
然而,5次尝试中的1次(因此约20%的请求)非常慢。当我在这个巨大的延迟到位时中断服务器处理时,我总是得到以下异常:
Exception happened during processing of request from ('192.168.1.100', 3540)
Traceback (most recent call last):
File "/usr/lib/python3.5/socketserver.py", line 313, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python3.5/socketserver.py", line 341, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python3.5/socketserver.py", line 354, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python3.5/socketserver.py", line 681, in __init__
self.handle()
File "/usr/lib/python3.5/wsgiref/simple_server.py", line 119, in handle
self.raw_requestline = self.rfile.readline(65537)
File "/usr/lib/python3.5/socket.py", line 575, in readinto
return self._sock.recv_into(b)
KeyboardInterrupt
你知道如何避免这种恼人的事情吗?或者这种行为背后的原因是什么?
UPDATE1: 我尝试过TCP_NODELAY修改simple_server.py - > WSGIServer-> server_bind函数如下:
def server_bind(self):
"""Override server_bind to store the server name."""
import socket
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY,1)
HTTPServer.server_bind(self)
self.setup_environ()
不幸的是没有变化:(
答案 0 :(得分:0)
您使用的是端口号3540,这是“TCP / UDP端口查找器”的默认端口。您可以在操作系统上与另一个服务/守护程序发生端口冲突。
您可以更改为端口80,8000,8080 ......如果您需要Web服务器。