An example for the net

Python comes with a range of modules to support Internet protocols and data structures.

Here is a simple example that logs into a list of FTP servers and gets listings of their public directories:

#!/usr/bin/env python
# -*- python -*-
#
#      Name: ftpdir.py
#   Purpose: Get a listing of /pub on some FTP servers
#
#    Author: Graeme Cross <gcross@netspace.net.au>
# Copyright: Graeme Cross
#   Created: 26 June 2000
#       RCS: $Id: ftp.html,v 1.1 2000/07/09 23:18:39 mib Exp $
#

from ftplib import FTP

sitesList = ('ftp.monash.edu.au', 'mirror.aarnet.edu.au')

for site in sitesList:
	ftp = FTP(site)
	ftp.login('anonymous', 'gcross@netspace.net.au')
	ftp.cwd('/pub/')
	ftp.retrlines('LIST')
	ftp.quit()

# eof

Notes:

  1. This script would be a lot more versatile if the sites were specified on the command line
  2. There is no error checking in this script
  3. This is an example of OOP in Python - ftplib.FTP is a class

[Previous] [Next]