Opening URLs

The protocol portion of the URL can consist of anything that processing software can understand. Perhaps the most common URL protocols (also called schemes) are HTTP, FTP, and FILE. HTTP is used to connect to web servers, FTP is used to retrieve files, and FILE is used to retrieve a local file. All are easily accomplished using Python’s urllib module.

The urllib.urlopen function takes care of opening URLs of all kinds and can give you back a file-like object to work with. To retrieve a local file, just use the filename. For example, to open an XML document in the local directory, you can use the following syntax:

>>> 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. This object can then be treated as a file to retrieve and display its contents. When the file object is closed, urlopen cleans up its business as well, terminating its connection to the remote server or local file.

Using FTP

The urlopen function works for remote files just as easily as it does for local files, provided you’re connected to the Internet. For example, if you supply a URL for an FTP server’s root directory, you may be able to pull back its contents, as shown here:

>>> fd = urlopen("ftp://ftp.oreilly.com") >>> print fd.read( ...

Get Python & XML 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.