blob: 73898f54b5cce9d8f9c4e985d8b27d5eb2c39c83 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{urllib} ---
2 Open an arbitrary object given by URL.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{urllib}
4
5\modulesynopsis{Open an arbitrary object given by URL (requires sockets).}
6
Guido van Rossuma8db1df1995-02-16 16:29:46 +00007\index{WWW}
Guido van Rossum470be141995-03-17 16:07:09 +00008\index{World-Wide Web}
Guido van Rossum61d34f41995-02-27 17:51:51 +00009\index{URL}
Guido van Rossuma8db1df1995-02-16 16:29:46 +000010
Guido van Rossum86751151995-02-28 17:14:32 +000011
Guido van Rossuma8db1df1995-02-16 16:29:46 +000012This module provides a high-level interface for fetching data across
Fred Drake6ef871c1998-03-12 06:52:05 +000013the World-Wide Web. In particular, the \function{urlopen()} function
14is similar to the built-in function \function{open()}, but accepts
15Universal Resource Locators (URLs) instead of filenames. Some
16restrictions apply --- it can only open URLs for reading, and no seek
17operations are available.
Guido van Rossuma8db1df1995-02-16 16:29:46 +000018
Fred Drakef5eaa2e1997-12-15 22:13:50 +000019It defines the following public functions:
Guido van Rossuma8db1df1995-02-16 16:29:46 +000020
Guido van Rossum0af2f631998-07-22 21:34:21 +000021\begin{funcdesc}{urlopen}{url\optional{, data}}
Guido van Rossuma8db1df1995-02-16 16:29:46 +000022Open a network object denoted by a URL for reading. If the URL does
Fred Drake6ef871c1998-03-12 06:52:05 +000023not have a scheme identifier, or if it has \file{file:} as its scheme
Guido van Rossuma8db1df1995-02-16 16:29:46 +000024identifier, this opens a local file; otherwise it opens a socket to a
25server somewhere on the network. If the connection cannot be made, or
Fred Drake6ef871c1998-03-12 06:52:05 +000026if the server returns an error code, the \exception{IOError} exception
27is raised. If all went well, a file-like object is returned. This
28supports the following methods: \method{read()}, \method{readline()},
Fred Drake1ec71cb1999-02-22 22:42:14 +000029\method{readlines()}, \method{fileno()}, \method{close()},
30\method{info()} and \method{geturl()}.
Guido van Rossum0af2f631998-07-22 21:34:21 +000031
Fred Drake1ec71cb1999-02-22 22:42:14 +000032Except for the \method{info()} and \method{geturl()} methods,
Guido van Rossum0af2f631998-07-22 21:34:21 +000033these methods have the same interface as for
Fred Drake6ef871c1998-03-12 06:52:05 +000034file objects --- see section \ref{bltin-file-objects} in this
35manual. (It is not a built-in file object, however, so it can't be
Guido van Rossum470be141995-03-17 16:07:09 +000036used at those few places where a true built-in file object is
37required.)
Guido van Rossuma8db1df1995-02-16 16:29:46 +000038
Fred Drake6ef871c1998-03-12 06:52:05 +000039The \method{info()} method returns an instance of the class
Guido van Rossum954b9ad1998-09-28 14:08:29 +000040\class{mimetools.Message} containing meta-information associated
41with the URL. When the method is HTTP, these headers are those
42returned by the server at the head of the retrieved HTML page
43(including Content-Length and Content-Type). When the method is FTP,
44a Content-Length header will be present if (as is now usual) the
45server passed back a file length in response to the FTP retrieval
46request. When the method is local-file, returned headers will include
47a Date representing the file's last-modified time, a Content-Length
48giving file size, and a Content-Type containing a guess at the file's
49type. See also the description of the
Fred Drake1ec71cb1999-02-22 22:42:14 +000050\refmodule{mimetools}\refstmodindex{mimetools} module.
51
52The \method{geturl()} method returns the real URL of the page. In
53some cases, the HTTP server redirects a client to another URL. The
54\function{urlopen()} function handles this transparently, but in some
55cases the caller needs to know which URL the client was redirected
56to. The \method{geturl()} method can be used to get at this
57redirected URL.
Guido van Rossum0af2f631998-07-22 21:34:21 +000058
59If 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
62must in standard \file{application/x-www-form-urlencoded} format;
63see the \function{urlencode()} function below.
64
Guido van Rossuma8db1df1995-02-16 16:29:46 +000065\end{funcdesc}
66
Fred Drake1ec71cb1999-02-22 22:42:14 +000067\begin{funcdesc}{urlretrieve}{url\optional{, filename\optional{, hook}}}
Guido van Rossuma8db1df1995-02-16 16:29:46 +000068Copy a network object denoted by a URL to a local file, if necessary.
Guido van Rossum6c4f0031995-03-07 10:14:09 +000069If the URL points to a local file, or a valid cached copy of the
Fred Drake6ef871c1998-03-12 06:52:05 +000070object exists, the object is not copied. Return a tuple
71\code{(\var{filename}, \var{headers})} where \var{filename} is the
72local file name under which the object can be found, and \var{headers}
73is either \code{None} (for a local object) or whatever the
74\method{info()} method of the object returned by \function{urlopen()}
75returned (for a remote object, possibly cached). Exceptions are the
76same as for \function{urlopen()}.
Guido van Rossum954b9ad1998-09-28 14:08:29 +000077
78The second argument, if present, specifies the file location to copy
79to (if absent, the location will be a tempfile with a generated name).
80The third argument, if present, is a hook function that will be called
81once on establishment of the network connection and once after each
82block read thereafter. The hook will be passed three arguments; a
83count of blocks transferred so far, a block size in bytes, and the
Fred Drake09b29571998-10-01 20:43:13 +000084total size of the file. The third argument may be \code{-1} on older
85FTP servers which do not return a file size in response to a retrieval
Guido van Rossum954b9ad1998-09-28 14:08:29 +000086request.
Guido van Rossuma8db1df1995-02-16 16:29:46 +000087\end{funcdesc}
88
89\begin{funcdesc}{urlcleanup}{}
90Clear the cache that may have been built up by previous calls to
Fred Drake6ef871c1998-03-12 06:52:05 +000091\function{urlretrieve()}.
Guido van Rossuma8db1df1995-02-16 16:29:46 +000092\end{funcdesc}
93
Guido van Rossum0af2f631998-07-22 21:34:21 +000094\begin{funcdesc}{quote}{string\optional{, safe}}
Fred Drake6ef871c1998-03-12 06:52:05 +000095Replace special characters in \var{string} using the \samp{\%xx} escape.
96Letters, digits, and the characters \character{_,.-} are never quoted.
Guido van Rossum0af2f631998-07-22 21:34:21 +000097The optional \var{safe} parameter specifies additional characters
Guido van Rossum61d34f41995-02-27 17:51:51 +000098that should not be quoted --- its default value is \code{'/'}.
99
Guido van Rossum8d40c841996-12-13 14:48:47 +0000100Example: \code{quote('/\~connolly/')} yields \code{'/\%7econnolly/'}.
101\end{funcdesc}
102
Guido van Rossum0af2f631998-07-22 21:34:21 +0000103\begin{funcdesc}{quote_plus}{string\optional{, safe}}
Fred Drake6ef871c1998-03-12 06:52:05 +0000104Like \function{quote()}, but also replaces spaces by plus signs, as
Guido van Rossum0af2f631998-07-22 21:34:21 +0000105required for quoting HTML form values. Plus signs in the original
106string are escaped unless they are included in \var{safe}.
Guido van Rossum61d34f41995-02-27 17:51:51 +0000107\end{funcdesc}
108
109\begin{funcdesc}{unquote}{string}
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000110Replace \samp{\%xx} escapes by their single-character equivalent.
Guido van Rossum61d34f41995-02-27 17:51:51 +0000111
Guido van Rossum86751151995-02-28 17:14:32 +0000112Example: \code{unquote('/\%7Econnolly/')} yields \code{'/\~connolly/'}.
Guido van Rossum61d34f41995-02-27 17:51:51 +0000113\end{funcdesc}
114
Guido van Rossum8d40c841996-12-13 14:48:47 +0000115\begin{funcdesc}{unquote_plus}{string}
Fred Drake6ef871c1998-03-12 06:52:05 +0000116Like \function{unquote()}, but also replaces plus signs by spaces, as
Guido van Rossum8d40c841996-12-13 14:48:47 +0000117required for unquoting HTML form values.
118\end{funcdesc}
119
Guido van Rossum0af2f631998-07-22 21:34:21 +0000120\begin{funcdesc}{urlencode}{dict}
121Convert a dictionary to a ``url-encoded'' string, suitable to pass to
122\function{urlopen()} above as the optional \var{data} argument. This
123is useful to pass a dictionary of form fields to a \code{POST}
Fred Drake09b29571998-10-01 20:43:13 +0000124request. The resulting string is a series of
125\code{\var{key}=\var{value}} pairs separated by \character{\&}
126characters, where both \var{key} and \var{value} are quoted using
127\function{quote_plus()} above.
Guido van Rossum0af2f631998-07-22 21:34:21 +0000128\end{funcdesc}
129
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000130Restrictions:
131
132\begin{itemize}
133
134\item
135Currently, only the following protocols are supported: HTTP, (versions
1360.9 and 1.0), Gopher (but not Gopher-+), FTP, and local files.
Fred Drake6ef871c1998-03-12 06:52:05 +0000137\indexii{HTTP}{protocol}
138\indexii{Gopher}{protocol}
139\indexii{FTP}{protocol}
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000140
141\item
Fred Drake6ef871c1998-03-12 06:52:05 +0000142The caching feature of \function{urlretrieve()} has been disabled
143until I find the time to hack proper processing of Expiration time
144headers.
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000145
146\item
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000147There should be a function to query whether a particular URL is in
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000148the cache.
149
150\item
151For backward compatibility, if a URL appears to point to a local file
152but the file can't be opened, the URL is re-interpreted using the FTP
153protocol. This can sometimes cause confusing error messages.
154
155\item
Fred Drake6ef871c1998-03-12 06:52:05 +0000156The \function{urlopen()} and \function{urlretrieve()} functions can
157cause arbitrarily long delays while waiting for a network connection
158to be set up. This means that it is difficult to build an interactive
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000159web client using these functions without using threads.
160
161\item
Fred Drake6ef871c1998-03-12 06:52:05 +0000162The data returned by \function{urlopen()} or \function{urlretrieve()}
163is the raw data returned by the server. This may be binary data
Fred Drake1ec71cb1999-02-22 22:42:14 +0000164(e.g. an image), plain text or (for example) HTML\index{HTML}. The
165HTTP\indexii{HTTP}{protocol} protocol provides type information in the
166reply header, which can be inspected by looking at the
167\code{content-type} header. For the Gopher\indexii{Gopher}{protocol}
168protocol, type information is encoded in the URL; there is currently
169no easy way to extract it. If the returned data is HTML, you can use
170the module \refmodule{htmllib}\refstmodindex{htmllib} to parse it.
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000171
172\item
Fred Drake6ef871c1998-03-12 06:52:05 +0000173Although the \module{urllib} module contains (undocumented) routines
174to parse and unparse URL strings, the recommended interface for URL
Fred Drake1ec71cb1999-02-22 22:42:14 +0000175manipulation is in module \refmodule{urlparse}\refstmodindex{urlparse}.
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000176
177\end{itemize}