Python Socket SSL OSError: [WinError 10057] A request to send or receive data was not allowed becaus
Posted
技术标签:
【中文标题】Python Socket SSL OSError: [WinError 10057] A request to send or receive data was not allowed because the socket is not connected and (wh【英文标题】:Python Socket SSL OSError: [WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (wh 【发布时间】:2021-10-12 02:45:10 【问题描述】:完整的错误代码:
Traceback (most recent call last):
File "C:\path\to\my\python\code\server_ssl_testing.py", line 15, in <module>
response = ssock.recv(1024).decode("utf-8")
File "C:\Program Files\Python39\lib\ssl.py", line 1228, in recv
return super().recv(buflen, flags)
OSError: [WinError 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 Socket 服务器和客户端,但随后服务器尝试从客户端接收信息失败并打印此文本上方的错误。
服务器:
import socket
import ssl
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain('certs/server-cert.pem', 'certs/server.key')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as sock:
sock.bind(('localhost', 2642))
sock.listen(5)
with context.wrap_socket(sock, server_side=True) as ssock:
conn, addr = ssock.accept()
response = ssock.recv(1024).decode("utf-8")
print(response)
客户:
import socket
import ssl
hostname = 'localhost'
context = ssl.create_default_context()
sock = socket.create_connection((hostname, 2642))
ssock = context.wrap_socket(sock, server_hostname=hostname)
msg = "Hello World!"
ssock.send(msg.encode("utf-8"))
【问题讨论】:
【参考方案1】:在 server.py 中我应该写 conn.recv
而不是 ssock.recv
。
如果将来有人想这样做应该会有所帮助=)
【讨论】:
以上是关于Python Socket SSL OSError: [WinError 10057] A request to send or receive data was not allowed becaus的主要内容,如果未能解决你的问题,请参考以下文章
Python随心记--python之错误:OSError: [WinError 10048]
[python] 解决OSError: Address already in use
Python中socket经ssl加密后server开多线程