[英]Error [SSL: WRONG_VERSION_NUMBER] when trying to re-wrap a socket
I'm building a TCP Server-Client app to run locally for a school project.我正在为学校项目构建一个 TCP 服务器客户端应用程序以在本地运行。 In order to send login credentials from the client to the server, we're supposed to encrypt the application data messages using SSL/TLS, and go back to using uncrypted data for other message exchanges.为了将登录凭据从客户端发送到服务器,我们应该使用 SSL/TLS 加密应用程序数据消息,并且 go 回使用未加密的数据进行其他消息交换。 I was able to do those two things using the ssl library for Python, however, after I've unwrapped a socket and attempt to wrapt it again, i get the following error:我能够使用 Python 的ssl库来完成这两件事,但是,在我打开一个套接字并尝试再次包装它之后,我收到以下错误:
File "D:\Documentos\USP\MAC0352 - Redes\EP2\ServidorHibrido-JogoDaVelha\src\cliente.py", line 18, in <module>
sockt = ssl.wrap_socket(sockt)
File "C:\Users\vitor\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1405, in wrap_socket
return context.wrap_socket(
File "C:\Users\vitor\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "C:\Users\vitor\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1040, in _create
self.do_handshake()
File "C:\Users\vitor\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1122)
I think its very unlikely that the error message is correct (or at least clear), given that the SSL handshake and message exchange has worked perfectly the first time.鉴于 SSL 握手和消息交换第一次完美运行,我认为错误消息不太可能是正确的(或至少是清楚的)。
Here is the login function in the server script:这是服务器脚本中的登录 function:
def login(self):
self.socket = ssl.wrap_socket(self.socket, server_side=True, keyfile="cert/MyKey.key", certfile="cert/MyCertificate.crt")
credentials = self.socket.recv(1024)
print("Received credentials: ", credentials)
self.socket = self.socket.unwrap()
return True
And the client script:和客户端脚本:
#!/usr/bin/env python3
import socket
import ssl
HOST = '127.0.0.1' # The server's hostname or IP address
PORT = 4000 # The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sockt:
sockt.connect((HOST, PORT))
while (True):
v = input("JogoDaVelha> ")
comando = v.split()
if (comando[0] == "login"):
# Sends a message indicating to the server to start an SSL handshake
sockt.sendall(bytes('login', 'ascii'))
sockt = ssl.wrap_socket(sockt)
sockt.send(bytes(comando[1] + ' ' + comando[2], 'ascii'))
sockt = sockt.unwrap()
continue
if (v == "exit"):
break
sockt.sendall(bytes(v, 'ascii'))
data = sockt.recv(1024)
print('Received', repr(data))
Can anyone help me?谁能帮我?
I know its super late, and I must admit I know next to nothing about the subject.我知道它非常晚,我必须承认我对这个主题几乎一无所知。 HOWEVER, I had the same error caused by an id10T error.. (using https in an address when I should have use http).但是,由于 id10T 错误,我遇到了同样的错误。(在我应该使用 http 的地址中使用 https)。 When I dug into the ssl.py file mentioned in the error I found the following message in the class definition:当我挖掘错误中提到的 ssl.py 文件时,我在 class 定义中发现了以下消息:
When compared to SSLSocket
, this object lacks the following features:与SSLSocket
相比,此 object 缺少以下功能:
* Any form of network IO, including methods such as ``recv`` and ``send``.
* The ``do_handshake_on_connect`` and ``suppress_ragged_eofs`` machinery.
Looks like the socket 'send' and 'recv' aren't compatible for your approach.看起来套接字“发送”和“接收”与您的方法不兼容。 Apologies I can't offer more info, just happened to stumble on your post looking for my own answer抱歉,我无法提供更多信息,只是偶然发现您的帖子正在寻找我自己的答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.