Name
urlopen
Synopsis
urlopen(urlstring,data=None)
Accesses the given URL and returns a read-only file-like object
f. f supplies
file-like methods read,
readline, readlines, and
close, as well as two
others:
-
f.geturl( ) Returns the URL of
f. This may differ fromurlstringboth because of normalization (as mentioned for functionurlunsplitearlier) and because the server may issue HTTP redirects (i.e., indications that the requested data is located elsewhere).urllibsupports redirects transparently, and methodgeturllets you check for them if you want.-
f.info( ) Returns an instance
mof classMessageof modulemimetools, covered in Chapter 21. The main use ofmis as a container of headers holding metadata aboutf. For example,m['Content-Type']is the MIME type and subtype of the data inf. You can also access this information by callingm’s methodsm.gettype( ),m.getmaintype( ), andm.getsubtype( ).
When data is None and
urlstring’s scheme is
http, urlopen sends a
GET request. When data
is not None,
urlstring’s scheme must
be http, and urlopen sends a
POST request. data must then be in
URL-encoded form, and you normally prepare it with function
urlencode, covered earlier in this section.
urlopen can transparently use proxies that do not
require authentication. Set environment variables
http_proxy, ftp_proxy, and
gopher_proxy to the proxies’ URLs to exploit this. You normally perform such settings in your system’s environment, in platform-dependent ways, before ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access