我做了
import requests
url = "https://randomuser.me/api/"
data = requests.get(url).json()
print data
我一直在
Traceback (most recent call last):
File "/Applications/MAMP/htdocs/code/python/curl.py", line 4, in <module>
data = requests.get(url).json()
File "/Library/Python/2.7/site-packages/requests/api.py", line 70, in get
return request('get', url, params=params, **kwargs)
File "/Library/Python/2.7/site-packages/requests/api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 475, in request
resp = self.send(prep, **send_kwargs)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 596, in send
r = adapter.send(request, **kwargs)
File "/Library/Python/2.7/site-packages/requests/adapters.py", line 497, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: ("bad handshake: Error([('SSL routines', 'SSL23_GET_SERVER_HELLO', 'sslv3 alert handshake failure')],)",)
但如果我运行 curl https://randomuser.me/api/
{"results":[{"gender":"male","name":{"title":"mr","first":"joscha","last":"maier"},"location":{"street":"7686 parkstraße","city":"kelheim","state":"hessen","postcode":46836},"email":"joscha.maier@example.com","login":{"username":"bluefrog937","password":"2003","salt":"xcZH4xOX","md5":"9fc3a4c2c8999066149d322ab4948669","sha1":"1c392831338c7b5715bd19a4c186111df32a6d04","sha256":"a21b5afa260d8d6a121d6f2dc688fb2f2b41e4f0b5c823daea76f968338653fb"},"dob":"1994-10-26 06:51:40","registered":"2009-05-25 10:37:41","phone":"0509-5347389","cell":"0174-6616884","id":{"name":"","value":null},"picture":{"large":"https://randomuser.me/api/portraits/men/26.jpg","medium":"https://randomuser.me/api/portraits/med/men/26.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/men/26.jpg"},"nat":"DE"}],"info":{"seed":"f74c00ae752dccac","results":1,"page":1,"version":"1.1"}}
答案 0 :(得分:1)
使用Python 2.7.12,您的代码非常适合我。您没有提供您正在使用的Python的确切版本,但我的猜测是它是2.7.9之前的版本。在2.7.9中对SSL实现进行了若干更改,包括添加对Server Name Indication (SNI)的支持。您的目标网址需要支持客户端中的SNI,这可以在使用SSLLabs测试网站时看到:
此网站仅适用于支持SNI的浏览器。
您显示的跟踪对于不支持SNI的客户来说是典型的。 来自FAQ for requests:
Python3和Python 2.7.9+在其SSL模块中包含对SNI的本机支持。有关在Python上使用SNI和请求的信息&lt; 2.7.9参考此Stack Overflow answer。