Your Python installation does not support SSL? You need to compile it again after editing Setup.dist file located in the Python source directory. Let’s see how to do that.
Before we begin, check if your existing Python installation supports OpenSSL as shown below:
In the python prompt, type ‘import ssl’
>>> import ssl
If you see error as below, then Python does not support SSL.
$ python Python 2.4.3 (#1, Jan 9 2013, 06:47:03) [GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import ssl Traceback (most recent call last): File "<stdin>", line 1, in ? ImportError: No module named ssl >>>
Note: In older versions of Python, you may try using ‘hasattr‘ function as shown below:
>>> import socket >>> hasattr(socket,"SSL") False >>>
If you see “False“, then Python does not support SSL.
How to Compile Python from source with OpenSSL Support
OpenSSL should have been installed by default, but if you don’t have one, install it as shown below.
Install OpenSSL and its development packages as below:
$ yum install openssl
$ yum install openssl-devel
Install OpenSSL from source:
$ wget http://www.openssl.org/source/openssl-1.0.2e.tar.gz
$ ./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl
Note: By default openssl will be installed under /usr/local/ssl. If you do not want to mess with existing SSL installation, then install it in a different directory.
$ make $ make test $ make install
OpenSSL installation is done. Let’s us now compile Python.
Download Python source:
$tar xvzf Python-3.5.1.tgz
Search for “SSL” and uncomment the code as shown below (in version 3.5, from line 203 to 210):
_socket socketmodule.c # Socket module helper for SSL support; you must comment out the other # socket line above, and possibly edit the SSL variable: #SSL=/usr/local/ssl _ssl _ssl.c \ -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \ -L$(SSL)/lib -lssl -lcrypto
Note: In case, if you had installed OpenSSL in a non-standard location, then you need to change ‘SSL‘ to refer the correct path in the above code.
_socket socketmodule.c # Socket module helper for SSL support; you must comment out the other # socket line above, and possibly edit the SSL variable: SSL=/usr/local/openssl _ssl _ssl.c \ -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \ -L$(SSL)/lib -lssl -lcrypto
$ ./configure $ make $ make install
Check Python for OpenSSL Support
In python prompt, type “import ssl”
>>> import ssl
If you don’t see any error, then Python supports SSL. That’s it!
I love bugs and I love fixing them!