Forcing requests library to use TLSv1.1 or TLSv1.2 in Python

后端 未结 2 1476
借酒劲吻你
借酒劲吻你 2021-01-12 02:13

I am trying to send a POST call using requests library in python to a server. Earlier I was able to successfully send POST calls but recently, the server deprecated TLSv1.0

2条回答
  •  一生所求
    2021-01-12 02:57

    I was getting random connection errors from very old server (it's rated F by https://www.ssllabs.com) until I wasn't start using this code in my HTTPAdapter:

    def init_poolmanager(self, *args, **kwargs):
        ssl_context = ssl.create_default_context()
    
        # Sets up old and insecure TLSv1.
        ssl_context.options &= ~ssl.OP_NO_TLSv1_3 & ~ssl.OP_NO_TLSv1_2 & ~ssl.OP_NO_TLSv1_1
        ssl_context.minimum_version = ssl.TLSVersion.TLSv1
    
        # Also you could try to set ciphers manually as it was in my case.
        # On other ciphers their server was reset the connection with:
        # [Errno 104] Connection reset by peer
        # ssl_context.set_ciphers("ECDHE-RSA-AES256-SHA")
    
        # See urllib3.poolmanager.SSL_KEYWORDS for all available keys.
        kwargs["ssl_context"] = ssl_context
    
        return super().init_poolmanager(*args, **kwargs)
    

提交回复
热议问题