gpt4 book ai didi

python - python 中的 TLS echo 服务器和 echoclient 崩溃

转载 作者:行者123 更新时间:2023-11-28 16:44:01 26 4
gpt4 key购买 nike

我用 Python 编写了一个简单的回显服务器客户端代码。我还使用命令生成了 keyfile.pem 和 certfile.pem:

openssl genrsa -des3 -out keyfile.pem 2048

openssl req -new -key keyfile.pem -out certfile.pem

当我运行客户端服务器时,它询问我有关 passphase 的信息:Enter PEM pass phrase: 然后我输入正确的文本并得到错误(真的不知道为什么):

Traceback (most recent call last):   File "echo_server.py", line 19, in <module>
connection, client_address= tls_server.accept() File "/usr/lib/python2.7/ssl.py", line 354, in accept
suppress_ragged_eofs=self.suppress_ragged_eofs), File "/usr/lib/python2.7/ssl.py", line 141, in __init__
ciphers) ssl.SSLError: [Errno 336445449] _ssl.c:365: error:140DC009:SSL routines:SSL_CTX_use_certificate_chain_file:PEM lib

这是我的 server.py:

#server side
# echo client
from socket import *
from ssl import *

#create socket
server_socket=socket(AF_INET, SOCK_STREAM)

#Bind to an unused port on the local machine
server_socket.bind(('localhost',6668))

#listen for connection
server_socket.listen (1)
tls_server = wrap_socket(server_socket, ssl_version=PROTOCOL_TLSv1, cert_reqs=CERT_NONE, server_side=True, keyfile='./keyfile.pem', certfile='./certfile.pem')

print('server started')

#accept connection
connection, client_address= tls_server.accept()
print ('connection from', client_address)

#server is not finnished
finnished =False

#while not finnished
while not finnished:


#send and receive data from the client socket
data_in=connection.recv(1024)
message=data_in.decode()
print('client send',message)

if message=='quit':
finnished= True
else:

data_out=message.encode()
connection.send(data_out)

#close the connection
connection.shutdown(SHUT_RDWR)
connection.close()

#close the server socket
server_socket.shutdown(SHUT_RDWR)
server_socket.close()

和client.py:

#client side
# echo client
from socket import *
from ssl import *

#user is not finnished
finnished =False

#create socket
client_socket=socket(AF_INET, SOCK_STREAM)
tls_client = wrap_socket(client_socket, ssl_version=PROTOCOL_TLSv1, cert_reqs=CERT_NONE)


#connect to the echo server
tls_client.connect(('localhost',6668))

#while not finnished
while not finnished:

#message
message=input ('enter message: ')

data_out= message.encode ()

#send data out
tls_client.send(data_out)

#receive data
data_in=tls_client.recv(1024)


#decode message
response= data_in.decode()
print('Received from client:', response)

reapet=input('yes or no? ')


if reapet == 'n':
finnished= True
client_socket.send(b'quit')



#close the socket
client_socket.shutdown(SHUT_RDWR)
client_socket.close()

可能出了什么问题?我使用 Kubuntu 12.04 LTS 和 Python 2.7。

最佳答案

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout my.key -out my.crt

关于python - python 中的 TLS echo 服务器和 echoclient 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15820634/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com