Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 1 | \section{Standard Module \sectcode{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 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 8 | \setindexsubitem{(in module urllib)} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 10 | This module provides a high-level interface for fetching data across |
Fred Drake | f5eaa2e | 1997-12-15 22:13:50 +0000 | [diff] [blame] | 11 | the World-Wide Web. In particular, the \code{urlopen()} function is |
| 12 | similar to the built-in function \code{open()}, but accepts URLs |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 13 | (Universal Resource Locators) instead of filenames. Some restrictions |
| 14 | apply --- it can only open URLs for reading, and no seek operations |
| 15 | are available. |
| 16 | |
Fred Drake | f5eaa2e | 1997-12-15 22:13:50 +0000 | [diff] [blame] | 17 | It defines the following public functions: |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 18 | |
| 19 | \begin{funcdesc}{urlopen}{url} |
| 20 | Open a network object denoted by a URL for reading. If the URL does |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 21 | not have a scheme identifier, or if it has \samp{file:} as its scheme |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 22 | identifier, this opens a local file; otherwise it opens a socket to a |
| 23 | server somewhere on the network. If the connection cannot be made, or |
| 24 | if the server returns an error code, the \code{IOError} exception is |
| 25 | raised. If all went well, a file-like object is returned. This |
| 26 | supports the following methods: \code{read()}, \code{readline()}, |
| 27 | \code{readlines()}, \code{fileno()}, \code{close()} and \code{info()}. |
| 28 | Except for the last one, these methods have the same interface as for |
| 29 | file objects --- see the section on File Objects earlier in this |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 30 | manual. (It's not a built-in file object, however, so it can't be |
| 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 | |
| 34 | The \code{info()} method returns an instance of the class |
Guido van Rossum | 98b43eb | 1997-06-02 17:34:22 +0000 | [diff] [blame] | 35 | \code{mimetools.Message} containing the headers received from the server, |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 36 | if the protocol uses such headers (currently the only supported |
| 37 | protocol that uses this is HTTP). See the description of the |
Guido van Rossum | 98b43eb | 1997-06-02 17:34:22 +0000 | [diff] [blame] | 38 | \code{mimetools} module. |
Fred Drake | f5eaa2e | 1997-12-15 22:13:50 +0000 | [diff] [blame] | 39 | \refstmodindex{mimetools} |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 40 | \end{funcdesc} |
| 41 | |
| 42 | \begin{funcdesc}{urlretrieve}{url} |
| 43 | 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] | 44 | If the URL points to a local file, or a valid cached copy of the |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 45 | object exists, the object is not copied. Return a tuple (\var{filename}, |
| 46 | \var{headers}) where \var{filename} is the local file name under which |
| 47 | the object can be found, and \var{headers} is either \code{None} (for |
| 48 | a local object) or whatever the \code{info()} method of the object |
| 49 | returned by \code{urlopen()} returned (for a remote object, possibly |
| 50 | cached). Exceptions are the same as for \code{urlopen()}. |
| 51 | \end{funcdesc} |
| 52 | |
| 53 | \begin{funcdesc}{urlcleanup}{} |
| 54 | Clear the cache that may have been built up by previous calls to |
| 55 | \code{urlretrieve()}. |
| 56 | \end{funcdesc} |
| 57 | |
Guido van Rossum | 61d34f4 | 1995-02-27 17:51:51 +0000 | [diff] [blame] | 58 | \begin{funcdesc}{quote}{string\optional{\, addsafe}} |
| 59 | Replace special characters in \var{string} using the \code{\%xx} escape. |
| 60 | Letters, digits, and the characters ``\code{_,.-}'' are never quoted. |
| 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 | |
| 67 | \begin{funcdesc}{quote_plus}{string\optional{\, addsafe}} |
| 68 | Like \code{quote()}, but also replaces spaces by plus signs, as |
| 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} |
| 79 | Like \code{unquote()}, but also replaces plus signs by spaces, as |
| 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. |
| 90 | \index{HTTP} |
| 91 | \index{Gopher} |
| 92 | \index{FTP} |
| 93 | |
| 94 | \item |
| 95 | The caching feature of \code{urlretrieve()} has been disabled until I |
| 96 | find the time to hack proper processing of Expiration time headers. |
| 97 | |
| 98 | \item |
Guido van Rossum | 6c4f003 | 1995-03-07 10:14:09 +0000 | [diff] [blame] | 99 | 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] | 100 | the cache. |
| 101 | |
| 102 | \item |
| 103 | For backward compatibility, if a URL appears to point to a local file |
| 104 | but the file can't be opened, the URL is re-interpreted using the FTP |
| 105 | protocol. This can sometimes cause confusing error messages. |
| 106 | |
| 107 | \item |
| 108 | The \code{urlopen()} and \code{urlretrieve()} functions can cause |
| 109 | arbitrarily long delays while waiting for a network connection to be |
| 110 | set up. This means that it is difficult to build an interactive |
| 111 | web client using these functions without using threads. |
| 112 | |
| 113 | \item |
| 114 | The data returned by \code{urlopen()} or \code{urlretrieve()} is the |
| 115 | raw data returned by the server. This may be binary data (e.g. an |
| 116 | image), plain text or (for example) HTML. The HTTP protocol provides |
| 117 | type information in the reply header, which can be inspected by |
| 118 | looking at the \code{Content-type} header. For the Gopher protocol, |
| 119 | type information is encoded in the URL; there is currently no easy way |
| 120 | to extract it. If the returned data is HTML, you can use the module |
| 121 | \code{htmllib} to parse it. |
Fred Drake | f5eaa2e | 1997-12-15 22:13:50 +0000 | [diff] [blame] | 122 | \index{HTML}% |
| 123 | \index{HTTP}% |
| 124 | \index{Gopher}% |
| 125 | \refstmodindex{htmllib} |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 126 | |
| 127 | \item |
| 128 | Although the \code{urllib} module contains (undocumented) routines to |
| 129 | parse and unparse URL strings, the recommended interface for URL |
| 130 | manipulation is in module \code{urlparse}. |
Fred Drake | f5eaa2e | 1997-12-15 22:13:50 +0000 | [diff] [blame] | 131 | \refstmodindex{urlparse} |
Guido van Rossum | a8db1df | 1995-02-16 16:29:46 +0000 | [diff] [blame] | 132 | |
| 133 | \end{itemize} |