Fred Drake | 3a0351c | 1998-04-04 07:23:21 +0000 | [diff] [blame] | 1 | \section{Standard Module \module{urllib}} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 2 | \label{module-urllib} |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 3 | \stmodindex{urllib} |
| 4 | \index{WWW} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 5 | \index{World-Wide Web} |
Guido van Rossum | 61d34f4 | 1995-02-27 17:51:51 +0000 | [diff] [blame] | 6 | \index{URL} |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 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 |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 10 | the World-Wide Web. In particular, the \function{urlopen()} function |
| 11 | is similar to the built-in function \function{open()}, but accepts |
| 12 | Universal Resource Locators (URLs) instead of filenames. Some |
| 13 | restrictions apply --- it can only open URLs for reading, and no seek |
| 14 | operations are available. |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 15 | |
Fred Drake | f5eaa2e | 1997-12-15 22:13:50 +0000 | [diff] [blame] | 16 | It defines the following public functions: |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame^] | 18 | \begin{funcdesc}{urlopen}{url\optional{, data}} |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 19 | Open a network object denoted by a URL for reading. If the URL does |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 20 | not have a scheme identifier, or if it has \file{file:} as its scheme |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 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 |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 23 | if the server returns an error code, the \exception{IOError} exception |
| 24 | is raised. If all went well, a file-like object is returned. This |
| 25 | supports the following methods: \method{read()}, \method{readline()}, |
| 26 | \method{readlines()}, \method{fileno()}, \method{close()} and |
| 27 | \method{info()}. |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame^] | 28 | |
| 29 | Except for the \method{info()} method, |
| 30 | these methods have the same interface as for |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 31 | file objects --- see section \ref{bltin-file-objects} in this |
| 32 | manual. (It is not a built-in file object, however, so it can't be |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 33 | used at those few places where a true built-in file object is |
| 34 | required.) |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 35 | |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 36 | The \method{info()} method returns an instance of the class |
| 37 | \class{mimetools.Message} containing the headers received from the |
| 38 | server, if the protocol uses such headers (currently the only |
| 39 | supported protocol that uses this is HTTP). See the description of |
| 40 | the \module{mimetools}\refstmodindex{mimetools} module. |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame^] | 41 | |
| 42 | If the \var{url} uses the \file{http:} scheme identifier, the optional |
| 43 | \var{data} argument may be given to specify a \code{POST} request |
| 44 | (normally the request type is \code{GET}). The \var{data} argument |
| 45 | must in standard \file{application/x-www-form-urlencoded} format; |
| 46 | see the \function{urlencode()} function below. |
| 47 | |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 48 | \end{funcdesc} |
| 49 | |
| 50 | \begin{funcdesc}{urlretrieve}{url} |
| 51 | Copy a network object denoted by a URL to a local file, if necessary. |
Guido van Rossum | 6c4f003 | 1995-03-07 10:14:09 +0000 | [diff] [blame] | 52 | If the URL points to a local file, or a valid cached copy of the |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 53 | object exists, the object is not copied. Return a tuple |
| 54 | \code{(\var{filename}, \var{headers})} where \var{filename} is the |
| 55 | local file name under which the object can be found, and \var{headers} |
| 56 | is either \code{None} (for a local object) or whatever the |
| 57 | \method{info()} method of the object returned by \function{urlopen()} |
| 58 | returned (for a remote object, possibly cached). Exceptions are the |
| 59 | same as for \function{urlopen()}. |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 60 | \end{funcdesc} |
| 61 | |
| 62 | \begin{funcdesc}{urlcleanup}{} |
| 63 | Clear the cache that may have been built up by previous calls to |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 64 | \function{urlretrieve()}. |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 65 | \end{funcdesc} |
| 66 | |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame^] | 67 | \begin{funcdesc}{quote}{string\optional{, safe}} |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 68 | Replace special characters in \var{string} using the \samp{\%xx} escape. |
| 69 | Letters, digits, and the characters \character{_,.-} are never quoted. |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame^] | 70 | The optional \var{safe} parameter specifies additional characters |
Guido van Rossum | 61d34f4 | 1995-02-27 17:51:51 +0000 | [diff] [blame] | 71 | that should not be quoted --- its default value is \code{'/'}. |
| 72 | |
Guido van Rossum | 8d40c84 | 1996-12-13 14:48:47 +0000 | [diff] [blame] | 73 | Example: \code{quote('/\~connolly/')} yields \code{'/\%7econnolly/'}. |
| 74 | \end{funcdesc} |
| 75 | |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame^] | 76 | \begin{funcdesc}{quote_plus}{string\optional{, safe}} |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 77 | Like \function{quote()}, but also replaces spaces by plus signs, as |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame^] | 78 | required for quoting HTML form values. Plus signs in the original |
| 79 | string are escaped unless they are included in \var{safe}. |
Guido van Rossum | 61d34f4 | 1995-02-27 17:51:51 +0000 | [diff] [blame] | 80 | \end{funcdesc} |
| 81 | |
| 82 | \begin{funcdesc}{unquote}{string} |
Guido van Rossum | 6c4f003 | 1995-03-07 10:14:09 +0000 | [diff] [blame] | 83 | Replace \samp{\%xx} escapes by their single-character equivalent. |
Guido van Rossum | 61d34f4 | 1995-02-27 17:51:51 +0000 | [diff] [blame] | 84 | |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 85 | Example: \code{unquote('/\%7Econnolly/')} yields \code{'/\~connolly/'}. |
Guido van Rossum | 61d34f4 | 1995-02-27 17:51:51 +0000 | [diff] [blame] | 86 | \end{funcdesc} |
| 87 | |
Guido van Rossum | 8d40c84 | 1996-12-13 14:48:47 +0000 | [diff] [blame] | 88 | \begin{funcdesc}{unquote_plus}{string} |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 89 | Like \function{unquote()}, but also replaces plus signs by spaces, as |
Guido van Rossum | 8d40c84 | 1996-12-13 14:48:47 +0000 | [diff] [blame] | 90 | required for unquoting HTML form values. |
| 91 | \end{funcdesc} |
| 92 | |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame^] | 93 | \begin{funcdesc}{urlencode}{dict} |
| 94 | Convert a dictionary to a ``url-encoded'' string, suitable to pass to |
| 95 | \function{urlopen()} above as the optional \var{data} argument. This |
| 96 | is useful to pass a dictionary of form fields to a \code{POST} |
| 97 | request. The resulting string is a series of \var{key}=\var{value} |
| 98 | pairs separated by \code{&} characters, where both \var{key} and |
| 99 | \var{value} are quoted using \function{quote_plus()} above. |
| 100 | \end{funcdesc} |
| 101 | |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 102 | Restrictions: |
| 103 | |
| 104 | \begin{itemize} |
| 105 | |
| 106 | \item |
| 107 | Currently, only the following protocols are supported: HTTP, (versions |
| 108 | 0.9 and 1.0), Gopher (but not Gopher-+), FTP, and local files. |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 109 | \indexii{HTTP}{protocol} |
| 110 | \indexii{Gopher}{protocol} |
| 111 | \indexii{FTP}{protocol} |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 112 | |
| 113 | \item |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 114 | The caching feature of \function{urlretrieve()} has been disabled |
| 115 | until I find the time to hack proper processing of Expiration time |
| 116 | headers. |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 117 | |
| 118 | \item |
Guido van Rossum | 6c4f003 | 1995-03-07 10:14:09 +0000 | [diff] [blame] | 119 | There should be a function to query whether a particular URL is in |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 120 | the cache. |
| 121 | |
| 122 | \item |
| 123 | For backward compatibility, if a URL appears to point to a local file |
| 124 | but the file can't be opened, the URL is re-interpreted using the FTP |
| 125 | protocol. This can sometimes cause confusing error messages. |
| 126 | |
| 127 | \item |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 128 | The \function{urlopen()} and \function{urlretrieve()} functions can |
| 129 | cause arbitrarily long delays while waiting for a network connection |
| 130 | to be set up. This means that it is difficult to build an interactive |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 131 | web client using these functions without using threads. |
| 132 | |
| 133 | \item |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 134 | The data returned by \function{urlopen()} or \function{urlretrieve()} |
| 135 | is the raw data returned by the server. This may be binary data |
| 136 | (e.g. an image), plain text or (for example) HTML. The HTTP protocol |
| 137 | provides type information in the reply header, which can be inspected |
| 138 | by looking at the \code{content-type} header. For the Gopher protocol, |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 139 | type information is encoded in the URL; there is currently no easy way |
| 140 | to extract it. If the returned data is HTML, you can use the module |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 141 | \module{htmllib}\refstmodindex{htmllib} to parse it. |
| 142 | \index{HTML} |
| 143 | \indexii{HTTP}{protocol} |
| 144 | \indexii{Gopher}{protocol} |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 145 | |
| 146 | \item |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 147 | Although the \module{urllib} module contains (undocumented) routines |
| 148 | to parse and unparse URL strings, the recommended interface for URL |
| 149 | manipulation is in module \module{urlparse}\refstmodindex{urlparse}. |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 150 | |
| 151 | \end{itemize} |