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 | |
| 18 | \begin{funcdesc}{urlopen}{url} |
| 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 | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 28 | Except for the last one, these methods have the same interface as for |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 29 | file objects --- see section \ref{bltin-file-objects} in this |
| 30 | 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] | 31 | used at those few places where a true built-in file object is |
| 32 | required.) |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 33 | |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 34 | The \method{info()} method returns an instance of the class |
| 35 | \class{mimetools.Message} containing the headers received from the |
| 36 | server, if the protocol uses such headers (currently the only |
| 37 | supported protocol that uses this is HTTP). See the description of |
| 38 | the \module{mimetools}\refstmodindex{mimetools} module. |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 39 | \end{funcdesc} |
| 40 | |
| 41 | \begin{funcdesc}{urlretrieve}{url} |
| 42 | 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] | 43 | 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] | 44 | object exists, the object is not copied. Return a tuple |
| 45 | \code{(\var{filename}, \var{headers})} where \var{filename} is the |
| 46 | local file name under which the object can be found, and \var{headers} |
| 47 | is either \code{None} (for a local object) or whatever the |
| 48 | \method{info()} method of the object returned by \function{urlopen()} |
| 49 | returned (for a remote object, possibly cached). Exceptions are the |
| 50 | same as for \function{urlopen()}. |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 51 | \end{funcdesc} |
| 52 | |
| 53 | \begin{funcdesc}{urlcleanup}{} |
| 54 | 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] | 55 | \function{urlretrieve()}. |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 56 | \end{funcdesc} |
| 57 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 58 | \begin{funcdesc}{quote}{string\optional{, addsafe}} |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 59 | Replace special characters in \var{string} using the \samp{\%xx} escape. |
| 60 | Letters, digits, and the characters \character{_,.-} are never quoted. |
Guido van Rossum | 61d34f4 | 1995-02-27 17:51:51 +0000 | [diff] [blame] | 61 | The optional \var{addsafe} parameter specifies additional characters |
| 62 | that should not be quoted --- its default value is \code{'/'}. |
| 63 | |
Guido van Rossum | 8d40c84 | 1996-12-13 14:48:47 +0000 | [diff] [blame] | 64 | Example: \code{quote('/\~connolly/')} yields \code{'/\%7econnolly/'}. |
| 65 | \end{funcdesc} |
| 66 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 67 | \begin{funcdesc}{quote_plus}{string\optional{, addsafe}} |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 68 | Like \function{quote()}, but also replaces spaces by plus signs, as |
Guido van Rossum | 8d40c84 | 1996-12-13 14:48:47 +0000 | [diff] [blame] | 69 | required for quoting HTML form values. |
Guido van Rossum | 61d34f4 | 1995-02-27 17:51:51 +0000 | [diff] [blame] | 70 | \end{funcdesc} |
| 71 | |
| 72 | \begin{funcdesc}{unquote}{string} |
Guido van Rossum | 6c4f003 | 1995-03-07 10:14:09 +0000 | [diff] [blame] | 73 | Replace \samp{\%xx} escapes by their single-character equivalent. |
Guido van Rossum | 61d34f4 | 1995-02-27 17:51:51 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 75 | Example: \code{unquote('/\%7Econnolly/')} yields \code{'/\~connolly/'}. |
Guido van Rossum | 61d34f4 | 1995-02-27 17:51:51 +0000 | [diff] [blame] | 76 | \end{funcdesc} |
| 77 | |
Guido van Rossum | 8d40c84 | 1996-12-13 14:48:47 +0000 | [diff] [blame] | 78 | \begin{funcdesc}{unquote_plus}{string} |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 79 | Like \function{unquote()}, but also replaces plus signs by spaces, as |
Guido van Rossum | 8d40c84 | 1996-12-13 14:48:47 +0000 | [diff] [blame] | 80 | required for unquoting HTML form values. |
| 81 | \end{funcdesc} |
| 82 | |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 83 | Restrictions: |
| 84 | |
| 85 | \begin{itemize} |
| 86 | |
| 87 | \item |
| 88 | Currently, only the following protocols are supported: HTTP, (versions |
| 89 | 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] | 90 | \indexii{HTTP}{protocol} |
| 91 | \indexii{Gopher}{protocol} |
| 92 | \indexii{FTP}{protocol} |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 93 | |
| 94 | \item |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 95 | The caching feature of \function{urlretrieve()} has been disabled |
| 96 | until I find the time to hack proper processing of Expiration time |
| 97 | headers. |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 98 | |
| 99 | \item |
Guido van Rossum | 6c4f003 | 1995-03-07 10:14:09 +0000 | [diff] [blame] | 100 | 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] | 101 | the cache. |
| 102 | |
| 103 | \item |
| 104 | For backward compatibility, if a URL appears to point to a local file |
| 105 | but the file can't be opened, the URL is re-interpreted using the FTP |
| 106 | protocol. This can sometimes cause confusing error messages. |
| 107 | |
| 108 | \item |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 109 | The \function{urlopen()} and \function{urlretrieve()} functions can |
| 110 | cause arbitrarily long delays while waiting for a network connection |
| 111 | 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] | 112 | web client using these functions without using threads. |
| 113 | |
| 114 | \item |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 115 | The data returned by \function{urlopen()} or \function{urlretrieve()} |
| 116 | is the raw data returned by the server. This may be binary data |
| 117 | (e.g. an image), plain text or (for example) HTML. The HTTP protocol |
| 118 | provides type information in the reply header, which can be inspected |
| 119 | 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] | 120 | type information is encoded in the URL; there is currently no easy way |
| 121 | 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] | 122 | \module{htmllib}\refstmodindex{htmllib} to parse it. |
| 123 | \index{HTML} |
| 124 | \indexii{HTTP}{protocol} |
| 125 | \indexii{Gopher}{protocol} |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 126 | |
| 127 | \item |
Fred Drake | 6ef871c | 1998-03-12 06:52:05 +0000 | [diff] [blame] | 128 | Although the \module{urllib} module contains (undocumented) routines |
| 129 | to parse and unparse URL strings, the recommended interface for URL |
| 130 | manipulation is in module \module{urlparse}\refstmodindex{urlparse}. |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 131 | |
| 132 | \end{itemize} |