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