我在用
import requests
requests.post(url='https://foo.example', data={'bar':'baz'})
但是我得到一个request。exceptions。sslerror。 网站的证书过期了,但我没有发送敏感数据,所以这对我来说无关紧要。 我可以想象有一个像“verify =False”这样的参数,我可以使用,但我似乎找不到它。
我在用
import requests
requests.post(url='https://foo.example', data={'bar':'baz'})
但是我得到一个request。exceptions。sslerror。 网站的证书过期了,但我没有发送敏感数据,所以这对我来说无关紧要。 我可以想象有一个像“verify =False”这样的参数,我可以使用,但我似乎找不到它。
当前回答
如果你正在编写一个scraper并且真的不关心SSL证书,你可以将其设置为全局:
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
不用于生产
编辑,评论来自@Misty
不工作。Requests现在使用urllib3,它创建自己的urllib3 SSLContext。您可以重写cert_reqs
ssl.SSLContext.verify_mode = property(lambda self: ssl.CERT_NONE, lambda self, newval: None)
其他回答
从文档中可以看到:
请求也可以忽略验证SSL证书 验证为False。 >>> requests.get('https://kennethreitz.com', verify=False) <响应[200]>
如果您正在使用第三方模块并想要禁用检查,这里有一个上下文管理器,它可以猴子修补请求并更改它,以便将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.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]>
在请求方法上使用requests.packages.urllib3.disable_warnings()和verify=False。
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的答案中,您可以使用Session禁用所有请求的SSL证书验证。verify = False
import requests
session = requests.Session()
session.verify = False
session.post(url='https://example.com', data={'bar':'baz'})
请注意urllib3(请求使用的)强烈不鼓励发出未经验证的HTTPS请求,并将引发InsecureRequestWarning。
如果你想准确地发送带有verify=False选项的post请求,最快的方法是使用以下代码:
import requests
requests.api.request('post', url, data={'bar':'baz'}, json=None, verify=False)
python 3 + 6。
import warnings
warnings.filterwarnings("ignore", message="Unverified HTTPS request")