如何在python中导入OpenSSL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30602843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:41:03  来源:igfitidea点击:

How to import OpenSSL in python

pythonpython-2.7openssl

提问by user2192774

I am trying to run this simple code to retrieve SSL certificate:

我正在尝试运行这个简单的代码来检索 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()

But I get error saying:

但我收到错误说:

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

I am aware that I have to import OpenSSL. But I do not know how? and where to get the OpenSSL from? I downloaded a module called pyOpenSSL from https://pypi.python.org/pypi/pyOpenSSLWhich contains two folders: pyOpenSSL-0.15.1.dist-info and OpenSSL. When I tried to add import OpenSSL or import pyOpenSSL I get errors. Can you explain clearly please, how to import these libraries or modules? where they should be placed? if not in the same directory of my code file? how to write the path in the import syntax?? Please, help.

我知道我必须导入 OpenSSL。但是我不知道怎么做?以及从哪里获得 OpenSSL?我从https://pypi.python.org/pypi/pyOpenSSL下载了一个名为 pyOpenSSL 的模块, 其中包含两个文件夹:pyOpenSSL-0.15.1.dist-info 和 OpenSSL。当我尝试添加 import OpenSSL 或 import pyOpenSSL 时,出现错误。你能解释清楚,如何导入这些库或模块吗?它们应该放在哪里?如果不在我的代码文件的同一目录中?导入语法中的路径怎么写??请帮忙。

EDIT:when tried to add from OpenSSL import SSLin the code, I got:

编辑:当尝试添加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

采纳答案by hd1

From the tests:

测试

from OpenSSL import SSL

Response to the edit: pip install pyopensslshould have installed six. If you're trying to install yourself, I'd not do this, but you can install the dependencies manually using pip install six cryptographyand then your import should work fine. If not, leave a comment and I'll do some further investigation.

对编辑的回应:pip install pyopenssl应该已经安装了六个。如果您尝试自己安装,我不会这样做,但是您可以使用手动安装依赖项pip install six cryptography,然后您的导入应该可以正常工作。如果没有,请发表评论,我会做一些进一步的调查。

Response to comment: There are instructions on installing pip on windows.

回复评论:有关于在 windows安装 pip 的说明