问题描述
我正在使用
import requests
requests.post(url='https://foo.com', data={'bar':'baz'})
但我收到了request.exceptions .SSLError。
该网站有一个过期的证书,但我没有发送敏感数据,所以对我来说无关紧要。
我会想象有一个像'verifiy = False'这样的论点我可以使用,但我似乎无法找到它。
but I get a request.exceptions.SSLError.The website has an expired certficate, but I am not sending sensitive data, so it doesn't matter to me.I would imagine there is an argument like 'verifiy=False' that I could use, but I can't seem to find it.
推荐答案
来自:
>>> requests.get('https://kennethreitz.com', verify=False)
<Response [200]>
如果您正在使用第三方模块并希望禁用检查,这是一个上下文补丁请求
的上下文管理器,并对其进行更改,以便 verify = False
是默认值,抑制警告。
If you're using a third-party module and want to disable the checks, here's a context manager that monkey patches requests
and changes it so that verify=False
is the default and suppresses the warning.
import warnings
import requests
import contextlib
try:
from functools import partialmethod
except ImportError:
# Python 2 fallback: https://gist.github.com/carymrobbins/8940382
from functools import partial
class partialmethod(partial):
def __get__(self, instance, owner):
if instance is None:
return self
return partial(self.func, instance, *(self.args or ()), **(self.keywords or {}))
@contextlib.contextmanager
def no_ssl_verification():
old_request = requests.Session.request
requests.Session.request = partialmethod(old_request, verify=False)
warnings.filterwarnings('ignore', 'Unverified HTTPS request')
yield
warnings.resetwarnings()
requests.Session.request = old_request
以及如何使用它的示例:
And an example of how to use it:
with no_ssl_verification():
requests.get('https://93.184.216.3/')
print('It works')
try:
requests.get('https://93.184.216.3/')
except requests.exceptions.SSLError:
print('It broke')
这篇关于如何在Python请求中禁用安全证书检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!