6

I'm using requests library for python, and I have a little problem: I'm connecting to a rest api and, in few days, this service will delete SSL connections, so I'll only can connect through TLS.

Does someone know if requests allows TLS connection and how enable it?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Javier Vazquez
  • 117
  • 1
  • 1
  • 9
  • what's your version of Python? This [SO Question & Answer](http://stackoverflow.com/questions/18578439/using-requests-with-tls-doesnt-give-sni-support) may give you some insights. – Anzel Nov 04 '14 at 11:03
  • 1
    …and what ssl library in what version are you using? – Klaus D. Nov 04 '14 at 11:05

1 Answers1

6

Requests uses the Python standard library ssl module under the hood - this supports various versions of SSL and TLS. You can tell requests to use a specific protocol (like TLSv1) by creating an HTTPAdapter that customizes the PoolManager instance that gets created.

I had to do something like this, but was bitten by the fact that we were also going via a proxy - in that case the init_poolmanager method isn't called, because it uses a ProxyManager instead. 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's 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)
babbageclunk
  • 8,523
  • 1
  • 33
  • 37