2013-04-11 25 views
2

我試圖運行的文檔中的ssl模塊這裏提供的示例代碼:http://docs.python.org/2/library/ssl.html#client-side-operationPython的SSL的例子給出了「連接被對方​​復位」的錯誤

服務器端代碼類似的例子在文檔中給出,它拋出這個異常:

Traceback (most recent call last): 
    File "serve.py", line 16, in <module> 
    ssl_version=ssl.PROTOCOL_TLSv1) 
    File "/usr/lib/python2.7/ssl.py", line 381, in wrap_socket 
    ciphers=ciphers) 
    File "/usr/lib/python2.7/ssl.py", line 143, in __init__ 
    self.do_handshake() 
    File "/usr/lib/python2.7/ssl.py", line 305, in do_handshake 
    self._sslobj.do_handshake() 
socket.error: [Errno 104] Connection reset by peer 

和客戶端代碼,也類似於文檔中的例子,引發此異常:

Traceback (most recent call last): 
    File "client.py", line 8, in <module> 
    ssl_sock.connect((host, port)) 
    File "/usr/lib/python2.7/ssl.py", line 331, in connect 
    self._real_connect(addr, False) 
    File "/usr/lib/python2.7/ssl.py", line 324, in _real_connect 
    raise e 
socket.error: [Errno 104] Connection reset by peer 

就我所見,我已經非常仔細地複製了文檔中提供的示例,所以我不知道問題是什麼。我所有的TCP,UDP和ICMP端口都是開放的,所以我不認爲這是防火牆問題。 (我已經編輯了這個問題來簡化我的代碼,因爲它與鏈接中提供的示例非常相似。如果您想查看我的代碼,請查看此問題的歷史記錄。 )

+0

如果在不指定證書文件的情況下使用它,該怎麼辦? – User 2013-04-11 17:30:12

+0

@User:它會拋出一個異常,指出'server_side'爲true時,必須指定它們。 – Flimm 2013-04-11 18:10:25

回答

4

我發現了這個問題。我生成私鑰和使用命令這樣的證書:

$ openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout key.pem 
Generating a 1024 bit RSA private key 
# ... 
Country Name (2 letter code) [AU]:US 
State or Province Name (full name) [Some-State]:MyState 
Locality Name (eg, city) []:Some City 
Organization Name (eg, company) [Internet Widgits Pty Ltd]:My Organization, Inc. 
Organizational Unit Name (eg, section) []:My Group 
Common Name (eg, YOUR name) []:myserver.mygroup.myorganization.com 
Email Address []:[email protected] 
$ 

的關鍵部分是,「通用名稱」中輸入必須的服務器的域名相匹配。我認爲當cacertsssl.CERT_NONE,這是默認爲wrap_socket,這不會被檢查,但我錯了。它總是被檢查。一晚的睡眠,這是我想要驗證的第一件事!

希望這會對其他人得到這個神祕的錯誤信息有用。

如果這樣不能解決問題,您可能會患上深度包檢測。當我在大學網絡上時,我再次遇到了這個錯誤,但在其他任何網絡上都沒有,我確信這是因爲深入的數據包檢查。

相關問題