我正在尝试运行这段简单的代码以获取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