python - 如何在 python 中导入 OpenSSL

标签 python python-2.7 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 6 cryptography 手动安装依赖项,然后您的导入应该可以正常工作。如果没有,请发表评论,我会做进一步调查。

评论回复:installing pip on windows上有说明.

关于python - 如何在 python 中导入 OpenSSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30602843/

相关文章:

python - 如何从 Python 中的十六进制字符串中删除 '\x'?

c++ - 使用OpenSSL和锁的多线程程序随机崩溃

python - 修补作为另一个类的属性的类实例属性

python - 为什么我得到 : "Length of values does not match length of index" error?

python - 使用 Python imaplib 永久删除 Gmail 电子邮件

python - 无法使用python将JSON文件从谷歌云存储加载到bigquery

php - Android应用内购买服务器签名验证使用php OpenSSL

c - openssl 升级后 AES 解密结果发生变化

python 3 : Demystifying encode and decode methods

python - 如何在一段时间后停止Python程序