python - requests.exceptions.SSLError : ("bad handshake: Error([(' SSL routines', 'SSL23_GET_SERVER_HELLO' , 'sslv3 alert handshake failure' )],),)

标签 python macos ssl curl

我做到了

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"}}

最佳答案

您的代码非常适合我使用 Python 2.7.12。你没有给出你正在使用的 Python 的确切版本,但我的猜测是它是 2.7.9 之前的版本。在 2.7.9 中完成了对 SSL 实现的几处更改,包括添加对 Server Name Indication (SNI) 的支持。 .您的目标 URL 需要在客户端支持 SNI,这在使用 SSLLabs 测试站点时可以看出。 :

This site works only in browsers with SNI support.

您显示的跟踪对于不支持 SNI 的客户端来说是典型的。 来自FAQ for requests :

Python3 and Python 2.7.9+ include native support for SNI in their SSL modules. For information on using SNI with Requests on Python < 2.7.9 refer to this Stack Overflow answer.

关于python - requests.exceptions.SSLError : ("bad handshake: Error([(' SSL routines', 'SSL23_GET_SERVER_HELLO' , 'sslv3 alert handshake failure' )],),),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43969408/

相关文章:

python - 正则表达式与 Python 中的\t\n\r\f\v 不匹配

python - 是否可以在selenium webdriver python中获取iframe的内容?

ssl - 在平衡的集群 IS 实例上应该为哪个主机颁发 SSL 私钥?

node.js - NodeJS SSL 从哪里获取 .key 文件

python - 使用 python 解压缩 zip 文件夹而不更改目录

python - 创建原子集列表

macos - 为什么我的 MacOS 应用程序在打瞌睡,尽管我指示它不要打瞌睡

objective-c - Mac 服务无法运行

c++ - 在 mac os x 上安装使用 cmake 构建的库

python - Python 中的 SSLSocket 口令/密码