- Python Network Programming - Home
- Python Network Introduction
- Python Networking - Environment Setup
- Python Networking - Internet Protocol
- Python Networking - IP Address
- Python Networking - DNS Lookup
- Python Networking - Routing
- Python Networking - HTTP Requests
- Python Networking - HTTP Response
- Python Networking - HTTP Headers
- Python Networking - Custom HTTP Requests
- Python Networking - Request Status Codes
- Python Networking - HTTP Authentication
- Python Networking - HTTP Data Download
- Python Networking - Connection Re-use
- Python Networking - Network Interface
- Python Networking - Sockets Programming
- Python Networking - HTTP Client
- Python Networking - HTTP Server
- Python Networking - Building URLs
- Python Networking - WebForm Submission
- Python Networking - Databases and SQL
- Python Networking - Telnet
- Python Networking - Email Messages
- Python Networking - SMTP
- Python Networking - POP3
- Python Networking - IMAP
- Python Networking - SSH
- Python Networking - FTP
- Python Networking - SFTP
- Python Networking - Web Servers
- Python Networking - Uploading Data
- Python Networking - Proxy Server
- Python Networking - Directory Listing
- Python Networking - Remote Procedure Call
- Python Networking - RPC JSON Server
- Python Networking - Google Maps
- Python Networking - RSS Feed
Python Network Programming Resources
Python Network - 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.
pip3 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.
main.py
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 −
main.py
import urllib3
http = urllib3.PoolManager()
response = http.request('GET','https://www.tutorialspoint.com/python')
print(response.status)
print(response.data.decode("utf-8"))
Output
When we run the above program, we get the following output −
200 <!DOCTYPE html> <html lang="en-US"> <head> <title>Python Tutorial</title> <meta charset="utf-8"> ...