SSL and TLS in Python requests

I am using the request library for python and I have a little problem: I am connecting to the rest api and after a few days this service will remove the SSL connections, so I can only connect via TLS.

Does anyone know if requests can allow a TLS connection and how to enable it?

+5
source share
2 answers

Requests use the standard Python ssl library under the hood - this supports various versions of SSL and TLS. You can specify requests to use a specific protocol (for example, TLSv1) by creating an HTTPAdapter that configures the instantiated pool pool instance.

I should have done something similar, but was bitten by the fact that we also went through the proxy - in this case, the init_poolmanager method init_poolmanager not called because it uses the ProxyManager . I used this:

 class ForceTLSV1Adapter(adapters.HTTPAdapter): """Require TLSv1 for the connection""" def init_poolmanager(self, connections, maxsize, block=False): # This method gets called when there no proxy. self.poolmanager = poolmanager.PoolManager( num_pools=connections, maxsize=maxsize, block=block, ssl_version=ssl.PROTOCOL_TLSv1, ) def proxy_manager_for(self, proxy, **proxy_kwargs): # This method is called when there is a proxy. proxy_kwargs['ssl_version'] = ssl.PROTOCOL_TLSv1 return super(ForceTLSV1Adapter, self).proxy_manager_for(proxy, **proxy_kwargs) 
+6
source

A few links to help.

https://github.com/kennethreitz/requests/blob/master/requests/packages/urllib3/contrib/pyopenssl.py

https://github.com/kennethreitz/requests/issues/749#issuecomment-19187417

There pull request for urllib3 adds SNI support for Python 3.2+ - link

Hope this helps. Let me know if you need more information.

+1
source

Source: https://habr.com/ru/post/1206155/


All Articles