
- Python - Network Programming
- Python - Network Introduction
- Python - Network Environment
- Python - Internet Protocol
- Python - IP Address
- Python - DNS Lookup
- Python - Routing
- Python - HTTP Requests
- Python - HTTP Response
- Python - HTTP Headers
- Python - Custom HTTP Requests
- Python - Request Status Codes
- Python - HTTP Authentication
- Python - HTTP Data Download
- Python - Connection Re-use
- Python - Network Interface
- Python - Sockets Programming
- Python - HTTP Client
- Python - HTTP Server
- Python - Building URLs
- Python - WebForm Submission
- Python - Databases and SQL
- Python - Telnet
- Python - Email Messages
- Python - SMTP
- Python - POP3
- Python - IMAP
- Python - SSH
- Python - FTP
- Python - SFTP
- Python - Web Servers
- Python - Uploading Data
- Python - Proxy Server
- Python - Directory Listing
- Python - Remote Procedure Call
- Python - RPC JSON Server
- Python - Google Maps
- Python - RSS Feed
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): 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