urllib HTTPS 请求:<urlopen 错误未知 url 类型:https>

新手上路,请多包涵

我在 python3.4 上有一个脚本,它一直很好,直到我从中下载文件的网站决定使用 https,现在我收到错误,但不知道如何检索文件。

我的脚本导入以下库并使用 urlretrive 获取先前的文件。因为它现在通过 302 重定向转发到 https。我收到一些错误。

 import urllib
import urllib.request

urllib.request.urlretrieve("http://wordpress.org/latest.tar.gz", "/thefile.gz")

我的错误:-

 Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/urllib/request.py", line 178, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "/usr/local/lib/python3.4/urllib/request.py", line 153, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/local/lib/python3.4/urllib/request.py", line 461, in open
    response = meth(req, response)
  File "/usr/local/lib/python3.4/urllib/request.py", line 571, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/local/lib/python3.4/urllib/request.py", line 493, in error
    result = self._call_chain(*args)
  File "/usr/local/lib/python3.4/urllib/request.py", line 433, in _call_chain
    result = func(*args)
  File "/usr/local/lib/python3.4/urllib/request.py", line 676, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "/usr/local/lib/python3.4/urllib/request.py", line 455, in open
    response = self._open(req, data)
  File "/usr/local/lib/python3.4/urllib/request.py", line 478, in _open
    'unknown_open', req)
  File "/usr/local/lib/python3.4/urllib/request.py", line 433, in _call_chain
    result = func(*args)
  File "/usr/local/lib/python3.4/urllib/request.py", line 1257, in unknown_open
    raise URLError('unknown url type: %s' % type)
urllib.error.URLError: <urlopen error unknown url type: https>

原文由 Adi Wong 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.5k
2 个回答

您的 Python 安装或操作系统很可能已损坏。

Python 仅在编译时支持 HTTPS 时才支持 HTTPS。但是,这应该是所有正常安装的默认设置。

 HTTPS support is only available if the socket module was compiled with SSL support.

https://docs.python.org/3/library/http.client.html

请说明您是如何安装 Python 的。 Python.org 提供官方 Python 发行版

原文由 Mikko Ohtamaa 发布,翻译遵循 CC BY-SA 3.0 许可协议

我遇到了类似的问题。

我使用了 anaconda,我只是将这两个文件移动到 anaconda3\DLLs 并且它工作了。

  • libcrypto-1_1-x64.dll
  • libssl-1_1-x64.dll

我不知道为什么。

我知道这是因为我试图使用 ssl 模块来忽略证书错误。

 import ssl
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

在这些行之后,https 错误消失并给了我另一个错误:

DLL load failed while importing _ssl: The specified module could not be found.

所以我去了 这篇 文章,找到了这两个重要文件。

ps: ssl 模块。

原文由 ChrisQIU 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题