python websocket服务器https_Python中真正无阻塞的HTTPS服务器

I'm trying to build a truly non-blocking HTTPS server in Python. The following minimal code works just fine if everyone is playing nice:

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()

However, the problem is that this server blocks at least during the TLS handshake.

Test with:

$ nc localhost 4443 # leave this open

And then (in another terminal):

$ 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.

The wget process blocks, indicating that something is blocked in the server. Once I close the nc process, wget continues. This is obviously not practical at all.

How do I get a truly non-blocking HTTPS server in Python, preferably without additional third-party software?

I should mention that the very same code works as expected without TLS (i.e., without the wrap_socket line).

Steffen Ullrich pointed out how to do it: pass do_handshake_on_connect=False to wrap_socket, then do the handshake yourself. In this case, subclass BaseHTTPServer.HTTPServer, override handle, and then do the handshake as shown in the Python docs (the socket is called self.request) followed by calling the super method.

解决方案

You have to do a non-blocking SSL accept by calling ssl.wrap_socket with do_handshake_on_connect=False and later calling do_handshake yourself until it succeeds. See https://docs.python.org/3/library/ssl.html#notes-on-non-blocking-sockets.

You might also simply use Tornado which is a web server written in python and which also does fully non-blocking SSL handling. Even if you don't want to use it yourself you might have a look at the source code to see how this is done (search for do_handshake).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值