如何在python中导入OpenSSL

如何在python中导入OpenSSL

本文介绍了如何在python中导入OpenSSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行以下简单代码来检索SSL证书:

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

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.当我尝试添加导入OpenSSL或导入pyOpenSSL时,出现错误.您能否清楚说明,如何导入这些库或模块?他们应该放在哪里?如果不在我的代码文件的同一目录中?如何在导入语法中编写路径?请帮忙.

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.

当尝试在代码中添加from OpenSSL import SSL时,我得到了:

when tried to add from OpenSSL import SSL in the code, I got:

    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

推荐答案

来自测试:

from OpenSSL import SSL

对编辑的响应:pip install pyopenssl应该已经安装了六个.如果您要自行安装,则不会执行此操作,但是您可以使用pip install six cryptography手动安装依赖项,然后导入应该可以正常工作.如果没有,请发表评论,我会做进一步的调查.

Response to the edit: pip install pyopenssl should 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 cryptography and then your import should work fine. If not, leave a comment and I'll do some further investigation.

回应评论:有关在Windows上安装pip的说明.

这篇关于如何在python中导入OpenSSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 02:19