2013-04-04 39 views
3

我在Python中編寫了一個簡單的回聲服務器客戶端代碼。我也產生了keyfile.pem和certfile.pem,用命令:TLS回聲服務器和echoclient在python崩潰

openssl genrsa -des3 -out keyfile.pem 2048

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

,當我跑的客戶端服務器,它問我關於passphase:Enter PEM pass phrase:和我enterec正確的文本,得到了錯誤(不知道爲什麼):

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。

回答

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

Geez @Mimi,現在工作!你能解釋一下,我的證書和密鑰文件有什麼問題?謝謝:) – yak 2013-06-15 11:11:41