python requests 库不进行 ssl 签名验证 verify False

创建日期: 2024-08-09 14:38 | 作者: 风波 | 浏览次数: 19 | 分类: Python

参考:https://stackoverflow.com/questions/15445981/how-do-i-disable-the-security-certificate-check-in-python-requests

方法一:直接传入参数 verify=False

使用python 库访问 https 网站的时候,需要验证网站的 ssl 证书。如果不想进行 ssl 证书验证,可以增加参数 verify=False

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

方法二:使用 contextli

文章中还介绍了使用 contextlib 进行设置的方式,但是看不太明白

import warnings
import contextlib

import requests
from urllib3.exceptions import InsecureRequestWarning

old_merge_environment_settings = requests.Session.merge_environment_settings

@contextlib.contextmanager
def no_ssl_verification():
    opened_adapters = set()

    def merge_environment_settings(self, url, proxies, stream, verify, cert):
        # Verification happens only once per connection so we need to close
        # all the opened adapters once we're done. Otherwise, the effects of
        # verify=False persist beyond the end of this context manager.
        opened_adapters.add(self.get_adapter(url))

        settings = old_merge_environment_settings(self, url, proxies, stream, verify, cert)
        settings['verify'] = False

        return settings

    requests.Session.merge_environment_settings = merge_environment_settings

    try:
        with warnings.catch_warnings():
            warnings.simplefilter('ignore', InsecureRequestWarning)
            yield
    finally:
        requests.Session.merge_environment_settings = old_merge_environment_settings

        for adapter in opened_adapters:
            try:
                adapter.close()
            except:
                pass

使用方式

with no_ssl_verification():
    requests.get('https://wrong.host.badssl.example/')
    print('It works')

    requests.get('https://wrong.host.badssl.example/', verify=True)
    print('Even if you try to force it to')

requests.get('https://wrong.host.badssl.example/', verify=False)
print('It resets back')

session = requests.Session()
session.verify = True

with no_ssl_verification():
    session.get('https://wrong.host.badssl.example/', verify=True)
    print('Works even here')

try:
    requests.get('https://wrong.host.badssl.example/')
except requests.exceptions.SSLError:
    print('It breaks')

try:
    session.get('https://wrong.host.badssl.example/')
except requests.exceptions.SSLError:
    print('It breaks here again')

方法三:

>>> import requests
>>> session = requests.Session()
>>> session.get('https://wrong.host.badssl.example/', verify=False)
/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py:857: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning)
<Response [200]>
>>> session.get('https://wrong.host.badssl.example/', verify=True)
/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py:857: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning)
<Response [200]>

方法四:urllib3

来源:https://stackoverflow.com/questions/18061640/ignore-certificate-validation-with-urllib3

import urllib3
c = urllib3.HTTPSConnectionPool('10.0.3.168', port=9001, cert_reqs='CERT_NONE',
                                assert_hostname=False)
c.request('GET', '/')

方法五

这儿有一个更复杂的方案,但是看不懂:https://sslinsights.com/fix-certificate-verify-failed-error-in-python/

方法六

Downgrading requests to 2.27.1 and adding the following code snippet to the beginning of your program file should fix the problem.

'''This following code will set the CURL_CA_BUNDLE environment variable to an empty string in the Python os module'''


import os
os.environ['CURL_CA_BUNDLE'] = ''
19 浏览
13 爬虫
0 评论