Python Requests throwing SSLError

前端 未结 24 2652
小蘑菇
小蘑菇 2020-11-22 02:49

I\'m working on a simple script that involves CAS, jspring security check, redirection, etc. I would like to use Kenneth Reitz\'s python requests because it\'s a great piec

相关标签:
24条回答
  • 2020-11-22 03:41

    $ pip install -U requests[security]

    • Tested on Python 2.7.6 @ Ubuntu 14.04.4 LTS
    • Tested on Python 2.7.5 @ MacOSX 10.9.5 (Mavericks)

    When this question was opened (2012-05) the Requests version was 0.13.1. On version 2.4.1 (2014-09) the "security" extras were introduced, using certifi package if available.

    Right now (2016-09) the main version is 2.11.1, that works good without verify=False. No need to use requests.get(url, verify=False), if installed with requests[security] extras.

    0 讨论(0)
  • 2020-11-22 03:41

    In my case the reason was fairly trivial.

    I had known that the SSL verification had worked until a few days earlier, and was infact working on a different machine.

    My next step was to compare the certificate contents and size between the machine on which verification was working, and the one on which it was not.

    This quickly led to me determining that the Certificate on the 'incorrectly' working machine was not good, and once I replaced it with the 'good' cert, everything was fine.

    0 讨论(0)
  • 2020-11-22 03:43

    The problem you are having is caused by an untrusted SSL certificate.

    Like @dirk mentioned in a previous comment, the quickest fix is setting verify=False:

    requests.get('https://example.com', verify=False)
    

    Please note that this will cause the certificate not to be verified. This will expose your application to security risks, such as man-in-the-middle attacks.

    Of course, apply judgment. As mentioned in the comments, this may be acceptable for quick/throwaway applications/scripts, but really should not go to production software.

    If just skipping the certificate check is not acceptable in your particular context, consider the following options, your best option is to set the verify parameter to a string that is the path of the .pem file of the certificate (which you should obtain by some sort of secure means).

    So, as of version 2.0, the verify parameter accepts the following values, with their respective semantics:

    • True: causes the certificate to validated against the library's own trusted certificate authorities (Note: you can see which Root Certificates Requests uses via the Certifi library, a trust database of RCs extracted from Requests: Certifi - Trust Database for Humans).
    • False: bypasses certificate validation completely.
    • Path to a CA_BUNDLE file for Requests to use to validate the certificates.

    Source: Requests - SSL Cert Verification

    Also take a look at the cert parameter on the same link.

    0 讨论(0)
  • 2020-11-22 03:43

    If you don't bother about certificate just use verify=False.

    import requests
    
    url = "Write your url here"
    
    returnResponse = requests.get(url, verify=False)
    
    0 讨论(0)
  • 2020-11-22 03:45

    If the request calls are buried somewhere deep in the code and you do not want to install the server certificate, then, just for debug purposes only, it's possible to monkeypatch requests:

    import requests.api
    import warnings
    
    
    def requestspatch(method, url, **kwargs):
        kwargs['verify'] = False
        return _origcall(method, url, **kwargs)
    
    _origcall = requests.api.request
    requests.api.request = requestspatch
    warnings.warn('Patched requests: SSL verification disabled!')
    

    Never use in production!

    0 讨论(0)
  • 2020-11-22 03:46

    After hours of debugging I could only get this to work using the following packages:

    requests[security]==2.7.0  # not 2.18.1
    cryptography==1.9  # not 2.0
    

    using OpenSSL 1.0.2g 1 Mar 2016

    Without these packages verify=False was not working.

    I hope this helps someone.

    0 讨论(0)
提交回复
热议问题