336

Tried to perform REST GET through python requests with the following code and I got error.

Code snip:

import requests
header = {'Authorization': 'Bearer...'}
url = az_base_url + az_subscription_id + '/resourcegroups/Default-Networking/resources?' + az_api_version
r = requests.get(url, headers=header)

Error:

/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:79: 
          InsecurePlatformWarning: A true SSLContext object is not available. 
          This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. 
          For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning

My python version is 2.7.3. I tried to install urllib3 and requests[security] as some other thread suggests, I still got the same error.

Wonder if anyone can provide some tips?

5
  • 12
    Did you read the link (urllib3.readthedocs.org/en/latest/…)? It gives two suggestions: either upgrade to at least Python 2.7.9 or use pyOpenSSL.
    – Ned Deily
    Mar 18, 2015 at 23:25
  • 1
    Thanks for your comments. I tried to install pyOpenSSL but failed. Let me try to upgrade to 2.7.9 and see if that fix the problem. Mar 19, 2015 at 20:10
  • @user4525298 did by updgrading 2.7.9 solve your problem?
    – bohr
    Feb 9, 2016 at 9:11
  • @NedDeily: the link is now broken
    – Sheena
    Jul 4, 2016 at 6:44
  • @Sheena: thanks. urllib3.readthedocs.io/en/latest/…
    – Ned Deily
    Jul 5, 2016 at 14:33

2 Answers 2

552

The docs give a fair indicator of what's required., however requests allow us to skip a few steps:

You only need to install the security package extras (thanks @admdrew for pointing it out)

$ pip install requests[security]

or, install them directly:

$ pip install pyopenssl ndg-httpsclient pyasn1

Requests will then automatically inject pyopenssl into urllib3


If you're on ubuntu, you may run into trouble installing pyopenssl, you'll need these dependencies:

$ apt-get install libffi-dev libssl-dev
17
  • 26
    You can also pip install requests[security] and only import requests.
    – admdrew
    Mar 26, 2015 at 19:59
  • 3
    the answer above is detailed, however @admdrew provides perhaps the simplest solution with least amount of headache.
    – Chrispy
    Apr 8, 2015 at 4:17
  • 9
    Don't forget the --upgrade option or you may not actually install anything. And if you want your system environment to be secure you want to sudo the install. So sudo pip install --upgrade pyopenssl ndg-httpsclient pyasn1 pip worked for me on Fedora 20 despite the deprecation warning for uninstalling and upgrading the distutils-installed pyopenssl package.
    – hobs
    Apr 15, 2015 at 22:13
  • 3
    @hobs Regarding sudo pip install, e.g. stackoverflow.com/questions/29310688/… As a general, but not unbreakable, rule, you should either use pip install --user or preferably use virtualenv to keep everything contained and pinned.
    – knickum
    Sep 14, 2015 at 16:54
  • 1
    Also, if you're having trouble installing the Ubuntu dependencies (I was), there's a bit more complete description here: stackoverflow.com/questions/22073516/… (answer is quite far down)
    – dpb
    Dec 18, 2015 at 7:15
72

If you are not able to upgrade your Python version to 2.7.9, and want to suppress warnings,

you can downgrade your 'requests' version to 2.5.3:

pip install requests==2.5.3

Bugfix disclosure / Warning introduced in 2.6.0

6
  • 51
    While this works, this probably isn't the best answer.
    – admdrew
    Mar 26, 2015 at 19:58
  • 24
    That diff hides the fact that 2.6.0 contains a security fix; downgrading leaves you exposed.
    – Martijn Pieters
    Mar 29, 2015 at 16:04
  • 7
    admdrew's comment on the answer above is a much better and simple solution. Simply pip install requests[security]
    – jjj
    Apr 8, 2015 at 15:23
  • 3
    The above answers might be better for most scenarios, but this one is the only one that works on a shared host with native libraries you don't control. requests[security] didn't work, but the older version did.
    – Jordan
    Mar 16, 2017 at 18:04
  • 3
    If pip itself is affected by InsecurePlatformWarning and cannot install anything from PyPI, it can be fixed with this step-by-step guide -- handy for older linux systems.. @Brian Feb 10, 2019 at 16:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.