我有一个在Linux上运行的Python HTTPS服务器,该服务器为另一项服务提供了远程状态信息。
该网页包含状态指示器,这些状态指示器通过AJAX GET请求每秒更新一次,并且只有三个或四个用户,因此吞吐量不高。如果浏览器连接到服务器数小时,则服务器最终将挂起并停止接受GET请求。 hange后运行netstat会显示CLOSE_WAIT状态的4-6个https连接和ESTABLISHED状态的一个。即使关闭浏览器,ESTABLISHED连接也不会消失。
服务器正在使用Python 2.6.6运行,因为这是在Linux机器上安装的,但是我尝试使用Python 2.7.15运行,结果相同。操作系统为RHEL 6,内核2.6.32 64位。
相关代码如下:
class AduWebServer(ThreadingMixIn, HTTPServer):
pass
class AduRequestHandler(BaseHTTPRequestHandler):
protocol_version = 'HTTP/1.1'
def do_GET(self):
try:
if (len(self.path) > 15 and self.path[:15] == '/adu_get_status'):
self.handleAduGetStatusRequest()
elif (self.path == '/'):
self.handleAduPage()
elif (self.path == '/logo.png'):
self.handleFile('image/png')
elif (self.path == '/favicon.ico'):
self.handleFile('image/x-icon')
elif (self.path == '/refresh.js'):
self.handleFile('text/javascript')
else:
self.send_error(404)
logger.error('GET attempt on invalid page: ' + self.path)
except Exception as msg:
self.send_error(500)
logger.error('GET exception: ' + str(msg))
def handleAduGetStatusRequest(self):
response = self.getAduStatus()
self.send_response(200)
self.send_header('Content-length', len(response))
self.end_headers()
self.wfile.write(response)
def handleAduPage(self):
aduPage = AduPage()
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.send_header('Content-length', len(aduPage.response))
self.end_headers()
self.wfile.write(aduPage.response)
def handleFile(self, contentType):
filePath = os.getcwd() + '/' + self.path
f = open(filePath, 'rb')
response = f.read()
self.send_response(200)
self.send_header('Content-type', contentType)
self.send_header('Content-length', len(response))
self.end_headers()
self.wfile.write(response)
handler = AduRequestHandler
httpd = AduWebServer(('', 443), handler)
httpd.socket = ssl.wrap_socket(httpd.socket,
keyfile='keyfile',
certfile='crtfile',
server_side=True)
httpd.serve_forever()
任何人都知道是什么导致服务器因CLOSE_WAIT挂起?