requests库请求报SSLError

requests库请求报SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[2019-04-18 14:14:46,594] ERROR in utils: Traceback (most recent call last):
File "/Users/higgs/PycharmProjects/workdir2/app_platform/utils.py", line 44, in fin
res['data'] = f(*args, **kw)
File "api.py", line 354, in prod_question
url = upload_prod(save_file, json.dumps(res))
File "/Users/higgs/PycharmProjects/workdir2/app_platform/utils.py", line 107, in upload_prod
_, info = put_data(token, key, data=f)
File "/Users/higgs/.pyenv/versions/app_platform/lib/python2.7/site-packages/qiniu/services/storage/uploader.py", line 31, in put_data
return _form_put(up_token, key, data, params, mime_type, crc, progress_handler, fname)
File "/Users/higgs/.pyenv/versions/app_platform/lib/python2.7/site-packages/qiniu/services/storage/uploader.py", line 81, in _form_put
url = config.get_default('default_zone').get_up_host_by_token(up_token) + '/'
File "/Users/higgs/.pyenv/versions/app_platform/lib/python2.7/site-packages/qiniu/zone.py", line 31, in get_up_host_by_token
up_hosts = self.get_up_host(ak, bucket)
File "/Users/higgs/.pyenv/versions/app_platform/lib/python2.7/site-packages/qiniu/zone.py", line 50, in get_up_host
bucket_hosts = self.get_bucket_hosts(ak, bucket)
File "/Users/higgs/.pyenv/versions/app_platform/lib/python2.7/site-packages/qiniu/zone.py", line 76, in get_bucket_hosts
hosts = compat.json.loads(self.bucket_hosts(ak, bucket))
File "/Users/higgs/.pyenv/versions/app_platform/lib/python2.7/site-packages/qiniu/zone.py", line 128, in bucket_hosts
ret = requests.get(url)
File "/Users/higgs/.pyenv/versions/app_platform/lib/python2.7/site-packages/requests/api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "/Users/higgs/.pyenv/versions/app_platform/lib/python2.7/site-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/Users/higgs/.pyenv/versions/app_platform/lib/python2.7/site-packages/requests/sessions.py", line 502, in request
resp = self.send(prep, **send_kwargs)
File "/Users/higgs/.pyenv/versions/app_platform/lib/python2.7/site-packages/requests/sessions.py", line 612, in send
r = adapter.send(request, **kwargs)
File "/Users/higgs/.pyenv/versions/app_platform/lib/python2.7/site-packages/requests/adapters.py", line 514, in send
raise SSLError(e, request=request)
SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)

127.0.0.1 - - [18/Apr/2019 14:14:46] "GET /questions/prod?channel=1000 HTTP/1.1" 200 160
  • 解决方法:

    1
    2
    pip uninstall -y certifi
    pip install certifi
  • 推测可能是本地证书过期了,所以ssl验证失败导致的;

  • 深挖一下发现certifi库也是requests项目组维护的,并且从requests项目中独立出来了,原文如下:
    Certifi CA Bundle
    Certifi is a carefully curated collection of Root Certificates for validating the trustworthiness of SSL certificates while verifying the identity of TLS hosts. It has been extracted from the Requests project.原文链接