我在用
import requests
requests.post(url='https://foo.com', data={'bar':'baz'})
但我得到一个 request.exceptions.SSLError。该网站有一个过期的证书,但我没有发送敏感数据,所以对我来说没关系。我想我可以使用像“verifiy=False”这样的论点,但我似乎找不到它。
我在用
import requests
requests.post(url='https://foo.com', data={'bar':'baz'})
但我得到一个 request.exceptions.SSLError。该网站有一个过期的证书,但我没有发送敏感数据,所以对我来说没关系。我想我可以使用像“verifiy=False”这样的论点,但我似乎找不到它。
从文档中:
requests
如果设置verify
为 False,也可以忽略验证 SSL 证书。>>> requests.get('https://kennethreitz.com', verify=False) <Response [200]>
如果您正在使用第三方模块并想要禁用检查,这里有一个上下文管理器,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')
请注意,一旦您离开上下文管理器,此代码将关闭处理修补请求的所有打开的适配器。这是因为 requests 维护一个每个会话的连接池,并且每个连接只发生一次证书验证,所以会发生这样的意外情况:
>>> 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)
<Response [200]>
>>> 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)
<Response [200]>
使用requests.packages.urllib3.disable_warnings()
和方法verify=False
。requests
import requests
from urllib3.exceptions import InsecureRequestWarning
# Suppress only the single warning from urllib3 needed.
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
# Set `verify=False` on `requests.post`.
requests.post(url='https://example.com', data={'bar':'baz'}, verify=False)
要添加到Blender 的答案,您可以使用禁用所有请求的 SSL 证书验证Session.verify = False
import requests
session = requests.Session()
session.verify = False
session.post(url='https://example.com', data={'bar':'baz'})
请注意urllib3
,(Requests 使用)强烈反对发出未经验证的 HTTPS 请求,并且会引发InsecureRequestWarning
.
也可以从环境变量中完成:
export CURL_CA_BUNDLE=""
如果您想使用 verify=False 选项准确发送 post 请求,最快的方法是使用此代码:
import requests
requests.api.request('post', url, data={'bar':'baz'}, json=None, verify=False)
如果您正在编写一个爬虫并且真的不关心 SSL 证书,您可以将其设置为全局:
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
不要在生产中使用
verify=False
Bug由于在设置
环境变量 ( )时session.verify=False
导致urllib*
忽略的错误。所以我们将其设置为空。CURL_CA_BUNDLE
import requests, os
session = requests.Session()
session.verify = False
session.trust_env = False
os.environ['CURL_CA_BUNDLE']="" # or whaever other is interfering with
session.post(url='https://example.com', data={'bar':'baz'})
不确定我需要trust_env