Python - HTTP Data Download


Advertisements

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):
    try:
        ftp.retrbinary("RETR " + filename ,open(filename, 'wb').write)
    except:
        print "Error"
 
 
ftp = ftplib.FTP("ftp.nluug.nl")
ftp.login("anonymous", "ftplib-example-1")
 
ftp.cwd('/pub/')          change directory to /pub/
getFile(ftp,'README.nluug')
 
ftp.quit()

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('http://www.tutorialspoint.com/python')
html = response.read(200)
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]-->
<head>
<!-- Basic -->
<meta charset="ut

Useful Video Courses


Video

Python Online Training

Most Popular

187 Lectures 17.5 hours

Malhar Lathkar

Video

Python Essentials Online Training

55 Lectures 8 hours

Arnab Chakraborty

Video

Learn Python Programming in 100 Easy Steps

136 Lectures 11 hours

In28Minutes Official

Video

Python with Data Science

75 Lectures 13 hours

Eduonix Learning Solutions

Video

Python 3 from scratch to become a developer in demand

70 Lectures 8.5 hours

Lets Kode It

Video

Python Data Science basics with Numpy, Pandas and Matplotlib

63 Lectures 6 hours

Abhilash Nelson

Advertisements