Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSL and TLS in Python requests

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?

like image 458
Javier Vazquez Avatar asked Nov 04 '14 10:11

Javier Vazquez


People also ask

Does Python requests use TLS?

Requests uses the Python standard library ssl module under the hood - this supports various versions of SSL and TLS.

What is ssl certificate Python requests?

Requests verifies SSL certificates for HTTPS requests, just like a web browser. SSL Certificates are small data files that digitally bind a cryptographic key to an organization's details. Often, a website with a SSL certificate is termed as secure website.

How does ssl work in Python?

Your browser and the server exchange public keys. Your browser and the server generate a shared private key. Your browser and the server encrypt and decrypt messages using this shared key through symmetric encryption.

Is Python requests library secure?

Requests is the only Non-GMO HTTP library for Python, safe for human consumption. Warning: Recreational use of other HTTP libraries may result in dangerous side-effects, including: security vulnerabilities, verbose code, reinventing the wheel, constantly reading documentation, depression, headaches, or even death.


1 Answers

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)
like image 122
babbageclunk Avatar answered Sep 22 '22 03:09

babbageclunk