How to import OpenSSL in python
Question:
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()
But I get error saying:
Traceback (most recent call last):
File "C:UserseDesktopPythonssltest.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/pyOpenSSL
Which 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.
EDIT:
when tried to add from OpenSSL import SSL
in the code, I got:
C:UserseDesktopPythonssl>test.py
Traceback (most recent call last):
File "C:UserseDesktopPythonssltest.py", line 2, in <module>
from OpenSSL import SSL
File "C:UserseDesktopPythonsslOpenSSL__init__.py", line 8, in <module>
from OpenSSL import rand, crypto, SSL
File "C:UserseDesktopPythonsslOpenSSLrand.py", line 9, in <module>
from six import integer_types as _integer_types
ImportError: No module named six
Answers:
From the tests:
from OpenSSL import SSL
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.
Response to comment: There are instructions on installing pip on windows.
Both these forms works.
import OpenSSL.SSL # or
from OpenSSL import SSL
You can also install using conda.
Please be aware that there are two python packages of similar names: openssl and pyopenssl.
- Both has the same import name OpenSSL
- Both are being used by many other packages. So it is not one supersede the other. Both are needed in general.
- Install pyopenssl looks like will install openssl too.
- For example, python itself uses openssl, while conda uses pyopenssl.
Here are the code snipes to see:
conda create -n x1 python=3.8
conda list -n x1 | grep openssl # will see openssl
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()
But I get error saying:
Traceback (most recent call last):
File "C:UserseDesktopPythonssltest.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/pyOpenSSL
Which 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.
EDIT:
when tried to add from OpenSSL import SSL
in the code, I got:
C:UserseDesktopPythonssl>test.py
Traceback (most recent call last):
File "C:UserseDesktopPythonssltest.py", line 2, in <module>
from OpenSSL import SSL
File "C:UserseDesktopPythonsslOpenSSL__init__.py", line 8, in <module>
from OpenSSL import rand, crypto, SSL
File "C:UserseDesktopPythonsslOpenSSLrand.py", line 9, in <module>
from six import integer_types as _integer_types
ImportError: No module named six
From the tests:
from OpenSSL import SSL
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.
Response to comment: There are instructions on installing pip on windows.
Both these forms works.
import OpenSSL.SSL # or
from OpenSSL import SSL
You can also install using conda.
Please be aware that there are two python packages of similar names: openssl and pyopenssl.
- Both has the same import name OpenSSL
- Both are being used by many other packages. So it is not one supersede the other. Both are needed in general.
- Install pyopenssl looks like will install openssl too.
- For example, python itself uses openssl, while conda uses pyopenssl.
Here are the code snipes to see:
conda create -n x1 python=3.8
conda list -n x1 | grep openssl # will see openssl