Download a file over HTTP in Python


In Python, we download a file over HTTP using Python built-in libraries like urllib, request, and httplib. HTTP is a hypertext transfer protocol and is used to access data over the world wide web.HTTP requests are typically initiated by a client, such as a web browser, and are sent to a server that hosts the requested resource. The request usually includes a method, such as GET or POST, a Uniform Resource Locator (URL) that identifies the resource, and optional headers that provide additional information about the request. In this article, we will understand how to use urllib and request libraries to download files.

Downloading a file using urllib

Urllib contains submodules like urllib.request which can be used to easily download a file from the internet. The urllib.request takes the URL of the file and the file name you want to give to the downloaded file as input.

Syntax

urllib.request.urlretrieve(URL, filename)

Here urllib.request.urlretrieve takes two arguments. One is the URL of the file over the internet and the other is the file name you want to give to the downloaded file.

Example

In the below example to download the file over HTTP using the urllib library first import the urllib.request module and then call the urlretrive function of the urllib.request module. Pass the URL of the file to download and the file name you want to keep after downloading the file.

If an invalid URL is passed or the file is not downloadable then it raises an exception.

import urllib.request
from PIL import Image

url = 'https://www.python.org/static/img/python-logo.png'
filename = 'python-logo.png'

urllib.request.urlretrieve(url, filename)

with open(filename, 'rb') as f:
   image = Image.open(f)
   image.show()

Output

Downloading files using the requests library

The requests library uses its get method to make HTTP requests in Python. It simply takes the URL of the file as input and makes a get request to download the file and returns the downloaded file as a response.

Syntax

requests.get(URL)

Here in requests.get() method URL is the URL of the file which is to be downloaded over the internet.

Example

In the below example, we import the requests library and specify the URL of the file we want to download and the name we want to give to the downloaded file. We then import the requests library and use the requests.get() method to download the Python logo. The method returns a response object that contains the content of the file. Finally, we read the downloaded file and print it on the screen.

import requests

url = 'https://www.python.org/static/img/python-logo.png'
filename = 'python-logo.png'

response = requests.get(url)

from PIL import Image

with open(filename, 'rb') as f:
   image = Image.open(f)
   image.show()

Output

Conclusion

In this article, we discussed how to download files over HTTP in Python using Python inbuilt libraries like urllib and requests library. The requests library provides a higher-level interface and is more user-friendly than urllib. The requests library provides a more simple way to download files than the urllib library. Any of the libraries can be used to download files in Python.

Updated on: 10-Jul-2023

306 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements