我在Python中编写了一个简单的echo服务器客户端代码。我还使用命令生成了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。
答案 0 :(得分:3)
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout my.key -out my.crt