Choosing The SSL Version In Python Requests

Quelle: http://lukasa.co.uk/2013/01/Choosing_SSL_Version_In_Requests/

Over the last few months (and probably for quite a while before then too), a few issues have been raised on the Requests GitHub page asking how to select the version of SSL used by Requests. This is actually simple once you know how, so I thought I’d write a short post to show you how it’s done.

A quick note before we begin: it is not possible to select the version of SSL you want to use before Requests v1.0.0 without changing the underlying library code. The following set of instructions will not work if you’re running an earlier version.

How It’s Done

Altogether this is relatively simple. To change the SSL version used in HTTPS, you are expected to subclass the HTTPAdapter class and mount it to a Session object. If, for example, you wanted to force the use of TLSv1, your new Transport Adapter will look like this:

With that done, you can mount it to a Requests Session object:

Of course, this is so easy that it’s simple to write a Transport Adapter that can take an arbitrary SSL type from the ssl package in the constructor and use that. Whack this in a file and import it into whatever you’re doing:

You can mount it to a Session object and just go to town.

Hopefully this will be of use to people. If you have any problems or improvements, leave a note in the comments or drop me a line on Twitter (the url is in the sidebar).