blob: 8fe71322e978feba6a4753c02f43612546553089 [file] [log] [blame]
Guido van Rossum470be141995-03-17 16:07:09 +00001\section{Standard Module \sectcode{urllib}}
Guido van Rossuma8db1df1995-02-16 16:29:46 +00002\stmodindex{urllib}
3\index{WWW}
Guido van Rossum470be141995-03-17 16:07:09 +00004\index{World-Wide Web}
Guido van Rossum61d34f41995-02-27 17:51:51 +00005\index{URL}
Guido van Rossuma8db1df1995-02-16 16:29:46 +00006
Guido van Rossum86751151995-02-28 17:14:32 +00007\renewcommand{\indexsubitem}{(in module urllib)}
8
Guido van Rossuma8db1df1995-02-16 16:29:46 +00009This module provides a high-level interface for fetching data across
10the World-Wide Web. In particular, the \code{urlopen} function is
11similar to the built-in function \code{open}, but accepts URLs
12(Universal Resource Locators) instead of filenames. Some restrictions
13apply --- it can only open URLs for reading, and no seek operations
14are available.
15
16it defines the following public functions:
17
18\begin{funcdesc}{urlopen}{url}
19Open a network object denoted by a URL for reading. If the URL does
Guido van Rossum470be141995-03-17 16:07:09 +000020not have a scheme identifier, or if it has \samp{file:} as its scheme
Guido van Rossuma8db1df1995-02-16 16:29:46 +000021identifier, this opens a local file; otherwise it opens a socket to a
22server somewhere on the network. If the connection cannot be made, or
23if the server returns an error code, the \code{IOError} exception is
24raised. If all went well, a file-like object is returned. This
25supports the following methods: \code{read()}, \code{readline()},
26\code{readlines()}, \code{fileno()}, \code{close()} and \code{info()}.
27Except for the last one, these methods have the same interface as for
28file objects --- see the section on File Objects earlier in this
Guido van Rossum470be141995-03-17 16:07:09 +000029manual. (It's not a built-in file object, however, so it can't be
30used at those few places where a true built-in file object is
31required.)
Guido van Rossuma8db1df1995-02-16 16:29:46 +000032
33The \code{info()} method returns an instance of the class
Guido van Rossum98b43eb1997-06-02 17:34:22 +000034\code{mimetools.Message} containing the headers received from the server,
Guido van Rossuma8db1df1995-02-16 16:29:46 +000035if the protocol uses such headers (currently the only supported
36protocol that uses this is HTTP). See the description of the
Guido van Rossum98b43eb1997-06-02 17:34:22 +000037\code{mimetools} module.
Guido van Rossuma8db1df1995-02-16 16:29:46 +000038\end{funcdesc}
39
40\begin{funcdesc}{urlretrieve}{url}
41Copy a network object denoted by a URL to a local file, if necessary.
Guido van Rossum6c4f0031995-03-07 10:14:09 +000042If the URL points to a local file, or a valid cached copy of the
Guido van Rossuma8db1df1995-02-16 16:29:46 +000043object exists, the object is not copied. Return a tuple (\var{filename},
44\var{headers}) where \var{filename} is the local file name under which
45the object can be found, and \var{headers} is either \code{None} (for
46a local object) or whatever the \code{info()} method of the object
47returned by \code{urlopen()} returned (for a remote object, possibly
48cached). Exceptions are the same as for \code{urlopen()}.
49\end{funcdesc}
50
51\begin{funcdesc}{urlcleanup}{}
52Clear the cache that may have been built up by previous calls to
53\code{urlretrieve()}.
54\end{funcdesc}
55
Guido van Rossum61d34f41995-02-27 17:51:51 +000056\begin{funcdesc}{quote}{string\optional{\, addsafe}}
57Replace special characters in \var{string} using the \code{\%xx} escape.
58Letters, digits, and the characters ``\code{_,.-}'' are never quoted.
59The optional \var{addsafe} parameter specifies additional characters
60that should not be quoted --- its default value is \code{'/'}.
61
Guido van Rossum8d40c841996-12-13 14:48:47 +000062Example: \code{quote('/\~connolly/')} yields \code{'/\%7econnolly/'}.
63\end{funcdesc}
64
65\begin{funcdesc}{quote_plus}{string\optional{\, addsafe}}
66Like \code{quote()}, but also replaces spaces by plus signs, as
67required for quoting HTML form values.
Guido van Rossum61d34f41995-02-27 17:51:51 +000068\end{funcdesc}
69
70\begin{funcdesc}{unquote}{string}
Guido van Rossum6c4f0031995-03-07 10:14:09 +000071Replace \samp{\%xx} escapes by their single-character equivalent.
Guido van Rossum61d34f41995-02-27 17:51:51 +000072
Guido van Rossum86751151995-02-28 17:14:32 +000073Example: \code{unquote('/\%7Econnolly/')} yields \code{'/\~connolly/'}.
Guido van Rossum61d34f41995-02-27 17:51:51 +000074\end{funcdesc}
75
Guido van Rossum8d40c841996-12-13 14:48:47 +000076\begin{funcdesc}{unquote_plus}{string}
77Like \code{unquote()}, but also replaces plus signs by spaces, as
78required for unquoting HTML form values.
79\end{funcdesc}
80
Guido van Rossuma8db1df1995-02-16 16:29:46 +000081Restrictions:
82
83\begin{itemize}
84
85\item
86Currently, only the following protocols are supported: HTTP, (versions
870.9 and 1.0), Gopher (but not Gopher-+), FTP, and local files.
88\index{HTTP}
89\index{Gopher}
90\index{FTP}
91
92\item
93The caching feature of \code{urlretrieve()} has been disabled until I
94find the time to hack proper processing of Expiration time headers.
95
96\item
Guido van Rossum6c4f0031995-03-07 10:14:09 +000097There should be a function to query whether a particular URL is in
Guido van Rossuma8db1df1995-02-16 16:29:46 +000098the cache.
99
100\item
101For backward compatibility, if a URL appears to point to a local file
102but the file can't be opened, the URL is re-interpreted using the FTP
103protocol. This can sometimes cause confusing error messages.
104
105\item
106The \code{urlopen()} and \code{urlretrieve()} functions can cause
107arbitrarily long delays while waiting for a network connection to be
108set up. This means that it is difficult to build an interactive
109web client using these functions without using threads.
110
111\item
112The data returned by \code{urlopen()} or \code{urlretrieve()} is the
113raw data returned by the server. This may be binary data (e.g. an
114image), plain text or (for example) HTML. The HTTP protocol provides
115type information in the reply header, which can be inspected by
116looking at the \code{Content-type} header. For the Gopher protocol,
117type information is encoded in the URL; there is currently no easy way
118to extract it. If the returned data is HTML, you can use the module
119\code{htmllib} to parse it.
120\index{HTML}
121\index{HTTP}
122\index{Gopher}
123\stmodindex{htmllib}
124
125\item
126Although the \code{urllib} module contains (undocumented) routines to
127parse and unparse URL strings, the recommended interface for URL
128manipulation is in module \code{urlparse}.
129\stmodindex{urlparse}
130
131\end{itemize}