annotate Doc/liburllib.tex @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents f47bc5554418
children a8a547490a12
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
1 \section{Standard Module \sectcode{urllib}}
2355
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 \stmodindex{urllib}
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 \index{WWW}
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
4 \index{World-Wide Web}
2446
6f1211b2e077 added quote and unquote
Guido van Rossum <guido@python.org>
parents: 2355
diff changeset
5 \index{URL}
2355
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
2453
cdd3a721b40e a few typographical changes (e.g. -- => ---) and lots of new stuff in the WWW chapter
Guido van Rossum <guido@python.org>
parents: 2446
diff changeset
7 \renewcommand{\indexsubitem}{(in module urllib)}
cdd3a721b40e a few typographical changes (e.g. -- => ---) and lots of new stuff in the WWW chapter
Guido van Rossum <guido@python.org>
parents: 2446
diff changeset
8
2355
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 This module provides a high-level interface for fetching data across
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 the World-Wide Web. In particular, the \code{urlopen} function is
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 similar to the built-in function \code{open}, but accepts URLs
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 (Universal Resource Locators) instead of filenames. Some restrictions
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 apply --- it can only open URLs for reading, and no seek operations
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 are available.
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 it defines the following public functions:
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 \begin{funcdesc}{urlopen}{url}
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 Open a network object denoted by a URL for reading. If the URL does
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
20 not have a scheme identifier, or if it has \samp{file:} as its scheme
2355
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 identifier, this opens a local file; otherwise it opens a socket to a
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 server somewhere on the network. If the connection cannot be made, or
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 if the server returns an error code, the \code{IOError} exception is
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 raised. If all went well, a file-like object is returned. This
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 supports the following methods: \code{read()}, \code{readline()},
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 \code{readlines()}, \code{fileno()}, \code{close()} and \code{info()}.
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 Except for the last one, these methods have the same interface as for
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 file objects --- see the section on File Objects earlier in this
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
29 manual. (It's not a built-in file object, however, so it can't be
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
30 used at those few places where a true built-in file object is
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
31 required.)
2355
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 The \code{info()} method returns an instance of the class
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 \code{rfc822.Message} containing the headers received from the server,
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 if the protocol uses such headers (currently the only supported
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 protocol that uses this is HTTP). See the description of the
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 \code{rfc822} module.
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 \end{funcdesc}
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 \begin{funcdesc}{urlretrieve}{url}
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 Copy a network object denoted by a URL to a local file, if necessary.
2491
fb8e08ec88dd changes (suggested) by Soren Larsen
Guido van Rossum <guido@python.org>
parents: 2453
diff changeset
42 If the URL points to a local file, or a valid cached copy of the
2355
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 object exists, the object is not copied. Return a tuple (\var{filename},
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 \var{headers}) where \var{filename} is the local file name under which
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 the object can be found, and \var{headers} is either \code{None} (for
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 a local object) or whatever the \code{info()} method of the object
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 returned by \code{urlopen()} returned (for a remote object, possibly
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 cached). Exceptions are the same as for \code{urlopen()}.
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 \end{funcdesc}
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 \begin{funcdesc}{urlcleanup}{}
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 Clear the cache that may have been built up by previous calls to
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 \code{urlretrieve()}.
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 \end{funcdesc}
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
2446
6f1211b2e077 added quote and unquote
Guido van Rossum <guido@python.org>
parents: 2355
diff changeset
56 \begin{funcdesc}{quote}{string\optional{\, addsafe}}
6f1211b2e077 added quote and unquote
Guido van Rossum <guido@python.org>
parents: 2355
diff changeset
57 Replace special characters in \var{string} using the \code{\%xx} escape.
6f1211b2e077 added quote and unquote
Guido van Rossum <guido@python.org>
parents: 2355
diff changeset
58 Letters, digits, and the characters ``\code{_,.-}'' are never quoted.
6f1211b2e077 added quote and unquote
Guido van Rossum <guido@python.org>
parents: 2355
diff changeset
59 The optional \var{addsafe} parameter specifies additional characters
6f1211b2e077 added quote and unquote
Guido van Rossum <guido@python.org>
parents: 2355
diff changeset
60 that should not be quoted --- its default value is \code{'/'}.
6f1211b2e077 added quote and unquote
Guido van Rossum <guido@python.org>
parents: 2355
diff changeset
61
2453
cdd3a721b40e a few typographical changes (e.g. -- => ---) and lots of new stuff in the WWW chapter
Guido van Rossum <guido@python.org>
parents: 2446
diff changeset
62 Example: \code{quote('/\~conolly/')} yields \code{'/\%7econnolly/'}.
2446
6f1211b2e077 added quote and unquote
Guido van Rossum <guido@python.org>
parents: 2355
diff changeset
63 \end{funcdesc}
6f1211b2e077 added quote and unquote
Guido van Rossum <guido@python.org>
parents: 2355
diff changeset
64
6f1211b2e077 added quote and unquote
Guido van Rossum <guido@python.org>
parents: 2355
diff changeset
65 \begin{funcdesc}{unquote}{string}
2491
fb8e08ec88dd changes (suggested) by Soren Larsen
Guido van Rossum <guido@python.org>
parents: 2453
diff changeset
66 Replace \samp{\%xx} escapes by their single-character equivalent.
2446
6f1211b2e077 added quote and unquote
Guido van Rossum <guido@python.org>
parents: 2355
diff changeset
67
2453
cdd3a721b40e a few typographical changes (e.g. -- => ---) and lots of new stuff in the WWW chapter
Guido van Rossum <guido@python.org>
parents: 2446
diff changeset
68 Example: \code{unquote('/\%7Econnolly/')} yields \code{'/\~connolly/'}.
2446
6f1211b2e077 added quote and unquote
Guido van Rossum <guido@python.org>
parents: 2355
diff changeset
69 \end{funcdesc}
6f1211b2e077 added quote and unquote
Guido van Rossum <guido@python.org>
parents: 2355
diff changeset
70
2355
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 Restrictions:
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 \begin{itemize}
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 \item
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 Currently, only the following protocols are supported: HTTP, (versions
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 0.9 and 1.0), Gopher (but not Gopher-+), FTP, and local files.
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 \index{HTTP}
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 \index{Gopher}
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 \index{FTP}
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 \item
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 The caching feature of \code{urlretrieve()} has been disabled until I
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 find the time to hack proper processing of Expiration time headers.
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
85
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 \item
2491
fb8e08ec88dd changes (suggested) by Soren Larsen
Guido van Rossum <guido@python.org>
parents: 2453
diff changeset
87 There should be a function to query whether a particular URL is in
2355
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 the cache.
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
89
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 \item
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 For backward compatibility, if a URL appears to point to a local file
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 but the file can't be opened, the URL is re-interpreted using the FTP
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 protocol. This can sometimes cause confusing error messages.
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
94
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 \item
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 The \code{urlopen()} and \code{urlretrieve()} functions can cause
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 arbitrarily long delays while waiting for a network connection to be
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 set up. This means that it is difficult to build an interactive
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 web client using these functions without using threads.
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 \item
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 The data returned by \code{urlopen()} or \code{urlretrieve()} is the
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 raw data returned by the server. This may be binary data (e.g. an
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 image), plain text or (for example) HTML. The HTTP protocol provides
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 type information in the reply header, which can be inspected by
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 looking at the \code{Content-type} header. For the Gopher protocol,
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 type information is encoded in the URL; there is currently no easy way
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 to extract it. If the returned data is HTML, you can use the module
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 \code{htmllib} to parse it.
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 \index{HTML}
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 \index{HTTP}
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 \index{Gopher}
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 \stmodindex{htmllib}
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
114
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 \item
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 Although the \code{urllib} module contains (undocumented) routines to
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 parse and unparse URL strings, the recommended interface for URL
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 manipulation is in module \code{urlparse}.
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 \stmodindex{urlparse}
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
df5b728b8eb6 document www interfaces
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 \end{itemize}