File: class/Workbook/Exercises/Lab10/sousa_fetch.py

#!/usr/local/bin/python
############################################### 
# Usage: % python sousa_fetch.py
# Fetch (only) the Monty Python theme song.
# This only fetches and saves the audio file,
# so it works anywhere you have an Internet 
# connection.  I've even used it from a DOS 
# box command line on Windows98, by dialing 
# in to an ISP via modem--it automatically 
# dials out if necessary; arguably amazing.
# On Windows, you may be able to pass the
# filename on a browser command line instead
# of piping it in to stdin; attempted here 
# for windows, but this scheme is portable.
###############################################
 
import os, sys
from ftplib import FTP             # socket-based ftp tools
from posixpath import exists       # file existence test
sample = 'sousa.au'

# ftp the audio file
if exists(sample):
    print sample, 'already fetched'
else:
    print 'Downloading.'
    theme = open(sample, 'w')
    ftp = FTP('ftp.python.org')       # connect to ftp site
    ftp.login()                       # use anonymous login
    ftp.cwd('pub/python/misc')
    ftp.retrbinary('RETR ' + sample, theme.write, 1024)
    ftp.quit()
    theme.close()
    print 'Done.'

if sys.platform[:3] == 'win':
    cwd = os.getcwd()
    brw = r'c:\"Program Files"\Netscape\Communicator\Program\netscape.exe'
    os.system('%s file://%s/%s' % (brw, cwd, sample))



[Home page] Books Code Blog Python Author Train Find ©M.Lutz