如何在 python 中导入 OpenSSL
How to import OpenSSL in python
我正在尝试 运行 这个简单的代码来检索 SSL 证书:
import ssl, socket
#print ssl.get_server_certificate(('www.google.com', 443))
cert=ssl.get_server_certificate(('www.google.com', 443))
# OpenSSL
x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
x509.get_subject().get_components()
但我收到错误提示:
Traceback (most recent call last):
File "C:\Users\e\Desktop\Python\ssl\test.py", line 6, in <module>
x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
NameError: name 'OpenSSL' is not defined
我知道我必须导入 OpenSSL。但是我不知道怎么做?以及从哪里获取 OpenSSL?
我从 https://pypi.python.org/pypi/pyOpenSSL 下载了一个名为 pyOpenSSL 的模块
其中包含两个文件夹:pyOpenSSL-0.15.1.dist-info 和 OpenSSL。
当我尝试添加 import OpenSSL 或 import pyOpenSSL 时出现错误。
你能解释清楚吗,如何导入这些库或模块?他们应该放在哪里?如果不在我的代码文件的同一目录中?如何在导入语法中写入路径??
请帮忙。
编辑:
当试图在代码中添加 from OpenSSL import SSL
时,我得到:
C:\Users\e\Desktop\Python\ssl>test.py
Traceback (most recent call last):
File "C:\Users\e\Desktop\Python\ssl\test.py", line 2, in <module>
from OpenSSL import SSL
File "C:\Users\e\Desktop\Python\ssl\OpenSSL\__init__.py", line 8, in <module>
from OpenSSL import rand, crypto, SSL
File "C:\Users\e\Desktop\Python\ssl\OpenSSL\rand.py", line 9, in <module>
from six import integer_types as _integer_types
ImportError: No module named six
来自tests:
from OpenSSL import SSL
对编辑的回复:pip install pyopenssl
应该已经安装了六个。如果你想自己安装,我不会这样做,但你可以使用 pip install six cryptography
手动安装依赖项,然后你的导入应该可以正常工作。如果没有,请发表评论,我会做进一步调查。
评论回复:installing pip on windows上有说明。
这两种形式都有效。
import OpenSSL.SSL # or
from OpenSSL import SSL
您也可以使用 conda 安装。
请注意,有两个 python 包的名称相似:openssl 和 pyopenssl。
- 两者具有相同的导入名称 OpenSSL
- 许多其他包都在使用两者。所以它不是一个取代另一个。一般都需要。
- 安装 pyopenssl 看起来也会安装 openssl。
- 例如python本身使用openssl,而conda使用pyopenssl。
这里是代码截图:
conda create -n x1 python=3.8
conda list -n x1 | grep openssl # will see openssl
我正在尝试 运行 这个简单的代码来检索 SSL 证书:
import ssl, socket
#print ssl.get_server_certificate(('www.google.com', 443))
cert=ssl.get_server_certificate(('www.google.com', 443))
# OpenSSL
x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
x509.get_subject().get_components()
但我收到错误提示:
Traceback (most recent call last):
File "C:\Users\e\Desktop\Python\ssl\test.py", line 6, in <module>
x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
NameError: name 'OpenSSL' is not defined
我知道我必须导入 OpenSSL。但是我不知道怎么做?以及从哪里获取 OpenSSL? 我从 https://pypi.python.org/pypi/pyOpenSSL 下载了一个名为 pyOpenSSL 的模块 其中包含两个文件夹:pyOpenSSL-0.15.1.dist-info 和 OpenSSL。 当我尝试添加 import OpenSSL 或 import pyOpenSSL 时出现错误。 你能解释清楚吗,如何导入这些库或模块?他们应该放在哪里?如果不在我的代码文件的同一目录中?如何在导入语法中写入路径?? 请帮忙。
编辑:
当试图在代码中添加 from OpenSSL import SSL
时,我得到:
C:\Users\e\Desktop\Python\ssl>test.py
Traceback (most recent call last):
File "C:\Users\e\Desktop\Python\ssl\test.py", line 2, in <module>
from OpenSSL import SSL
File "C:\Users\e\Desktop\Python\ssl\OpenSSL\__init__.py", line 8, in <module>
from OpenSSL import rand, crypto, SSL
File "C:\Users\e\Desktop\Python\ssl\OpenSSL\rand.py", line 9, in <module>
from six import integer_types as _integer_types
ImportError: No module named six
来自tests:
from OpenSSL import SSL
对编辑的回复:pip install pyopenssl
应该已经安装了六个。如果你想自己安装,我不会这样做,但你可以使用 pip install six cryptography
手动安装依赖项,然后你的导入应该可以正常工作。如果没有,请发表评论,我会做进一步调查。
评论回复:installing pip on windows上有说明。
这两种形式都有效。
import OpenSSL.SSL # or
from OpenSSL import SSL
您也可以使用 conda 安装。
请注意,有两个 python 包的名称相似:openssl 和 pyopenssl。
- 两者具有相同的导入名称 OpenSSL
- 许多其他包都在使用两者。所以它不是一个取代另一个。一般都需要。
- 安装 pyopenssl 看起来也会安装 openssl。
- 例如python本身使用openssl,而conda使用pyopenssl。
这里是代码截图:
conda create -n x1 python=3.8
conda list -n x1 | grep openssl # will see openssl