Open In App

Download a file over HTTP in Python

Last Updated : 04 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to install a File from HTTP using Python.

For this we will use the following methods:

  • Using Request
  • Using urllib
  • Using wget

Using requests

Here we send an HTTP request to the server and save the HTTP response in a response object and received content as a .png file in binary format.

Python3




# imported the requests library
import requests
image_url = "https://media.geeksforgeeks.org/wp-content/cdn-uploads/
                            20200623173636/Python-Tutorial1.png"
  
r = requests.get(image_url)
  
with open("python_logo.png",'wb') as f:
    f.write(r.content)
    print("Downloaded")


Output:

Downloaded

Using urllib

Here we will use urlretrieve() method to return the object of response object.

Python3




import urllib.request
image_url = "https://media.geeksforgeeks.org/wp-content/
                            cdn-uploads/20200623173636/Python-Tutorial1.png"
  
urllib.request.urlretrieve(image_url,"image.png")


Output:

('image.png', <http.client.HTTPMessage at 0x289dc10ed08>)

Using wget

Here we will use download() methods from wget to return the object from response. If the below code gives Importerror then you can import the wget module using the pip command.

Python3




import wget
  
image_url = "https://media.geeksforgeeks.org/wp-content/
                cdn-uploads/20200623173636/Python-Tutorial1.png"
  
wget.download(image_url)


Output:

'Python-Tutorial1.png'


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads