python - SocketServer.TCPSocket 适用于 stock python,但不适用于使用更新的 OpenSSL 针对 MSVCRT100 编译的 python

标签 python windows sockets

环境:

  • 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/

相关文章:

c++ - hstrerror() 的替换函数

c++ - Socket编程C++报错undefined reference

python - 如何从 http.client.HTTPResponse 对象中获取 URL?

Python 检查是否为质数

C 并行线程与并行进程

linux - 如何确保或保持套接字存活?

python - 列出父类的属性对象

python:设置不同的配置文件进行单元测试

windows - 找出环境变量是否包含子字符串

windows - Windows "netstat -b"如何确定拥有每个套接字的进程的名称?