python证书有什么用_如何在Python请求中禁用安全证书检查

从文档中:

如果您设置verify为False,则请求也可以忽略验证SSL证书 。

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

如果您使用的是第三方模块,并且想禁用检查功能,则可以使用上下文管理器来修补requests和更改它,从而使它verify=False成为默认设置并禁止显示警告。

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.com/')

print('It works')

requests.get('https://wrong.host.badssl.com/', verify=True)

print('Even if you try to force it to')

requests.get('https://wrong.host.badssl.com/', verify=False)

print('It resets back')

session = requests.Session()

session.verify = True

with no_ssl_verification():

session.get('https://wrong.host.badssl.com/', verify=True)

print('Works even here')

try:

requests.get('https://wrong.host.badssl.com/')

except requests.exceptions.SSLError:

print('It breaks')

try:

session.get('https://wrong.host.badssl.com/')

except requests.exceptions.SSLError:

print('It breaks here again')

请注意,一旦您离开上下文管理器,此代码将关闭处理已打补丁请求的所有打开的适配器。这是因为请求维护每个会话的连接池,并且证书验证仅对每个连接执行一次,因此将发生以下意外情况:

>>> import requests

>>> session = requests.Session()

>>> session.get('https://wrong.host.badssl.com/', 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)

>>> session.get('https://wrong.host.badssl.com/', 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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值