环境:
- Python 2.7.1(针对 MSVCRT100 构建)
- Windows 7
代码是:
import ssl
import socket
socket = socket.socket()
socket = ssl.wrap_socket(socket, keyfile='key', certfile='cert', server_side=True)
虽然这在 mac os x 上工作正常,但在 windows 上失败:
socket.error: [Errno 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
有趣的是,如果使用标准的 python,这会起作用,它使用 OpenSSL 0.9.8l - 据我所知,我已尽可能使用默认值编译 python 和 OpenSSL 0.9.8r。
我应该如何让这段代码工作 - 或者,我应该如何调查罪魁祸首?
最佳答案
下面的错误表明,“套接字未连接”
socket.error: [Errno 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
ssl.wrap_socket方法使用惰性上下文构造:
For server-side sockets, if the socket has no remote peer, it is assumed to be a listening socket, and the server-side SSL wrapping is automatically performed on client connections accepted via the accept() method.
不知何故,这对于带有 MSVCRT100 的 Python 2.7 来说是错误的, 只需在 accept()
之后调用 ssl.wrap_socket()关于python - SocketServer.TCPSocket 适用于 stock python,但不适用于使用更新的 OpenSSL 针对 MSVCRT100 编译的 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6605406/