python - 从 opentdb 请求时出现 SSLError

标签 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 出了什么问题。

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)

更奇怪的事情

repl.it中运行相同的代码时,一切正常,没有问题。是不是我电脑的原因导致这个不能用?我不知道为什么它可以在 repl.it 上运行,但不能在我自己的计算机上运行。

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

import requests

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

更多信息:

使用python 3.4.4

有关请求的信息:

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

最佳答案

取自this issue ,解决方案是安装 pyopenssl ,但我显然没有。运行

pip3 install --user pyopenssl

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

关于python - 从 opentdb 请求时出现 SSLError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49848395/

相关文章:

python - 从API获取的字符串中的奇怪字符无法解码

python - r 中的简单 download.file() 不适用于 requests.get

python - 来自 StackOverflow 公司的网络抓取公司描述

python - 对 pandas 中的多个 .csv 文件应用相同的操作

python - 如何在Python中拥有数组的数组

python - 用 Conv1D 替换密集自动编码器中的一层

python - 如何在保留 '\' 的同时替换 '\n'?

python - 如何获得在Python 3.x中记录的音频的频率和幅度?

python - SSL 请求失败 - Python OSX

python - fastapi 主体在两个函数之间表现不同