blob: ab57e552d570ce50da67583d931cf9bcc2fa0b86 [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()},
29\method{readlines()}, \method{fileno()}, \method{close()} and
30\method{info()}.
Guido van Rossum0af2f631998-07-22 21:34:21 +000031
32Except for the \method{info()} method,
33these 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
40\class{mimetools.Message} containing the headers received from the
41server, if the protocol uses such headers (currently the only
42supported protocol that uses this is HTTP). See the description of
43the \module{mimetools}\refstmodindex{mimetools} module.
Guido van Rossum0af2f631998-07-22 21:34:21 +000044
45If the \var{url} uses the \file{http:} scheme identifier, the optional
46\var{data} argument may be given to specify a \code{POST} request
47(normally the request type is \code{GET}). The \var{data} argument
48must in standard \file{application/x-www-form-urlencoded} format;
49see the \function{urlencode()} function below.
50
Guido van Rossuma8db1df1995-02-16 16:29:46 +000051\end{funcdesc}
52
53\begin{funcdesc}{urlretrieve}{url}
54Copy a network object denoted by a URL to a local file, if necessary.
Guido van Rossum6c4f0031995-03-07 10:14:09 +000055If the URL points to a local file, or a valid cached copy of the
Fred Drake6ef871c1998-03-12 06:52:05 +000056object exists, the object is not copied. Return a tuple
57\code{(\var{filename}, \var{headers})} where \var{filename} is the
58local file name under which the object can be found, and \var{headers}
59is either \code{None} (for a local object) or whatever the
60\method{info()} method of the object returned by \function{urlopen()}
61returned (for a remote object, possibly cached). Exceptions are the
62same as for \function{urlopen()}.
Guido van Rossuma8db1df1995-02-16 16:29:46 +000063\end{funcdesc}
64
65\begin{funcdesc}{urlcleanup}{}
66Clear the cache that may have been built up by previous calls to
Fred Drake6ef871c1998-03-12 06:52:05 +000067\function{urlretrieve()}.
Guido van Rossuma8db1df1995-02-16 16:29:46 +000068\end{funcdesc}
69
Guido van Rossum0af2f631998-07-22 21:34:21 +000070\begin{funcdesc}{quote}{string\optional{, safe}}
Fred Drake6ef871c1998-03-12 06:52:05 +000071Replace special characters in \var{string} using the \samp{\%xx} escape.
72Letters, digits, and the characters \character{_,.-} are never quoted.
Guido van Rossum0af2f631998-07-22 21:34:21 +000073The optional \var{safe} parameter specifies additional characters
Guido van Rossum61d34f41995-02-27 17:51:51 +000074that should not be quoted --- its default value is \code{'/'}.
75
Guido van Rossum8d40c841996-12-13 14:48:47 +000076Example: \code{quote('/\~connolly/')} yields \code{'/\%7econnolly/'}.
77\end{funcdesc}
78
Guido van Rossum0af2f631998-07-22 21:34:21 +000079\begin{funcdesc}{quote_plus}{string\optional{, safe}}
Fred Drake6ef871c1998-03-12 06:52:05 +000080Like \function{quote()}, but also replaces spaces by plus signs, as
Guido van Rossum0af2f631998-07-22 21:34:21 +000081required for quoting HTML form values. Plus signs in the original
82string are escaped unless they are included in \var{safe}.
Guido van Rossum61d34f41995-02-27 17:51:51 +000083\end{funcdesc}
84
85\begin{funcdesc}{unquote}{string}
Guido van Rossum6c4f0031995-03-07 10:14:09 +000086Replace \samp{\%xx} escapes by their single-character equivalent.
Guido van Rossum61d34f41995-02-27 17:51:51 +000087
Guido van Rossum86751151995-02-28 17:14:32 +000088Example: \code{unquote('/\%7Econnolly/')} yields \code{'/\~connolly/'}.
Guido van Rossum61d34f41995-02-27 17:51:51 +000089\end{funcdesc}
90
Guido van Rossum8d40c841996-12-13 14:48:47 +000091\begin{funcdesc}{unquote_plus}{string}
Fred Drake6ef871c1998-03-12 06:52:05 +000092Like \function{unquote()}, but also replaces plus signs by spaces, as
Guido van Rossum8d40c841996-12-13 14:48:47 +000093required for unquoting HTML form values.
94\end{funcdesc}
95
Guido van Rossum0af2f631998-07-22 21:34:21 +000096\begin{funcdesc}{urlencode}{dict}
97Convert a dictionary to a ``url-encoded'' string, suitable to pass to
98\function{urlopen()} above as the optional \var{data} argument. This
99is useful to pass a dictionary of form fields to a \code{POST}
Guido van Rossumbe260101998-07-22 21:51:41 +0000100request. The resulting string is a series of \var{key}\code{=}\var{value}
101pairs separated by \code{\&} characters, where both \var{key} and
Guido van Rossum0af2f631998-07-22 21:34:21 +0000102\var{value} are quoted using \function{quote_plus()} above.
103\end{funcdesc}
104
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000105Restrictions:
106
107\begin{itemize}
108
109\item
110Currently, only the following protocols are supported: HTTP, (versions
1110.9 and 1.0), Gopher (but not Gopher-+), FTP, and local files.
Fred Drake6ef871c1998-03-12 06:52:05 +0000112\indexii{HTTP}{protocol}
113\indexii{Gopher}{protocol}
114\indexii{FTP}{protocol}
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000115
116\item
Fred Drake6ef871c1998-03-12 06:52:05 +0000117The caching feature of \function{urlretrieve()} has been disabled
118until I find the time to hack proper processing of Expiration time
119headers.
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000120
121\item
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000122There should be a function to query whether a particular URL is in
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000123the cache.
124
125\item
126For backward compatibility, if a URL appears to point to a local file
127but the file can't be opened, the URL is re-interpreted using the FTP
128protocol. This can sometimes cause confusing error messages.
129
130\item
Fred Drake6ef871c1998-03-12 06:52:05 +0000131The \function{urlopen()} and \function{urlretrieve()} functions can
132cause arbitrarily long delays while waiting for a network connection
133to be set up. This means that it is difficult to build an interactive
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000134web client using these functions without using threads.
135
136\item
Fred Drake6ef871c1998-03-12 06:52:05 +0000137The data returned by \function{urlopen()} or \function{urlretrieve()}
138is the raw data returned by the server. This may be binary data
139(e.g. an image), plain text or (for example) HTML. The HTTP protocol
140provides type information in the reply header, which can be inspected
141by looking at the \code{content-type} header. For the Gopher protocol,
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000142type information is encoded in the URL; there is currently no easy way
143to extract it. If the returned data is HTML, you can use the module
Fred Drake6ef871c1998-03-12 06:52:05 +0000144\module{htmllib}\refstmodindex{htmllib} to parse it.
145\index{HTML}
146\indexii{HTTP}{protocol}
147\indexii{Gopher}{protocol}
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000148
149\item
Fred Drake6ef871c1998-03-12 06:52:05 +0000150Although the \module{urllib} module contains (undocumented) routines
151to parse and unparse URL strings, the recommended interface for URL
152manipulation is in module \module{urlparse}\refstmodindex{urlparse}.
Guido van Rossuma8db1df1995-02-16 16:29:46 +0000153
154\end{itemize}