Monday, March 6, 2017

Dealing with SSL HandShake failure with Python Requests (_ssl.c:590)

I was working with Requests trying to talk to a REST api when I encountered this SSL HandShake error. It was fairly easy to replicate because it happens every time I tried talking to a HTTPS endpoint or resource. It didn't matter if it was a GET or POST action. I suspect it'd be the same for other actions like PATCH or DELETE.

So what's the fix? Uninstall-install then maybe an update or upgrade.
  1. I updated Python to 2.7.11. You might not need to do this if your up-to-date. 
  2. I uninstalled requests. - $ pip uninstall requests
  3. I uninstalled urllib3. - $ pip uninstall urllib3
  4. Installed crypto dependencies or libs. This is where it gets hairy.

    First I had to update my machine's openSSL library. This could range from easy to hair pulling hard. Thankfully, I had brew on my machine so it was relatively easy.

    Anyhow you'll want to see this from the terminal after the update.

    $ openssl version
    OpenSSL 0.9.8zh 14 Jan 2016
    

    After you'll need to run this command:

    $ pip install pyopenssl ndg-httpsclient pyasn1
    

     This installs the rest of the crypto dependencies.
  5. I installed urllib3 back. - $ pip install urllib3
    I also installed urllib3[secure]. - $ pip install urllib3[secure]
    You might not need the secure version but I covering all the bases.
  6.  Install requests back. - $ pip install requests
    Same deal with urllib3, you might also want to install request with security.

    $ pip install request[security]
Why this works? I suspect that there's an installation-time check for either urllib3 (or requests) for the crypto stuff that keeps things from working without the uninstall-reinstall.