ssl module in Python 2’s standard library does not support the SNI
TLS-extension, and it probably never will, which means that the brilliant
requests HTTP library is also missing SNI-support on Python 2.
urllib3 has a contrib-module for SNI-support on Python 2 this might
change, but if you don’t want to wait for the updated
urllib3 to be included
requests release, you can monkey-patch
Install the required packages:
pip install pyOpenSSL ndg-httpsclient pyasn1
requests_pyopenssl.pyand put it somewhere on your
requestssomewhere early in your application (possibly in a
try: import requests_pyopenssl from requests.packages.urllib3 import connectionpool connectionpool.ssl_wrap_socket = requests_pyopenssl.ssl_wrap_socket except ImportError: pass
If you want to test that it works, the following Python script should print
requests SNI-support Ok” when run:
try: import requests_pyopenssl from requests.packages.urllib3 import connectionpool connectionpool.ssl_wrap_socket = requests_pyopenssl.ssl_wrap_socket except ImportError: raise import requests for host in ['alice', 'bob', 'carol', 'dave', 'mallory', 'www']: response = requests.get('https://%s.sni.velox.ch' % host) html = response.content.decode('utf-8', 'replace') assert 'Great!' in html, host print 'requests SNI-support Ok'