Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 1 | \section{Built-in module \sectcode{urllib}} |
| 2 | \stmodindex{urllib} |
| 3 | \index{WWW} |
| 4 | \indexii{World-Wide}{Web} |
Guido van Rossum | 61d34f4 | 1995-02-27 17:51:51 +0000 | [diff] [blame] | 5 | \index{URL} |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 7 | \renewcommand{\indexsubitem}{(in module urllib)} |
| 8 | |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 9 | This module provides a high-level interface for fetching data across |
| 10 | the World-Wide Web. In particular, the \code{urlopen} function is |
| 11 | similar to the built-in function \code{open}, but accepts URLs |
| 12 | (Universal Resource Locators) instead of filenames. Some restrictions |
| 13 | apply --- it can only open URLs for reading, and no seek operations |
| 14 | are available. |
| 15 | |
| 16 | it defines the following public functions: |
| 17 | |
| 18 | \begin{funcdesc}{urlopen}{url} |
| 19 | Open a network object denoted by a URL for reading. If the URL does |
| 20 | not have a scheme identifier, or if it has \code{file:} as its scheme |
| 21 | identifier, this opens a local file; otherwise it opens a socket to a |
| 22 | server somewhere on the network. If the connection cannot be made, or |
| 23 | if the server returns an error code, the \code{IOError} exception is |
| 24 | raised. If all went well, a file-like object is returned. This |
| 25 | supports the following methods: \code{read()}, \code{readline()}, |
| 26 | \code{readlines()}, \code{fileno()}, \code{close()} and \code{info()}. |
| 27 | Except for the last one, these methods have the same interface as for |
| 28 | file objects --- see the section on File Objects earlier in this |
| 29 | manual. |
| 30 | |
| 31 | The \code{info()} method returns an instance of the class |
| 32 | \code{rfc822.Message} containing the headers received from the server, |
| 33 | if the protocol uses such headers (currently the only supported |
| 34 | protocol that uses this is HTTP). See the description of the |
| 35 | \code{rfc822} module. |
| 36 | \end{funcdesc} |
| 37 | |
| 38 | \begin{funcdesc}{urlretrieve}{url} |
| 39 | Copy a network object denoted by a URL to a local file, if necessary. |
| 40 | If the URL points to a local file, or a valid cached copy of the the |
| 41 | object exists, the object is not copied. Return a tuple (\var{filename}, |
| 42 | \var{headers}) where \var{filename} is the local file name under which |
| 43 | the object can be found, and \var{headers} is either \code{None} (for |
| 44 | a local object) or whatever the \code{info()} method of the object |
| 45 | returned by \code{urlopen()} returned (for a remote object, possibly |
| 46 | cached). Exceptions are the same as for \code{urlopen()}. |
| 47 | \end{funcdesc} |
| 48 | |
| 49 | \begin{funcdesc}{urlcleanup}{} |
| 50 | Clear the cache that may have been built up by previous calls to |
| 51 | \code{urlretrieve()}. |
| 52 | \end{funcdesc} |
| 53 | |
Guido van Rossum | 61d34f4 | 1995-02-27 17:51:51 +0000 | [diff] [blame] | 54 | \begin{funcdesc}{quote}{string\optional{\, addsafe}} |
| 55 | Replace special characters in \var{string} using the \code{\%xx} escape. |
| 56 | Letters, digits, and the characters ``\code{_,.-}'' are never quoted. |
| 57 | The optional \var{addsafe} parameter specifies additional characters |
| 58 | that should not be quoted --- its default value is \code{'/'}. |
| 59 | |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 60 | Example: \code{quote('/\~conolly/')} yields \code{'/\%7econnolly/'}. |
Guido van Rossum | 61d34f4 | 1995-02-27 17:51:51 +0000 | [diff] [blame] | 61 | \end{funcdesc} |
| 62 | |
| 63 | \begin{funcdesc}{unquote}{string} |
| 64 | Remove \code{\%xx} escapes by their single-character equivalent. |
| 65 | |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 66 | Example: \code{unquote('/\%7Econnolly/')} yields \code{'/\~connolly/'}. |
Guido van Rossum | 61d34f4 | 1995-02-27 17:51:51 +0000 | [diff] [blame] | 67 | \end{funcdesc} |
| 68 | |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 69 | Restrictions: |
| 70 | |
| 71 | \begin{itemize} |
| 72 | |
| 73 | \item |
| 74 | Currently, only the following protocols are supported: HTTP, (versions |
| 75 | 0.9 and 1.0), Gopher (but not Gopher-+), FTP, and local files. |
| 76 | \index{HTTP} |
| 77 | \index{Gopher} |
| 78 | \index{FTP} |
| 79 | |
| 80 | \item |
| 81 | The caching feature of \code{urlretrieve()} has been disabled until I |
| 82 | find the time to hack proper processing of Expiration time headers. |
| 83 | |
| 84 | \item |
| 85 | There should be an function to query whether a particular URL is in |
| 86 | the cache. |
| 87 | |
| 88 | \item |
| 89 | For backward compatibility, if a URL appears to point to a local file |
| 90 | but the file can't be opened, the URL is re-interpreted using the FTP |
| 91 | protocol. This can sometimes cause confusing error messages. |
| 92 | |
| 93 | \item |
| 94 | The \code{urlopen()} and \code{urlretrieve()} functions can cause |
| 95 | arbitrarily long delays while waiting for a network connection to be |
| 96 | set up. This means that it is difficult to build an interactive |
| 97 | web client using these functions without using threads. |
| 98 | |
| 99 | \item |
| 100 | The data returned by \code{urlopen()} or \code{urlretrieve()} is the |
| 101 | raw data returned by the server. This may be binary data (e.g. an |
| 102 | image), plain text or (for example) HTML. The HTTP protocol provides |
| 103 | type information in the reply header, which can be inspected by |
| 104 | looking at the \code{Content-type} header. For the Gopher protocol, |
| 105 | type information is encoded in the URL; there is currently no easy way |
| 106 | to extract it. If the returned data is HTML, you can use the module |
| 107 | \code{htmllib} to parse it. |
| 108 | \index{HTML} |
| 109 | \index{HTTP} |
| 110 | \index{Gopher} |
| 111 | \stmodindex{htmllib} |
| 112 | |
| 113 | \item |
| 114 | Although the \code{urllib} module contains (undocumented) routines to |
| 115 | parse and unparse URL strings, the recommended interface for URL |
| 116 | manipulation is in module \code{urlparse}. |
| 117 | \stmodindex{urlparse} |
| 118 | |
| 119 | \end{itemize} |