Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{urllib} --- |
| 2 | Open an arbitrary object given by URL.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | \declaremodule{standard}{urllib} |
| 4 | |
| 5 | \modulesynopsis{Open an arbitrary object given by URL (requires sockets).} |
| 6 | |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 7 | \index{WWW} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 8 | \index{World-Wide Web} |
Guido van Rossum | 61d34f4 | 1995-02-27 17:51:51 +0000 | [diff] [blame] | 9 | \index{URL} |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 12 | This module provides a high-level interface for fetching data across |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 13 | the World-Wide Web. In particular, the \function{urlopen()} function |
| 14 | is similar to the built-in function \function{open()}, but accepts |
| 15 | Universal Resource Locators (URLs) instead of filenames. Some |
| 16 | restrictions apply --- it can only open URLs for reading, and no seek |
| 17 | operations are available. |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 18 | |
Fred Drake | f5eaa2e | 1997-12-15 22:13:50 +0000 | [diff] [blame] | 19 | It defines the following public functions: |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 20 | |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame] | 21 | \begin{funcdesc}{urlopen}{url\optional{, data}} |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 22 | 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] | 23 | 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] | 24 | identifier, this opens a local file; otherwise it opens a socket to a |
| 25 | server somewhere on the network. If the connection cannot be made, or |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 26 | if the server returns an error code, the \exception{IOError} exception |
| 27 | is raised. If all went well, a file-like object is returned. This |
| 28 | supports the following methods: \method{read()}, \method{readline()}, |
| 29 | \method{readlines()}, \method{fileno()}, \method{close()} and |
| 30 | \method{info()}. |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame] | 31 | |
| 32 | Except for the \method{info()} method, |
| 33 | these methods have the same interface as for |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 34 | file objects --- see section \ref{bltin-file-objects} in this |
| 35 | 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] | 36 | used at those few places where a true built-in file object is |
| 37 | required.) |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 38 | |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 39 | The \method{info()} method returns an instance of the class |
Guido van Rossum | 954b9ad | 1998-09-28 14:08:29 +0000 | [diff] [blame] | 40 | \class{mimetools.Message} containing meta-information associated |
| 41 | with the URL. When the method is HTTP, these headers are those |
| 42 | returned by the server at the head of the retrieved HTML page |
| 43 | (including Content-Length and Content-Type). When the method is FTP, |
| 44 | a Content-Length header will be present if (as is now usual) the |
| 45 | server passed back a file length in response to the FTP retrieval |
| 46 | request. When the method is local-file, returned headers will include |
| 47 | a Date representing the file's last-modified time, a Content-Length |
| 48 | giving file size, and a Content-Type containing a guess at the file's |
| 49 | type. See also the description of the |
| 50 | \module{mimetools}\refstmodindex{mimetools} module. |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame] | 51 | |
| 52 | If the \var{url} uses the \file{http:} scheme identifier, the optional |
| 53 | \var{data} argument may be given to specify a \code{POST} request |
| 54 | (normally the request type is \code{GET}). The \var{data} argument |
| 55 | must in standard \file{application/x-www-form-urlencoded} format; |
| 56 | see the \function{urlencode()} function below. |
| 57 | |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 58 | \end{funcdesc} |
| 59 | |
Fred Drake | 09b2957 | 1998-10-01 20:43:13 +0000 | [diff] [blame] | 60 | \begin{funcdesc}{urlretrieve}{url\optional{, filename}\optional{, hook}} |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 61 | 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] | 62 | 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] | 63 | object exists, the object is not copied. Return a tuple |
| 64 | \code{(\var{filename}, \var{headers})} where \var{filename} is the |
| 65 | local file name under which the object can be found, and \var{headers} |
| 66 | is either \code{None} (for a local object) or whatever the |
| 67 | \method{info()} method of the object returned by \function{urlopen()} |
| 68 | returned (for a remote object, possibly cached). Exceptions are the |
| 69 | same as for \function{urlopen()}. |
Guido van Rossum | 954b9ad | 1998-09-28 14:08:29 +0000 | [diff] [blame] | 70 | |
| 71 | The second argument, if present, specifies the file location to copy |
| 72 | to (if absent, the location will be a tempfile with a generated name). |
| 73 | The third argument, if present, is a hook function that will be called |
| 74 | once on establishment of the network connection and once after each |
| 75 | block read thereafter. The hook will be passed three arguments; a |
| 76 | count of blocks transferred so far, a block size in bytes, and the |
Fred Drake | 09b2957 | 1998-10-01 20:43:13 +0000 | [diff] [blame] | 77 | total size of the file. The third argument may be \code{-1} on older |
| 78 | FTP servers which do not return a file size in response to a retrieval |
Guido van Rossum | 954b9ad | 1998-09-28 14:08:29 +0000 | [diff] [blame] | 79 | request. |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 80 | \end{funcdesc} |
| 81 | |
| 82 | \begin{funcdesc}{urlcleanup}{} |
| 83 | 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] | 84 | \function{urlretrieve()}. |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 85 | \end{funcdesc} |
| 86 | |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame] | 87 | \begin{funcdesc}{quote}{string\optional{, safe}} |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 88 | Replace special characters in \var{string} using the \samp{\%xx} escape. |
| 89 | Letters, digits, and the characters \character{_,.-} are never quoted. |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame] | 90 | The optional \var{safe} parameter specifies additional characters |
Guido van Rossum | 61d34f4 | 1995-02-27 17:51:51 +0000 | [diff] [blame] | 91 | that should not be quoted --- its default value is \code{'/'}. |
| 92 | |
Guido van Rossum | 8d40c84 | 1996-12-13 14:48:47 +0000 | [diff] [blame] | 93 | Example: \code{quote('/\~connolly/')} yields \code{'/\%7econnolly/'}. |
| 94 | \end{funcdesc} |
| 95 | |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame] | 96 | \begin{funcdesc}{quote_plus}{string\optional{, safe}} |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 97 | Like \function{quote()}, but also replaces spaces by plus signs, as |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame] | 98 | required for quoting HTML form values. Plus signs in the original |
| 99 | string are escaped unless they are included in \var{safe}. |
Guido van Rossum | 61d34f4 | 1995-02-27 17:51:51 +0000 | [diff] [blame] | 100 | \end{funcdesc} |
| 101 | |
| 102 | \begin{funcdesc}{unquote}{string} |
Guido van Rossum | 6c4f003 | 1995-03-07 10:14:09 +0000 | [diff] [blame] | 103 | Replace \samp{\%xx} escapes by their single-character equivalent. |
Guido van Rossum | 61d34f4 | 1995-02-27 17:51:51 +0000 | [diff] [blame] | 104 | |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 105 | Example: \code{unquote('/\%7Econnolly/')} yields \code{'/\~connolly/'}. |
Guido van Rossum | 61d34f4 | 1995-02-27 17:51:51 +0000 | [diff] [blame] | 106 | \end{funcdesc} |
| 107 | |
Guido van Rossum | 8d40c84 | 1996-12-13 14:48:47 +0000 | [diff] [blame] | 108 | \begin{funcdesc}{unquote_plus}{string} |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 109 | Like \function{unquote()}, but also replaces plus signs by spaces, as |
Guido van Rossum | 8d40c84 | 1996-12-13 14:48:47 +0000 | [diff] [blame] | 110 | required for unquoting HTML form values. |
| 111 | \end{funcdesc} |
| 112 | |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame] | 113 | \begin{funcdesc}{urlencode}{dict} |
| 114 | Convert a dictionary to a ``url-encoded'' string, suitable to pass to |
| 115 | \function{urlopen()} above as the optional \var{data} argument. This |
| 116 | is useful to pass a dictionary of form fields to a \code{POST} |
Fred Drake | 09b2957 | 1998-10-01 20:43:13 +0000 | [diff] [blame] | 117 | request. The resulting string is a series of |
| 118 | \code{\var{key}=\var{value}} pairs separated by \character{\&} |
| 119 | characters, where both \var{key} and \var{value} are quoted using |
| 120 | \function{quote_plus()} above. |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame] | 121 | \end{funcdesc} |
| 122 | |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 123 | Restrictions: |
| 124 | |
| 125 | \begin{itemize} |
| 126 | |
| 127 | \item |
| 128 | Currently, only the following protocols are supported: HTTP, (versions |
| 129 | 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] | 130 | \indexii{HTTP}{protocol} |
| 131 | \indexii{Gopher}{protocol} |
| 132 | \indexii{FTP}{protocol} |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 133 | |
| 134 | \item |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 135 | The caching feature of \function{urlretrieve()} has been disabled |
| 136 | until I find the time to hack proper processing of Expiration time |
| 137 | headers. |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 138 | |
| 139 | \item |
Guido van Rossum | 6c4f003 | 1995-03-07 10:14:09 +0000 | [diff] [blame] | 140 | 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] | 141 | the cache. |
| 142 | |
| 143 | \item |
| 144 | For backward compatibility, if a URL appears to point to a local file |
| 145 | but the file can't be opened, the URL is re-interpreted using the FTP |
| 146 | protocol. This can sometimes cause confusing error messages. |
| 147 | |
| 148 | \item |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 149 | The \function{urlopen()} and \function{urlretrieve()} functions can |
| 150 | cause arbitrarily long delays while waiting for a network connection |
| 151 | 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] | 152 | web client using these functions without using threads. |
| 153 | |
| 154 | \item |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 155 | The data returned by \function{urlopen()} or \function{urlretrieve()} |
| 156 | is the raw data returned by the server. This may be binary data |
| 157 | (e.g. an image), plain text or (for example) HTML. The HTTP protocol |
| 158 | provides type information in the reply header, which can be inspected |
| 159 | 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] | 160 | type information is encoded in the URL; there is currently no easy way |
| 161 | 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] | 162 | \module{htmllib}\refstmodindex{htmllib} to parse it. |
| 163 | \index{HTML} |
| 164 | \indexii{HTTP}{protocol} |
| 165 | \indexii{Gopher}{protocol} |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 166 | |
| 167 | \item |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 168 | Although the \module{urllib} module contains (undocumented) routines |
| 169 | to parse and unparse URL strings, the recommended interface for URL |
| 170 | manipulation is in module \module{urlparse}\refstmodindex{urlparse}. |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 171 | |
| 172 | \end{itemize} |