Python SSL certificate verify error

前端 未结 2 799
谎友^
谎友^ 2020-12-03 08:43

I\'m using requests to access a RESTful API. Everything seems to work. I can authenticate, pull back a session token and even unit test the methods in my class I wrote for

相关标签:
2条回答
  • 2020-12-03 08:59

    So the issue might have three resolutions as I see it:

    1. A certificate is OK and there is something wrong with the code. The issue may occur, for example, while using prepared requests as described in this solution

      But I don't really think it is your case because in the snippet you've provided no such methods are used. For the two next variants, you'll need to get the URL that causes an error and explore it's certificate (can be done via browser).

    2. A certificate is OK but a certificate authority that signed it is not included in CA list that is utilized by requests library. After you'll open a troubling URL, check CA in it and see if it's dates are valid and it is included in this list. If not, add CA in the trusted list for the requests library -- as explained in the answers to this StackOverflow question.

    3. A certificate is not valid or self-singed. Same solution as in 2.

    The general solution is to wrap your script in the try except clause and to print out all the URLs that will result in mistakes. Then try to request them one by one via requests library and see if the issue occurs. If it does, it's (2) or (3) case. If not — try to run script on another machine with fresh installed python and requests. If the run will be successful — then there's some issue in your configuration.

    0 讨论(0)
  • 2020-12-03 08:59

    I have found this over here

    I found this solution, insert this code at the beginning of your source file:

    import ssl
    
    try:
        _create_unverified_https_context = ssl._create_unverified_context
    except AttributeError:
        # Legacy Python that doesn't verify HTTPS certificates by default
        pass
    else:
        # Handle target environment that doesn't support HTTPS verification
        ssl._create_default_https_context = _create_unverified_https_context
    
    0 讨论(0)
提交回复
热议问题