26

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.

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.

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>
martineau
  • 119,623
  • 25
  • 170
  • 301
Adi Wong
  • 383
  • 1
  • 4
  • 8
  • [urllib HTTPS request: ](https://stackoverflow.com/q/28376506/608639), [urllib cannot read https](https://stackoverflow.com/q/27208131/608639), [urllib.error.URLError: ](https://stackoverflow.com/q/27115803/608639), [urllib HTTPS request: ](https://stackoverflow.com/q/44750732/608639), etc. – jww Feb 08 '18 at 11:37
  • i had the same issue and resolved it by switching to python 3.6, running "pip install pyopenssl" and updating python interpreter to 3.6 both in global Pycharm settings in File>Settings>Project>Python Interpreter as well as in Edit Configurations under Run menu – alex Sep 27 '19 at 02:35

7 Answers7

22

Most likely your Python installation or operating system is broken.

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

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

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

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

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • 1
    I got this issue with `Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32` – ubershmekel Mar 30 '20 at 01:55
  • I got this solved by checking the (not recommended) option to add everything to the PATH. Seee https://stackoverflow.com/a/54929204/6391429. – Agent49 Sep 11 '21 at 10:43
  • Thanks for the hint of this root cause analysis. That points me to the right direction. It turns the python I used was compiled with a higher version of openssl than the one shipped in my OS (Termux 0.118 published on GitHub). Upgrading the openssl on my OS fixed the issue. – RayLuo Jul 10 '23 at 19:26
4

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

conda install -c anaconda openssl
4

I ran into the similar problem.
I used anaconda, and I simply moved these two file to anaconda3\DLLs and it worked.

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

I don't know why.
The reason I know this is that I tried to use ssl module to ignore certificate errors.

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

After these lines, the https error went away and gave me another error:
DLL load failed while importing _ssl: The specified module could not be found.

So I went to this post and found these two important files.

ps: the ssl module is not necessary.

ChrisQIU
  • 306
  • 2
  • 6
2

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

brew upgrade python
moyo
  • 1,312
  • 1
  • 13
  • 29
2

For me the error was resolved by downloading the correct version of openssl.

I was using python 3.7.5 32 bit version on windows 10 machine.

  1. goto https://slproweb.com/products/Win32OpenSSL.html and download Win32 OpenSSL v1.1.1h EXE | MSI 54MB Installer version . I used 32 bit as my python interpreter was 32 bit.

  2. Install and run.

Issue fixed :)

-1

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

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

just change localhost to 127.0.0.1

looks like Request method is looking for a domain.something in url

Khalid
  • 41
  • 1
-1

If your url has single or double quotes, i.e. '' or "", then it also gives this error.

Replace your single or double quotes with %20

url='http://stackoverflow.com/wizard.php?id="hah"'

should be changed to

url='http://stackoverflow.com/wizard.php?id=%20hah%20'
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135