我正在尝试用Python构建一个真正无阻塞的HTTPS服务器.如果每个人都玩得很好,下面的最小代码就可以了:
import BaseHTTPServer import SimpleHTTPServer import SocketServer import ssl class ThreadedHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer): pass httpd = ThreadedHTTPServer(('localhost', 4443), SimpleHTTPServer.SimpleHTTPRequestHandler) httpd.socket = ssl.wrap_socket(httpd.socket, keyfile="localhost.key", certfile="localhost.pem", server_side=True) httpd.serve_forever()
但是,问题是该服务器至少在TLS握手期间阻塞.
测试:
$ nc localhost 4443 # leave this open
然后(在另一个终端):
$ wget --no-check-certificate https://localhost:4443/ --2014-10-23 16:55:54-- https://localhost:4443/ Resolving localhost (localhost)... 127.0.0.1 Connecting to localhost (localhost)|127.0.0.1|:4443... connected.
wget进程阻塞,表示服务器中的某些内容被阻止.一旦我关闭了nc进程,wget就会继续.这显然不实用.
如何在Python中获得真正无阻塞的HTTPS服务器,最好不需要额外的第三方软件?
我应该提一下,在没有TLS的情况下,相同的代码可以正常工作(即没有wrap_socket行).
Steffen Ullrich指出如何做到这一点:传递do_handshake_on_connect=False
给wrap_socket
自己,然后自己做握手.在这种情况下,子类BaseHTTPServer.HTTPServer
,覆盖handle
,然后执行握手,如Python文档(self.request
调用套接字)中所示,然后调用super方法.