python中的ssl错误是什么意思?

2024-04-19 09:28:17 发布

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

我有以下简单的python代码,用于在客户机和服务器之间执行ssl握手和验证证书:

ssl测试.py:

import ssl
import socket

s = socket.socket()
print "connecting..."
#logging.debug("Connecting")
# Connect with SSL mutual authentication
# We only trust our server's CA, and it only trusts user certificates signed by it
c = ssl.wrap_socket(s, cert_reqs=ssl.CERT_REQUIRED,
                    ssl_version=ssl.PROTOCOL_SSLv3, ca_certs='ca.crt',
                    certfile='user.crt', keyfile='user.key')
c.connect((constants.server_addr, constants.port))

当我执行这个时,我得到以下错误。

>python ssl_test.py
Traceback (most recent call last):
  File "ssl_test.py", line 12, in <module>
    c.connect(('192.168.1.82', 7070))
  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 0] _ssl.c:340: error:00000000:lib(0):func(0):reason(0)

这个错误是什么意思?我该如何解决它?


Tags: inpyimportselfsslonlyserverlib
2条回答

我是Python新手,在搜索了原始ssl.SSLError之后,最终走上了这条路。我知道这对最初的海报没有帮助,但它可能会帮助其他人处理这个错误。大多数Python示例使用:

    ca_certs="/etc/ca_certs_file"

由于此文件不存在,因此会出现此错误。要在最新版本的Linux上使用系统CA证书,请使用:

    ca_certs="/etc/ssl/certs/ca-certificates.crt"

这看起来像http://bugs.python.org/issue2687,其中给出了以下答案:

No, the problem is with your "ca_certs" argument on the client side. You can't use a directory. You must use a file containing a number of concatenated certificates. I'll beef up the documentation to make that clearer.

我看到您的ca_certs是一个文件,而不是一个目录,但也许这仍然会给您一些启示。ca.crt文件的格式正确吗?

相关问题 更多 >