GNU/Linux >> Tutoriels Linux >  >> Linux

Comment compiler et installer Python avec le support OpenSSL ?

Votre installation Python ne supporte pas SSL ? Vous devez le compiler à nouveau après avoir modifié Setup.dist fichier situé dans le répertoire source de Python. Voyons comment procéder.

Avant de commencer, vérifiez si votre installation Python existante prend en charge OpenSSL comme indiqué ci-dessous :

$ python

Dans l'invite python, tapez "import ssl"

>>> import ssl

Si vous voyez l'erreur ci-dessous, Python ne prend pas en charge 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
>>>

Remarque : Dans les anciennes versions de Python, vous pouvez essayer d'utiliser 'hasattr ‘ fonction comme indiqué ci-dessous :

>>> import socket
>>> hasattr(socket,"SSL")
False
>>>

Si vous voyez "Faux ", alors Python ne prend pas en charge SSL.

Comment compiler Python à partir de la source avec le support OpenSSL

Prérequis :

OpenSSL devrait avoir été installé par défaut, mais si vous n'en avez pas, installez-le comme indiqué ci-dessous.

Installer OpenSSL et ses packages de développement comme ci-dessous :

$ yum install openssl
$ yum install openssl-devel

(ou)

Installez OpenSSL depuis la source :

$ wget http://www.openssl.org/source/openssl-1.0.2e.tar.gz
$ ./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl

Remarque : Par défaut, openssl sera installé sous /usr/local/ssl . Si vous ne voulez pas jouer avec l'installation SSL existante, installez-la dans un répertoire différent.

$ make
$ make test
$ make install

L'installation d'OpenSSL est terminée. Compilons maintenant Python.

Télécharger la source Python :

$wget https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz
$tar xvzf Python-3.5.1.tgz
$vim Modules/Setup.dist

Recherchez "SSL ” et décommentez le code comme indiqué ci-dessous (en version 3.5, de la ligne 203 à 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

Remarque : Au cas où, si vous aviez installé OpenSSL dans un emplacement non standard, vous devez alors changer 'SSL ' pour faire référence au chemin correct dans le code ci-dessus.

Par exemple,

_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

Une fois fait,

$ ./configure
$ make
$ make install

Vérifiez Python pour la prise en charge d'OpenSSL

$/usr/local/bin/python3.5

Dans l'invite python, tapez "import ssl

>>> import ssl

Si vous ne voyez aucune erreur, alors Python prend en charge SSL. C'est tout !


Linux
  1. Comment installer Python 2 et Python 3 sur Fedora 35

  2. Comment installer Python sur Ubuntu 20.04 et 18.04 ?

  3. Comment installer Elasticsearch sur Ubuntu 22.04 avec SSL

  4. Comment installer Apache 2 avec SSL sur Linux (avec mod_ssl, openssl)

  5. Comment compiler le fichier .c avec OpenSSL inclut ?

Comment installer Flask avec Python 3 sur Ubuntu 18.04

Comment installer Python 2 et Python 3 sur CentOS 8

Comment installer FTP avec SSL dans Rocky Linux et AlmaLinux

Comment installer Python 2.7 sur CentOS 7.1 ou 6.7 avec Anaconda

Comment installer Apache avec Python Mod_wsgi sur Debian 10

Comment installer et configurer WordPress avec Ansible