4.3. The GetURL Utility—A Web Resource Retriever in Python

On machines with Internet or intranet connections, it is useful to be able to point the xmln and xmlv utilities at arbitrary URLs. Python has excellent support for Internet protocols. The following simple program retrieves the contents of an arbitrary URL and outputs its contents on standard output.

CD-ROM reference=4035.txt
C>type geturl.py

"""
Simple utility to retrieve a URL and print
its contents to standard output.
"""
import sys
import os
from urllib   import urlretrieve, urlcleanup

def geturl(url):
      try:
            filename,headers = urlretrieve(url)
            print open(filename,"r").read()
      finally:
            urlcleanup()

if __name__ == "__main__":
      geturl(sys.argv[1])

Here is an example of the geturl utility ...

Get XML Processing with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.