Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{urllib} --- |
Fred Drake | dfca4dc | 2000-08-25 05:13:42 +0000 | [diff] [blame] | 2 | Open arbitrary resources by URL} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{urllib} |
| 5 | \modulesynopsis{Open an arbitrary network resource by URL (requires sockets).} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 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()}, |
Fred Drake | 1ec71cb | 1999-02-22 22:42:14 +0000 | [diff] [blame] | 29 | \method{readlines()}, \method{fileno()}, \method{close()}, |
| 30 | \method{info()} and \method{geturl()}. |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame] | 31 | |
Fred Drake | 1ec71cb | 1999-02-22 22:42:14 +0000 | [diff] [blame] | 32 | Except for the \method{info()} and \method{geturl()} methods, |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame] | 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 |
Fred Drake | 1ec71cb | 1999-02-22 22:42:14 +0000 | [diff] [blame] | 50 | \refmodule{mimetools}\refstmodindex{mimetools} module. |
| 51 | |
| 52 | The \method{geturl()} method returns the real URL of the page. In |
| 53 | some cases, the HTTP server redirects a client to another URL. The |
| 54 | \function{urlopen()} function handles this transparently, but in some |
| 55 | cases the caller needs to know which URL the client was redirected |
| 56 | to. The \method{geturl()} method can be used to get at this |
| 57 | redirected URL. |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame] | 58 | |
| 59 | If the \var{url} uses the \file{http:} scheme identifier, the optional |
| 60 | \var{data} argument may be given to specify a \code{POST} request |
| 61 | (normally the request type is \code{GET}). The \var{data} argument |
Fred Drake | 5100133 | 2000-12-15 23:57:51 +0000 | [diff] [blame] | 62 | must in standard \mimetype{application/x-www-form-urlencoded} format; |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame] | 63 | see the \function{urlencode()} function below. |
| 64 | |
Fred Drake | aef0e89 | 2000-08-31 17:23:35 +0000 | [diff] [blame] | 65 | The \function{urlopen()} function works transparently with proxies |
Fred Drake | 81c1735 | 2000-09-15 04:12:56 +0000 | [diff] [blame] | 66 | which do not require authentication. In a \UNIX{} or Windows |
Fred Drake | aef0e89 | 2000-08-31 17:23:35 +0000 | [diff] [blame] | 67 | environment, set the \envvar{http_proxy}, \envvar{ftp_proxy} or |
| 68 | \envvar{gopher_proxy} environment variables to a URL that identifies |
| 69 | the proxy server before starting the Python interpreter. For example |
| 70 | (the \character{\%} is the command prompt): |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 71 | |
| 72 | \begin{verbatim} |
| 73 | % http_proxy="http://www.someproxy.com:3128" |
| 74 | % export http_proxy |
| 75 | % python |
| 76 | ... |
| 77 | \end{verbatim} |
| 78 | |
| 79 | In a Macintosh environment, \function{urlopen()} will retrieve proxy |
| 80 | information from Internet\index{Internet Config} Config. |
| 81 | |
Fred Drake | aef0e89 | 2000-08-31 17:23:35 +0000 | [diff] [blame] | 82 | Proxies which require authentication for use are not currently |
| 83 | supported; this is considered an implementation limitation. |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 84 | \end{funcdesc} |
| 85 | |
Fred Drake | 5100133 | 2000-12-15 23:57:51 +0000 | [diff] [blame] | 86 | \begin{funcdesc}{urlretrieve}{url\optional{, filename\optional{, |
| 87 | reporthook\optional{, data}}}} |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 88 | 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] | 89 | 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] | 90 | object exists, the object is not copied. Return a tuple |
| 91 | \code{(\var{filename}, \var{headers})} where \var{filename} is the |
| 92 | local file name under which the object can be found, and \var{headers} |
| 93 | is either \code{None} (for a local object) or whatever the |
| 94 | \method{info()} method of the object returned by \function{urlopen()} |
| 95 | returned (for a remote object, possibly cached). Exceptions are the |
| 96 | same as for \function{urlopen()}. |
Guido van Rossum | 954b9ad | 1998-09-28 14:08:29 +0000 | [diff] [blame] | 97 | |
| 98 | The second argument, if present, specifies the file location to copy |
| 99 | to (if absent, the location will be a tempfile with a generated name). |
| 100 | The third argument, if present, is a hook function that will be called |
| 101 | once on establishment of the network connection and once after each |
| 102 | block read thereafter. The hook will be passed three arguments; a |
| 103 | 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] | 104 | total size of the file. The third argument may be \code{-1} on older |
| 105 | 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] | 106 | request. |
Fred Drake | 9fa4d61 | 2000-08-24 01:06:40 +0000 | [diff] [blame] | 107 | |
| 108 | If the \var{url} uses the \file{http:} scheme identifier, the optional |
| 109 | \var{data} argument may be given to specify a \code{POST} request |
| 110 | (normally the request type is \code{GET}). The \var{data} argument |
Fred Drake | 5100133 | 2000-12-15 23:57:51 +0000 | [diff] [blame] | 111 | must in standard \mimetype{application/x-www-form-urlencoded} format; |
Fred Drake | 9fa4d61 | 2000-08-24 01:06:40 +0000 | [diff] [blame] | 112 | see the \function{urlencode()} function below. |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 113 | \end{funcdesc} |
| 114 | |
| 115 | \begin{funcdesc}{urlcleanup}{} |
| 116 | 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] | 117 | \function{urlretrieve()}. |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 118 | \end{funcdesc} |
| 119 | |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame] | 120 | \begin{funcdesc}{quote}{string\optional{, safe}} |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 121 | Replace special characters in \var{string} using the \samp{\%xx} escape. |
| 122 | Letters, digits, and the characters \character{_,.-} are never quoted. |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame] | 123 | The optional \var{safe} parameter specifies additional characters |
Guido van Rossum | 61d34f4 | 1995-02-27 17:51:51 +0000 | [diff] [blame] | 124 | that should not be quoted --- its default value is \code{'/'}. |
| 125 | |
Fred Drake | 10853c9 | 2000-07-28 13:51:27 +0000 | [diff] [blame] | 126 | Example: \code{quote('/\~{}connolly/')} yields \code{'/\%7econnolly/'}. |
Guido van Rossum | 8d40c84 | 1996-12-13 14:48:47 +0000 | [diff] [blame] | 127 | \end{funcdesc} |
| 128 | |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame] | 129 | \begin{funcdesc}{quote_plus}{string\optional{, safe}} |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 130 | Like \function{quote()}, but also replaces spaces by plus signs, as |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame] | 131 | required for quoting HTML form values. Plus signs in the original |
| 132 | string are escaped unless they are included in \var{safe}. |
Guido van Rossum | 61d34f4 | 1995-02-27 17:51:51 +0000 | [diff] [blame] | 133 | \end{funcdesc} |
| 134 | |
| 135 | \begin{funcdesc}{unquote}{string} |
Guido van Rossum | 6c4f003 | 1995-03-07 10:14:09 +0000 | [diff] [blame] | 136 | Replace \samp{\%xx} escapes by their single-character equivalent. |
Guido van Rossum | 61d34f4 | 1995-02-27 17:51:51 +0000 | [diff] [blame] | 137 | |
Fred Drake | 10853c9 | 2000-07-28 13:51:27 +0000 | [diff] [blame] | 138 | Example: \code{unquote('/\%7Econnolly/')} yields \code{'/\~{}connolly/'}. |
Guido van Rossum | 61d34f4 | 1995-02-27 17:51:51 +0000 | [diff] [blame] | 139 | \end{funcdesc} |
| 140 | |
Guido van Rossum | 8d40c84 | 1996-12-13 14:48:47 +0000 | [diff] [blame] | 141 | \begin{funcdesc}{unquote_plus}{string} |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 142 | Like \function{unquote()}, but also replaces plus signs by spaces, as |
Guido van Rossum | 8d40c84 | 1996-12-13 14:48:47 +0000 | [diff] [blame] | 143 | required for unquoting HTML form values. |
| 144 | \end{funcdesc} |
| 145 | |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame] | 146 | \begin{funcdesc}{urlencode}{dict} |
| 147 | Convert a dictionary to a ``url-encoded'' string, suitable to pass to |
| 148 | \function{urlopen()} above as the optional \var{data} argument. This |
| 149 | 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] | 150 | request. The resulting string is a series of |
| 151 | \code{\var{key}=\var{value}} pairs separated by \character{\&} |
| 152 | characters, where both \var{key} and \var{value} are quoted using |
| 153 | \function{quote_plus()} above. |
Guido van Rossum | 0af2f63 | 1998-07-22 21:34:21 +0000 | [diff] [blame] | 154 | \end{funcdesc} |
| 155 | |
Fred Drake | dfca4dc | 2000-08-25 05:13:42 +0000 | [diff] [blame] | 156 | The public functions \function{urlopen()} and |
| 157 | \function{urlretrieve()} create an instance of the |
| 158 | \class{FancyURLopener} class and use it to perform their requested |
| 159 | actions. To override this functionality, programmers can create a |
| 160 | subclass of \class{URLopener} or \class{FancyURLopener}, then assign |
| 161 | that an instance of that class to the |
| 162 | \code{urllib._urlopener} variable before calling the desired function. |
| 163 | For example, applications may want to specify a different |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 164 | \code{user-agent} header than \class{URLopener} defines. This can be |
| 165 | accomplished with the following code: |
| 166 | |
| 167 | \begin{verbatim} |
| 168 | class AppURLopener(urllib.FancyURLopener): |
| 169 | def __init__(self, *args): |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 170 | self.version = "App/1.7" |
Guido van Rossum | 3d0c77b | 2000-08-24 16:20:32 +0000 | [diff] [blame] | 171 | apply(urllib.FancyURLopener.__init__, (self,) + args) |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 172 | |
Fred Drake | 6c16019 | 2000-05-30 14:39:45 +0000 | [diff] [blame] | 173 | urllib._urlopener = AppURLopener() |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 174 | \end{verbatim} |
| 175 | |
| 176 | \begin{classdesc}{URLopener}{\optional{proxies\optional{, **x509}}} |
| 177 | Base class for opening and reading URLs. Unless you need to support |
| 178 | opening objects using schemes other than \file{http:}, \file{ftp:}, |
| 179 | \file{gopher:} or \file{file:}, you probably want to use |
| 180 | \class{FancyURLopener}. |
| 181 | |
| 182 | By default, the \class{URLopener} class sends a |
| 183 | \code{user-agent} header of \samp{urllib/\var{VVV}}, where |
| 184 | \var{VVV} is the \module{urllib} version number. Applications can |
| 185 | define their own \code{user-agent} header by subclassing |
| 186 | \class{URLopener} or \class{FancyURLopener} and setting the instance |
Fred Drake | dfca4dc | 2000-08-25 05:13:42 +0000 | [diff] [blame] | 187 | attribute \member{version} to an appropriate string value before the |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 188 | \method{open()} method is called. |
| 189 | |
| 190 | Additional keyword parameters, collected in \var{x509}, are used for |
| 191 | authentication with the \file{https:} scheme. The keywords |
| 192 | \var{key_file} and \var{cert_file} are supported; both are needed to |
| 193 | actually retrieve a resource at an \file{https:} URL. |
| 194 | \end{classdesc} |
| 195 | |
| 196 | \begin{classdesc}{FancyURLopener}{...} |
| 197 | \class{FancyURLopener} subclasses \class{URLopener} providing default |
| 198 | handling for the following HTTP response codes: 301, 302 or 401. For |
| 199 | 301 and 302 response codes, the \code{location} header is used to |
| 200 | fetch the actual URL. For 401 response codes (authentication |
| 201 | required), basic HTTP authentication is performed. |
| 202 | |
| 203 | The parameters to the constructor are the same as those for |
| 204 | \class{URLopener}. |
| 205 | \end{classdesc} |
| 206 | |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 207 | Restrictions: |
| 208 | |
| 209 | \begin{itemize} |
| 210 | |
| 211 | \item |
| 212 | Currently, only the following protocols are supported: HTTP, (versions |
| 213 | 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] | 214 | \indexii{HTTP}{protocol} |
| 215 | \indexii{Gopher}{protocol} |
| 216 | \indexii{FTP}{protocol} |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 217 | |
| 218 | \item |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 219 | The caching feature of \function{urlretrieve()} has been disabled |
| 220 | until I find the time to hack proper processing of Expiration time |
| 221 | headers. |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 222 | |
| 223 | \item |
Guido van Rossum | 6c4f003 | 1995-03-07 10:14:09 +0000 | [diff] [blame] | 224 | 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] | 225 | the cache. |
| 226 | |
| 227 | \item |
| 228 | For backward compatibility, if a URL appears to point to a local file |
| 229 | but the file can't be opened, the URL is re-interpreted using the FTP |
| 230 | protocol. This can sometimes cause confusing error messages. |
| 231 | |
| 232 | \item |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 233 | The \function{urlopen()} and \function{urlretrieve()} functions can |
| 234 | cause arbitrarily long delays while waiting for a network connection |
| 235 | 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] | 236 | web client using these functions without using threads. |
| 237 | |
| 238 | \item |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 239 | The data returned by \function{urlopen()} or \function{urlretrieve()} |
| 240 | is the raw data returned by the server. This may be binary data |
Fred Drake | 1ec71cb | 1999-02-22 22:42:14 +0000 | [diff] [blame] | 241 | (e.g. an image), plain text or (for example) HTML\index{HTML}. The |
| 242 | HTTP\indexii{HTTP}{protocol} protocol provides type information in the |
| 243 | reply header, which can be inspected by looking at the |
| 244 | \code{content-type} header. For the Gopher\indexii{Gopher}{protocol} |
| 245 | protocol, type information is encoded in the URL; there is currently |
| 246 | no easy way to extract it. If the returned data is HTML, you can use |
| 247 | the module \refmodule{htmllib}\refstmodindex{htmllib} to parse it. |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 248 | |
| 249 | \item |
Fred Drake | 81c1735 | 2000-09-15 04:12:56 +0000 | [diff] [blame] | 250 | This module does not support the use of proxies which require |
| 251 | authentication. This may be implemented in the future. |
| 252 | |
| 253 | \item |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 254 | Although the \module{urllib} module contains (undocumented) routines |
| 255 | to parse and unparse URL strings, the recommended interface for URL |
Fred Drake | 1ec71cb | 1999-02-22 22:42:14 +0000 | [diff] [blame] | 256 | manipulation is in module \refmodule{urlparse}\refstmodindex{urlparse}. |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 257 | |
| 258 | \end{itemize} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 259 | |
| 260 | |
| 261 | \subsection{URLopener Objects \label{urlopener-objs}} |
| 262 | \sectionauthor{Skip Montanaro}{skip@mojam.com} |
| 263 | |
| 264 | \class{URLopener} and \class{FancyURLopener} objects have the |
Fred Drake | dfca4dc | 2000-08-25 05:13:42 +0000 | [diff] [blame] | 265 | following attributes. |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 266 | |
Fred Drake | dfca4dc | 2000-08-25 05:13:42 +0000 | [diff] [blame] | 267 | \begin{methoddesc}[URLopener]{open}{fullurl\optional{, data}} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 268 | Open \var{fullurl} using the appropriate protocol. This method sets |
| 269 | up cache and proxy information, then calls the appropriate open method with |
| 270 | its input arguments. If the scheme is not recognized, |
| 271 | \method{open_unknown()} is called. The \var{data} argument |
| 272 | has the same meaning as the \var{data} argument of \function{urlopen()}. |
| 273 | \end{methoddesc} |
| 274 | |
Fred Drake | dfca4dc | 2000-08-25 05:13:42 +0000 | [diff] [blame] | 275 | \begin{methoddesc}[URLopener]{open_unknown}{fullurl\optional{, data}} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 276 | Overridable interface to open unknown URL types. |
| 277 | \end{methoddesc} |
| 278 | |
Fred Drake | dfca4dc | 2000-08-25 05:13:42 +0000 | [diff] [blame] | 279 | \begin{methoddesc}[URLopener]{retrieve}{url\optional{, |
| 280 | filename\optional{, |
| 281 | reporthook\optional{, data}}}} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 282 | Retrieves the contents of \var{url} and places it in \var{filename}. The |
| 283 | return value is a tuple consisting of a local filename and either a |
| 284 | \class{mimetools.Message} object containing the response headers (for remote |
| 285 | URLs) or None (for local URLs). The caller must then open and read the |
| 286 | contents of \var{filename}. If \var{filename} is not given and the URL |
| 287 | refers to a local file, the input filename is returned. If the URL is |
| 288 | non-local and \var{filename} is not given, the filename is the output of |
| 289 | \function{tempfile.mktemp()} with a suffix that matches the suffix of the last |
| 290 | path component of the input URL. If \var{reporthook} is given, it must be |
| 291 | a function accepting three numeric parameters. It will be called after each |
| 292 | chunk of data is read from the network. \var{reporthook} is ignored for |
| 293 | local URLs. |
Fred Drake | 9fa4d61 | 2000-08-24 01:06:40 +0000 | [diff] [blame] | 294 | |
| 295 | If the \var{url} uses the \file{http:} scheme identifier, the optional |
| 296 | \var{data} argument may be given to specify a \code{POST} request |
| 297 | (normally the request type is \code{GET}). The \var{data} argument |
Fred Drake | 5100133 | 2000-12-15 23:57:51 +0000 | [diff] [blame] | 298 | must in standard \mimetype{application/x-www-form-urlencoded} format; |
Fred Drake | 9fa4d61 | 2000-08-24 01:06:40 +0000 | [diff] [blame] | 299 | see the \function{urlencode()} function below. |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 300 | \end{methoddesc} |
| 301 | |
Fred Drake | dfca4dc | 2000-08-25 05:13:42 +0000 | [diff] [blame] | 302 | \begin{memberdesc}[URLopener]{version} |
| 303 | Variable that specifies the user agent of the opener object. To get |
| 304 | \refmodule{urllib} to tell servers that it is a particular user agent, |
| 305 | set this in a subclass as a class variable or in the constructor |
| 306 | before calling the base constructor. |
| 307 | \end{memberdesc} |
| 308 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 309 | |
| 310 | \subsection{Examples} |
| 311 | \nodename{Urllib Examples} |
| 312 | |
| 313 | Here is an example session that uses the \samp{GET} method to retrieve |
| 314 | a URL containing parameters: |
| 315 | |
| 316 | \begin{verbatim} |
| 317 | >>> import urllib |
| 318 | >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) |
| 319 | >>> f = urllib.urlopen("http://www.musi-cal.com/cgi-bin/query?%s" % params) |
| 320 | >>> print f.read() |
| 321 | \end{verbatim} |
| 322 | |
| 323 | The following example uses the \samp{POST} method instead: |
| 324 | |
| 325 | \begin{verbatim} |
| 326 | >>> import urllib |
| 327 | >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) |
| 328 | >>> f = urllib.urlopen("http://www.musi-cal.com/cgi-bin/query", params) |
| 329 | >>> print f.read() |
| 330 | \end{verbatim} |