Ignore certificate validation with urllib3

前端 未结 3 890
没有蜡笔的小新
没有蜡笔的小新 2020-12-06 07:14

I\'m using urllib3 against private services that have self signed certificates. Is there any way to have urllib3 ignore the certificate errors and make the request anyways?<

3条回答
  •  伪装坚强ぢ
    2020-12-06 07:51

    I found the answer to my problem. The urllib3 documentation does not, in fact, completely explain how to suppress SSL certificate validation. What is missing is a reference to ssl.CERT_NONE.

    My code has a boolean, ssl_verify, to indicate whether or not I want SSL validation. The code now looks like this:

    import ssl
    import urllib3
    
    #
    #
    #
        if (ssl_verify):
            cert_reqs = ssl.CERT_REQUIRED
        else:
            cert_reqs = ssl.CERT_NONE
            urllib3.disable_warnings()
    
        http = urllib3.PoolManager(cert_reqs = cert_reqs)
    
        auth_url = f'https://{fmc_ip}/api/fmc_platform/v1/auth/generatetoken'
        type = {'Content-Type': 'application/json'}
    
        auth = urllib3.make_headers(basic_auth=f'{username}:{password}')
        headers = { **type, **auth }
    
        resp = http.request('POST',
                        auth_url,
                        headers=headers,
                        timeout=10.0)
    

提交回复
热议问题