Python (Scripting) - Download File using urllib

by
Jeremy Canfield |
Updated: April 01 2024
| Python (Scripting) articles
There are a couple ways to go about downloading a file.
- using requests
- using paramiko Secure Copy Protocol (SCP)
- using urllib (this article)
urllib3 can be used to download a file. Use the pip list command to determine if the urllib3 package is installed.
~]$ pip list
Package Version
------------------- ---------
urllib3 1.26.8
If the urllib package is not listed, use the pip install command to install the urllib package.
pip install urllib3
Here is the minimal boilterplate code without error handling to download a file using urllib.
#!/usr/bin/python3
import urllib.request
src = f"https://storage.googleapis.com/kubernetes-release/release/v1.23.6/bin/linux/amd64/kubectl"
dest = f"/tmp/kubectl1.23.6"
urllib.request.urlretrieve(src, dest)
Here is a more practical example, with try/except/else error handling.
#!/usr/bin/python3
import urllib.request
src = f"https://storage.googleapis.com/kubernetes-release/release/v1.23.6/bin/linux/amd64/kubectl"
dest = f"/tmp/kubectl1.23.6"
try:
urllib.request.urlretrieve(src, dest)
except Exception as exception:
print(f"got the following exception when attempting to download {src} to {dest}: {exception}")
else:
print(f"successfully downloaded {src} to {dest}")
And here is an example of how you can include headers in the request.
#!/usr/bin/python3
import urllib.request
opener = urllib.request.build_opener()
opener.addheaders = [('Authorization', 'Token abc123')]
urllib.request.install_opener(opener)
src = f"https://raw.githubusercontent.com/JohnDoe/foo/main/bar/example.json"
dest = f"/tmp/example.txt"
try:
urllib.request.urlretrieve(src, dest)
except Exception as exception:
print(f"got the following exception when attempting to download {src} to {dest}: {exception}")
else:
print(f"successfully downloaded {src} to {dest}")
Did you find this article helpful?
If so, consider buying me a coffee over at