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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28376506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:10:54  来源:igfitidea点击:

urllib HTTPS request: <urlopen error unknown url type: https>

pythonpython-3.4

提问by Adi Wong

I have a script on python3.4 and it has been fine until the website I download the file from decides to use https and now I am getting error but can't figure out how I can retrive the file.

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

My script import the following library and uses the urlretrive to get the file previously. Since it is now forwarded to https with 302 redirection. I am getting some error.

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

import urllib
import urllib.request

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

My error:-

我的错误:-

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>

采纳答案by Mikko Ohtamaa

Most likely your Python installation or operating system is broken.

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

Python has only support for HTTPS if it was compiled with HTTPS support. However, this should be the default for all sane installations.

如果 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

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

Please clarify how you installed Python. Official Python distributions are available at python.org

请说明您是如何安装 Python 的。官方 Python 发行版可在 python.org 获得

回答by Farshad Hasanpour

I had the same problem with Anaconda but after installing the OpenSSL package, it works well.

我在使用 Anaconda 时遇到了同样的问题,但在安装 OpenSSL 软件包后,它运行良好。

conda install -c anaconda openssl

回答by moyo

Had this issue, and it was solved by upgrading Python with

有这个问题,它是通过升级 Python 解决的

brew upgrade python

回答by Khalid

for HTTP
people who encountered the error ValueError: unknown url type: 'httpor ValueError: unknown url type: b'http

对于
遇到错误的HTTP人员 ValueError: unknown url type: 'httpValueError: unknown url type: b'http

while opening some url like below with urllib.request.Request'http://localhost/simple_form/insert.php'

同时打开一些像下面这样的网址 urllib.request.Request'http://localhost/simple_form/insert.php'

just change localhostto 127.0.0.1

只需更改localhost127.0.0.1

looks like Request method is looking for a domain.somethingin url

看起来 Request 方法正在寻找一个domain.somethingin url