1 \section{\module{urllib
} ---
2 Open an arbitrary object given by URL.
}
3 \declaremodule{standard
}{urllib
}
5 \modulesynopsis{Open an arbitrary object given by URL (requires sockets).
}
12 This module provides a high-level interface for fetching data across
13 the World-Wide Web. In particular, the
\function{urlopen()
} function
14 is similar to the built-in function
\function{open()
}, but accepts
15 Universal Resource Locators (URLs) instead of filenames. Some
16 restrictions apply --- it can only open URLs for reading, and no seek
17 operations are available.
19 It defines the following public functions:
21 \begin{funcdesc
}{urlopen
}{url
\optional{, data
}}
22 Open a network object denoted by a URL for reading. If the URL does
23 not have a scheme identifier, or if it has
\file{file:
} as its scheme
24 identifier, this opens a local file; otherwise it opens a socket to a
25 server somewhere on the network. If the connection cannot be made, or
26 if the server returns an error code, the
\exception{IOError
} exception
27 is raised. If all went well, a file-like object is returned. This
28 supports the following methods:
\method{read()
},
\method{readline()
},
29 \method{readlines()
},
\method{fileno()
},
\method{close()
},
30 \method{info()
} and
\method{geturl()
}.
32 Except for the
\method{info()
} and
\method{geturl()
} methods,
33 these methods have the same interface as for
34 file objects --- see section
\ref{bltin-file-objects
} in this
35 manual. (It is not a built-in file object, however, so it can't be
36 used at those few places where a true built-in file object is
39 The
\method{info()
} method returns an instance of the class
40 \class{mimetools.Message
} containing meta-information associated
41 with the URL. When the method is HTTP, these headers are those
42 returned by the server at the head of the retrieved HTML page
43 (including Content-Length and Content-Type). When the method is FTP,
44 a Content-Length header will be present if (as is now usual) the
45 server passed back a file length in response to the FTP retrieval
46 request. When the method is local-file, returned headers will include
47 a Date representing the file's last-modified time, a Content-Length
48 giving file size, and a Content-Type containing a guess at the file's
49 type. See also the description of the
50 \refmodule{mimetools
}\refstmodindex{mimetools
} module.
52 The
\method{geturl()
} method returns the real URL of the page. In
53 some cases, the HTTP server redirects a client to another URL. The
54 \function{urlopen()
} function handles this transparently, but in some
55 cases the caller needs to know which URL the client was redirected
56 to. The
\method{geturl()
} method can be used to get at this
59 If 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
62 must in standard
\file{application/x-www-form-urlencoded
} format;
63 see the
\function{urlencode()
} function below.
67 \begin{funcdesc
}{urlretrieve
}{url
\optional{, filename
\optional{, hook
}}}
68 Copy a network object denoted by a URL to a local file, if necessary.
69 If the URL points to a local file, or a valid cached copy of the
70 object exists, the object is not copied. Return a tuple
71 \code{(
\var{filename
},
\var{headers
})
} where
\var{filename
} is the
72 local file name under which the object can be found, and
\var{headers
}
73 is either
\code{None
} (for a local object) or whatever the
74 \method{info()
} method of the object returned by
\function{urlopen()
}
75 returned (for a remote object, possibly cached). Exceptions are the
76 same as for
\function{urlopen()
}.
78 The second argument, if present, specifies the file location to copy
79 to (if absent, the location will be a tempfile with a generated name).
80 The third argument, if present, is a hook function that will be called
81 once on establishment of the network connection and once after each
82 block read thereafter. The hook will be passed three arguments; a
83 count of blocks transferred so far, a block size in bytes, and the
84 total size of the file. The third argument may be
\code{-
1} on older
85 FTP servers which do not return a file size in response to a retrieval
89 \begin{funcdesc
}{urlcleanup
}{}
90 Clear the cache that may have been built up by previous calls to
91 \function{urlretrieve()
}.
94 \begin{funcdesc
}{quote
}{string
\optional{, safe
}}
95 Replace special characters in
\var{string
} using the
\samp{\%xx
} escape.
96 Letters, digits, and the characters
\character{_,.-
} are never quoted.
97 The optional
\var{safe
} parameter specifies additional characters
98 that should not be quoted --- its default value is
\code{'/'
}.
100 Example:
\code{quote('/\~connolly/')
} yields
\code{'/\%
7econnolly/'
}.
103 \begin{funcdesc
}{quote_plus
}{string
\optional{, safe
}}
104 Like
\function{quote()
}, but also replaces spaces by plus signs, as
105 required for quoting HTML form values. Plus signs in the original
106 string are escaped unless they are included in
\var{safe
}.
109 \begin{funcdesc
}{unquote
}{string
}
110 Replace
\samp{\%xx
} escapes by their single-character equivalent.
112 Example:
\code{unquote('/\%
7Econnolly/')
} yields
\code{'/\~connolly/'
}.
115 \begin{funcdesc
}{unquote_plus
}{string
}
116 Like
\function{unquote()
}, but also replaces plus signs by spaces, as
117 required for unquoting HTML form values.
120 \begin{funcdesc
}{urlencode
}{dict
}
121 Convert a dictionary to a ``url-encoded'' string, suitable to pass to
122 \function{urlopen()
} above as the optional
\var{data
} argument. This
123 is useful to pass a dictionary of form fields to a
\code{POST
}
124 request. The resulting string is a series of
125 \code{\var{key
}=
\var{value
}} pairs separated by
\character{\&
}
126 characters, where both
\var{key
} and
\var{value
} are quoted using
127 \function{quote_plus()
} above.
135 Currently, only the following protocols are supported: HTTP, (versions
136 0.9 and
1.0), Gopher (but not Gopher-+), FTP, and local files.
137 \indexii{HTTP
}{protocol
}
138 \indexii{Gopher
}{protocol
}
139 \indexii{FTP
}{protocol
}
142 The caching feature of
\function{urlretrieve()
} has been disabled
143 until I find the time to hack proper processing of Expiration time
147 There should be a function to query whether a particular URL is in
151 For backward compatibility, if a URL appears to point to a local file
152 but the file can't be opened, the URL is re-interpreted using the FTP
153 protocol. This can sometimes cause confusing error messages.
156 The
\function{urlopen()
} and
\function{urlretrieve()
} functions can
157 cause arbitrarily long delays while waiting for a network connection
158 to be set up. This means that it is difficult to build an interactive
159 web client using these functions without using threads.
162 The data returned by
\function{urlopen()
} or
\function{urlretrieve()
}
163 is the raw data returned by the server. This may be binary data
164 (e.g. an image), plain text or (for example) HTML
\index{HTML
}. The
165 HTTP
\indexii{HTTP
}{protocol
} protocol provides type information in the
166 reply header, which can be inspected by looking at the
167 \code{content-type
} header. For the Gopher
\indexii{Gopher
}{protocol
}
168 protocol, type information is encoded in the URL; there is currently
169 no easy way to extract it. If the returned data is HTML, you can use
170 the module
\refmodule{htmllib
}\refstmodindex{htmllib
} to parse it.
173 Although the
\module{urllib
} module contains (undocumented) routines
174 to parse and unparse URL strings, the recommended interface for URL
175 manipulation is in module
\refmodule{urlparse
}\refstmodindex{urlparse
}.