XML technologies / XML / Python & XML / The Python Internet APIs / Opening URLs

(Excerpt from "Python & XML" by Christopher A. Jones & Fred L. Drake, Jr.)

A URL’s protocol component can be anything the processing software understands. The most common URL protocols, also called schemes, are probably HTTP, FTP, and FILE. HTTP connects to web services, FTP retrieves files, and FILE retrieves local files. All of this is easy with Python’s urllib module.

The function urllib.urlopen opens all kinds of URLs and returns a file-like object to work with. For a local file, simply use its filename. For example, use this syntax to open an XML document in the local directory:

>>> from urllib import urlopen          
>>> fd = urlopen("order.xml")
>>> print fd.read( )
<?xml version="1.0"?>
<!DOCTYPE order SYSTEM "order.dtd">
<order>
      <customer_name>eDonkey Enterprises</customer_name>
      <sku>343-3940938</sku>
      <unit_price>39.95</unit_price>
</order>

>>> fd.close( )

The urlopen function returns a file-like object, which can be treated as a file to access and display its content. When the file object is closed, urlopen also cleans up and closes the connection to the remote server or local file.

Using FTP

The function urlopen works with remote files as well as local files, provided you have an Internet connection. For example, a URL for an FTP server’s root directory may let you display its contents as follows:

>>> fd = urlopen("ftp://ftp.oreilly.com")
>>> print fd.read( )
total 64
drwxr-xr-x 3 61             512 Aug 29 2000 bin             
drwxr-xr-x 2 3              512 Aug 30 2000 dev
drwxr-xr-x 4 61             512 Oct 16 2000 etc
lrwxrwxrwx 1 1               12 Aug 31 2000 examples -> pub/examples
drwxrwx-wx 2 100            512 May 7 22:22 incoming
drwxrws--x 48 61          17408 May 6 04:00 intl
drwxr-xr-x 2 1              512 Sep 1 2000 lost+found
drwxrws--x 55 61           4608 May 7 22:22 outgoing
drwxrwsr-x 21 61            512 Mar 30 21:47 pub
drwxr-xr-x 2 61             512 Aug 31 2000 published
drwxr-sr-x 4 100            512 Apr 17 17:17 software
dr-xr-xr-x 5 61             512 Aug 30 2000 usr

>>> fd.close( )

Retrieving URL content

urlretrieve is similar to urlopen. It accepts an optional filename for saving the remote file locally and returns a tuple containing the filename and the actual data as a MIME message. For example:

>>> from urllib import urlretrieve
>>> ob = urlretrieve("ftp://ftp.oreilly.com", "menu.txt")
>>> ob
('menu.txt', <mimetools.Message instance at 007F382C>)

The first argument is the URL to connect to; the second is the name of the local file receiving the data.

One of the most interesting features of urlretrieve is its callback support. When retrieving a document, pass a callback method as an optional third parameter to receive progress notifications.

If you provide a callback, urlretrieve expects it to accept three arguments: the current block number, the block size, and the total file size. The following example shows a simple progress-reporting routine.

Example: retrieve.py

"""
retrieve.py
"""
from urllib import urlretrieve

def callback(blocknum, blocksize, totalsize):
    print "Geladen: " + str((blocknum * blocksize)),
    print " von ", totalsize

urlretrieve("http://www.example.com/pyxml.xml", "px.xml", callback)
print "Ladeoperation komplett"

Running the example displays:

C:\WINDOWS\Desktop\oreilly\pythonxml\c8>python retrieve.py
Geladen: 0 von 116063
Geladen: 8192 von 116063
Geladen: 16384 von 116063
Geladen: 24576 von 116063
Geladen: 32768 von 116063
Geladen: 40960 von 116063
Geladen: 49152 von 116063
Geladen: 57344 von 116063
Geladen: 65536 von 116063
Geladen: 73728 von 116063
Geladen: 81920 von 116063
Geladen: 90112 von 116063
Geladen: 98304 von 116063
Geladen: 106496 von 116063
Geladen: 114688 von 116063
Geladen: 122880 von 116063
Geladen: 131072 von 116063
Ladeoperation komplett

Callbacks are ideal for tracking FTP progress. They are also useful for monitoring long downloads or informing a frustrated waiting user of their status.

  

 

 

 

back to top