创建标题为ssl.py的python脚本后出现文件错误

时间:2018-04-05 04:10:10

标签: python macos ssl openssl pyopenssl

追踪(最近一次通话):   文件“ssl-common.py”,第14行,in     s = ctx.wrap_socket(s,server_hostname = dst [0])   在wrap_socket中输入文件“/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py”,第363行     _context =自我)   文件“/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py”,第611行, init     self.do_handshake()   在do_handshake中输入文件“/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py”,第840行     self._sslobj.do_handshake() socket.error:[Errno 54]通过对等方重置连接

当我尝试在Mac上运行以下脚本时,我收到了上述错误。我已经通过自制软件重新安装openssl,以及通过pip

重新安装
import socket
import ssl
import OpenSSL.crypto as crypto

dst = ('1.2.3.4',443)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ret = s.connect_ex(dst)
if ret == 0 :
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    s = ctx.wrap_socket(s, server_hostname=dst[0])

    # get certificate
    cert_bin = s.getpeercert(True)
    x509 = crypto.load_certificate(crypto.FILETYPE_ASN1,cert_bin)
    print(x509.get_subject().CN)
else :
    print "socket.connect_ex returned ", ret

1 个答案:

答案 0 :(得分:0)

[Errno 54] Connection reset by peer

这似乎是您想要连接的问题。通过ping等检查连接性。

请检查一下:What does "connection reset by peer" mean?