python cod疑难解答

2024-05-13 05:08:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习python的第一步,请原谅我的问题。我想运行下面的代码(取自:http://docs.python.org/library/ssl.html):

import socket, ssl, pprint
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# require a certificate from the server
ssl_sock = ssl.wrap_socket(s,
                           ca_certs="F:/cert",
                           cert_reqs=ssl.CERT_REQUIRED)

ssl_sock.connect(('www.versign.com', 443))

print repr(ssl_sock.getpeername())
print ssl_sock.cipher()
print pprint.pformat(ssl_sock.getpeercert())

# Set a simple HTTP request -- use httplib in actual code.
ssl_sock.write("""GET / HTTP/1.0\r
Host: www.verisign.com\r\n\r\n""")

# Read a chunk of data.  Will not necessarily
# read all the data returned by the server.
data = ssl_sock.read()

# note that closing the SSLSocket will also close the underlying socket
ssl_sock.close()  

我有以下错误:

Traceback (most recent call last): File "C:\Users\e\workspace\PythonTesting\source\HelloWorld.py", line 38, in ssl_sock.connect(('www.versign.com', 443))

File "C:\Python27\lib\ssl.py", line 331, in connect

self._real_connect(addr, False)

File "C:\Python27\lib\ssl.py", line 314, in _real_connect

self.ca_certs, self.ciphers)

ssl.SSLError: [Errno 185090050] _ssl.c:340: error:0B084002:x509 certificate routines:X509_load_cert_crl_file:system lib

python中的错误报告看起来并不能指导您找到问题的根源。我可能弄错了。有谁能帮忙告诉我代码有什么问题吗?


Tags: theinpycomssldatacertlib
3条回答

文件系统中是否存在引用的证书?我认为该错误是对来自此代码的无效证书的响应:

ssl_sock=ssl.wrap_socket(s,ca_certs=“F:/cert”,cert_reqs=ssl.cert_必选)

众所周知,Python标准库很难使用。相反,您可能希望使用请求库。有关发送证书的文档位于:http://docs.python-requests.org/en/latest/user/advanced/#ssl-cert-verification

您的代码引用的是驱动器“F:”上的证书文件(使用ca_certs参数),该文件在执行过程中找不到--有吗?

请参阅相关的documentation

The ca_certs file contains a set of concatenated “certification authority” certificates, which are used to validate certificates passed from the other end of the connection.

相关问题 更多 >