Python 3.7.7 SSL socketserver ConnectionResetError:[WinError 10054]远程主机强制关闭了现有连接
我在win10系统上用python编写了一个简单的客户机-服务器应用程序。我正在使用Python 3.7.7 当客户端连接到服务器时,我收到以下错误:Python 3.7.7 SSL socketserver ConnectionResetError:[WinError 10054]远程主机强制关闭了现有连接,python,windows,ssl,socketserver,Python,Windows,Ssl,Socketserver,我在win10系统上用python编写了一个简单的客户机-服务器应用程序。我正在使用Python 3.7.7 当客户端连接到服务器时,我收到以下错误: Sending data Traceback (most recent call last): File "Client.py", line 23, in <module> ssock.send(b'this is a test\n') File "D:\SecureServer\Python37\lib\ssl.py
Sending data
Traceback (most recent call last):
File "Client.py", line 23, in <module>
ssock.send(b'this is a test\n')
File "D:\SecureServer\Python37\lib\ssl.py", line 1003, in send
return self._sslobj.write(data)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
这是客户:
import socket
import ssl
if __name__ == '__main__':
ctx = ssl.create_default_context()
ctx.verify_mode = ssl.CERT_REQUIRED
ctx.check_hostname = True
certs_folder = './certs/'
server_certs = certs_folder + 'ca-chain.cert.pem'
ctx.load_verify_locations(cafile=server_certs)
client_cert = certs_folder + 'www.example.com.cert.pem'
client_key = certs_folder + 'www.example.com.key.pem'
ctx.load_cert_chain(certfile=client_cert, keyfile=client_key, password=None)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
print(sock)
with ctx.wrap_socket(sock, server_side=False, server_hostname="www.example.com") as ssock:
ssock.connect(("127.0.0.1", 3278))
print("SSL established. Peer: {}".format(ssock.getpeercert()))
ssock.send(b'this is a test\n') # this is line 23
经过深入分析,我发现了一些错误: 1) 客户端证书不能是服务器证书;我对客户端和服务器使用了相同的证书。解决方案:创建正确的客户端证书 2) SSL套接字由于SSL 3路握手而阻塞,因此,如果客户端在发送数据后关闭套接字,服务器将断开连接。必须管理数据传输
import socket
import ssl
if __name__ == '__main__':
ctx = ssl.create_default_context()
ctx.verify_mode = ssl.CERT_REQUIRED
ctx.check_hostname = True
certs_folder = './certs/'
server_certs = certs_folder + 'ca-chain.cert.pem'
ctx.load_verify_locations(cafile=server_certs)
client_cert = certs_folder + 'www.example.com.cert.pem'
client_key = certs_folder + 'www.example.com.key.pem'
ctx.load_cert_chain(certfile=client_cert, keyfile=client_key, password=None)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
print(sock)
with ctx.wrap_socket(sock, server_side=False, server_hostname="www.example.com") as ssock:
ssock.connect(("127.0.0.1", 3278))
print("SSL established. Peer: {}".format(ssock.getpeercert()))
ssock.send(b'this is a test\n') # this is line 23