从opentdb请求时SSLError

时间:2018-04-16 01:13:31

标签: python python-3.x python-requests

问题

opentdb请求时,我收到以下错误:

Traceback (most recent call last):
  File "trivia.py", line 3, in <module>
    print(requests.get('https://opentdb.com/api.php?amount=1'))
  File "/Users/edl/Library/Python/3.4/lib/python/site-packages/requests/api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
  File "/Users/edl/Library/Python/3.4/lib/python/site-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/Users/edl/Library/Python/3.4/lib/python/site-packages/requests/sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/edl/Library/Python/3.4/lib/python/site-packages/requests/sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "/Users/edl/Library/Python/3.4/lib/python/site-packages/requests/adapters.py", line 506, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='opentdb.com', port=443): Max retries exceeded with url: /api.php?amount=1 (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:600)'),))

我查看了this question以及issues page on github,但没有一个解决方案正在运作。来自其他网站的请求在我的计算机上完全正常,所以我不确定是什么问题。

更奇怪

但是,在js中运行此代码不会出错,所以我不确定requests有什么问题。

&#13;
&#13;
function get(url) {
  return new Promise((resolve, reject) => {
    const req = new XMLHttpRequest();
    req.open("GET", url);
    req.onload = () => req.status === 200 ? resolve(req.response) : reject(req.statusText);
    req.onerror = () => reject("Network Error");
    req.send();
  });
}

get('https://opentdb.com/api.php?amount=1').then(console.log)
&#13;
&#13;
&#13;

甚至更加奇怪

repl.it中运行相同的代码时,一切都没有问题。是因为我的电脑,这不起作用吗?我不确定为什么它可以从repl.it而不是我自己的电脑。

最小,完整且可验证的示例:

import requests

print(requests.get('https://opentdb.com/api.php?amount=1'))

更多信息:

使用python 3.4.4

有关requests的信息:

Name: requests
Version: 2.18.4
Summary: Python HTTP for Humans.
Home-page: http://python-requests.org
Author: Kenneth Reitz
Author-email: me@kennethreitz.org
License: Apache 2.0
Location: /Users/edl/Library/Python/3.4/lib/python/site-packages
Requires: urllib3, certifi, idna, chardet

1 个答案:

答案 0 :(得分:1)

this issue开始,解决方案是安装pyopenssl,我显然没有。运行

pip3 install --user pyopenssl

修正了所有错误。仍然不确定为什么会这样。如果有人能够解释为什么pyopenssl修复它,那就太好了。谢谢!