>>> import requests
>>> requests.get("https://api.example.com/index.php")
<Response [200]>
>>> requests.get("https://sandbox.example.com/index.php")
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 55, in get
return request('get', url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 383, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 486, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 385, in send
raise SSLError(e)
SSLError: hostname 'sandbox.example.com' doesn't match either of 'example.com', 'account.example.com', 'api.example.com', 'secure.example s.com'
为什么https://sandbox.example.com/index.php
和https://api.example.com/index.php both
在浏览器上正常运行时会出现此错误?
答案 0 :(得分:2)
Python3的SSL模块包括对SNI的本机支持。此支持尚未移植到Python2。远程服务器可能依赖于SNI来为您提供正确的证书,从而导致错误。
有关在Python2上使用SNI和请求的信息,请参阅此Stack Overflow answer。