[urllib3|https://github.com/shazow/urllib3] est un module python extrêmement puissant, qui enrichit les différents modules relatifs aux réseaux présents dans la bibliothèque standard.

Il existe différentes manières pour effectuer une requête (il suffit de voir la [documentation|https://urllib3.readthedocs.io/en/latest/user-guide.html]). Jusqu’ici je ne m’étais pas penché sur le cas du protocole [??HTTPS|HyperText Transfer Protocol Secure??|https://fr.wikipedia.org/wiki/HyperText_Transfer_Protocol_Secure].

Par défaut on obtient ce message :

> /usr/local/lib/python2.7/site-packages/urllib3/connectionpool.py:841: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: [https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings]
>
> InsecureRequestWarning)

Même si l’on utilise la classe [urllib3.connectionpool.HTTPSConnectionPool|https://urllib3.readthedocs.io/en/latest/reference/index.html#urllib3.connectionpool.HTTPSConnectionPool], le  »warning » est toujours présent.

Il faut faire appel à un autre module, [certifi|https://certifi.io/en/latest/], donc l’instance de __HTTPSConnectionPool__ s’écrit alors :

///
[…]

conn = urllib3.connectionpool.HTTPSConnectionPool(host, cert_reqs=’CERT_REQUIRED’, ca_certs=certifi.where())
res = conn.request(‘GET’, ‘/’)

[…]
///

En annexe, vous trouverez un exemple complet.

On le lance de cette manière :

///
python npmjs.py
HTTPHeaderDict({‘Content-Length’: ‘3143’, ‘Via’: ‘1.1 varnish’, ‘X-Cache’: ‘MISS’, ‘Accept-Ranges’: ‘bytes’, ‘X-Timer’: ‘S1476454061.911429,VS0,VE292’, ‘Vary’: ‘Accept-Encoding’, ‘X-Served-By’: ‘cache-fra1231-FRA’, ‘server’: ‘CouchDB/1.5.0 (Erlang OTP/R16B03)’, ‘Connection’: ‘keep-alive’, ‘etag’: ‘ »ETFO48QX32KIQMANPF4IXAOMS »‘, ‘X-Cache-Hits’: ‘0’, ‘Cache-Control’: ‘max-age=300’, ‘Date’: ‘Fri, 14 Oct 2016 14:07:42 GMT’, ‘Content-Type’: ‘application/json’, ‘Age’: ‘0’})
///