Python - HTTP Data Download


We can download data from a serer using python's module which handle ftp or File Transfer Protocol. We can also read the data and later save it to the local system.

We need to install the module ftplib to acheive this.

pip install ftplib

Fetching the Files

We can fetch a specific file by using the getfile method. This method moves a copy of the file from the remote system to the local system from where the ftp connection was initiated.

import ftplib
import sys
def getFile(ftp, filename):
        ftp.retrbinary("RETR " + filename ,open(filename, 'wb').write)
        print "Error"
ftp = ftplib.FTP("")
ftp.login("anonymous", "ftplib-example-1")
ftp.cwd('/pub/')          change directory to /pub/

When we run the above program, we find the file README.nlug to be present in the local system from where the connection was initiated.

Reading the Data

In the below example we use the module urllib2 to read the required portion of the data which we can copy and save it to local system.

When we run the above program, we get the following output −

import urllib2
response = urllib2.urlopen('')
html =
print html

When we run the above program, we get the following output −

<!DOCTYPE html>
<!--[if IE 8]><html class="ie ie8"> <![endif]-->
<!--[if IE 9]><html class="ie ie9"> <![endif]-->
<!--[if gt IE 9]><!-->  <html> <!--<![endif]-->
<!-- Basic -->
<meta charset="ut

Useful Video Courses


Python Online Training

Most Popular

187 Lectures 17.5 hours

Malhar Lathkar


Python Essentials Online Training

55 Lectures 8 hours

Arnab Chakraborty


Learn Python Programming in 100 Easy Steps

136 Lectures 11 hours

In28Minutes Official


Python with Data Science

75 Lectures 13 hours

Eduonix Learning Solutions


Python 3 from scratch to become a developer in demand

70 Lectures 8.5 hours

Lets Kode It


Python Data Science basics with Numpy, Pandas and Matplotlib

63 Lectures 6 hours

Abhilash Nelson