C:\...\PP4E\Internet\Other> http-getfile-urllib2.py http://www.python.org /index.html
http://www.python.org/index.html index.html
b'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3....'
b'\n'
b'\n'
b'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\n'
b'\n'
b'<head>\n'
Because this version uses a urllib.request interface that automatically saves the down-
loaded data in a local file, it’s similar to FTP downloads in spirit. But this script must
also somehow come up with a local filename for storing the data. You can either let the
script strip and use the base filename from the constructed URL, or explicitly pass a
local filename as a last command-line argument. In the prior run, for instance, the
downloaded web page is stored in the local file index.html in the current working
directory—the base filename stripped from the URL (the script prints the URL and
local filename as its first output line). In the next run, the local filename is passed
explicitly as py-index.html:
C:\...\PP4E\Internet\Other> http-getfile-urllib2.py
http://www.python.org /index.html py-index.html
http://www.python.org/index.html py-index.html
b'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3....'
b'\n'
b'\n'
b'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\n'
b'\n'
b'<head>\n'
C:\...\PP4E\Internet\Other> http-getfile-urllib2.py http://www.rmi.net /~lutz books.html
http://www.rmi.net/~lutz books.html
b'<HTML>\n'
b'\n'
b'<HEAD>\n'
b"<TITLE>Mark Lutz's Book Support Site</TITLE>\n"
b'</HEAD>\n'
b'<BODY BGCOLOR="#f1f1ff">\n'
C:\...\PP4E\Internet\Other> http-getfile-urllib2.py http://www.rmi.net /~lutz/about-pp.html
http://www.rmi.net/~lutz/about-pp.html about-pp.html
b'<HTML>\n'
b'\n'
b'<HEAD>\n'
b'<TITLE>About "Programming Python"</TITLE>\n'
b'</HEAD>\n'
b'\n'
Invoking programs and escaping text
The next listing shows this script being used to trigger a remote program. As before, if
you don’t give the local filename explicitly, the script strips the base filename out of
the filename argument. That’s not always easy or appropriate for program
1000 | Chapter 13: Client-Side Scripting