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