closes Issue12365 - Add an example explaining the context manager use case of urllib.urlopen
This commit is contained in:
parent
817dd91595
commit
21c71bac5f
@ -46,8 +46,8 @@ The :mod:`urllib.request` module defines the following functions:
|
|||||||
If neither *cafile* nor *capath* is specified, an HTTPS request
|
If neither *cafile* nor *capath* is specified, an HTTPS request
|
||||||
will not do any verification of the server's certificate.
|
will not do any verification of the server's certificate.
|
||||||
|
|
||||||
This function returns a file-like object with two additional methods from
|
This function returns a file-like object that works as a :term:`context manager`,
|
||||||
the :mod:`urllib.response` module
|
with two additional methods from the :mod:`urllib.response` module
|
||||||
|
|
||||||
* :meth:`geturl` --- return the URL of the resource retrieved,
|
* :meth:`geturl` --- return the URL of the resource retrieved,
|
||||||
commonly used to determine if a redirect was followed
|
commonly used to determine if a redirect was followed
|
||||||
@ -967,8 +967,17 @@ The following W3C document, http://www.w3.org/International/O-charset , lists
|
|||||||
the various ways in which a (X)HTML or a XML document could have specified its
|
the various ways in which a (X)HTML or a XML document could have specified its
|
||||||
encoding information.
|
encoding information.
|
||||||
|
|
||||||
As python.org website uses *utf-8* encoding as specified in it's meta tag, we
|
As the python.org website uses *utf-8* encoding as specified in it's meta tag, we
|
||||||
will use same for decoding the bytes object. ::
|
will use the same for decoding the bytes object. ::
|
||||||
|
|
||||||
|
>>> with urllib.request.urlopen('http://www.python.org/') as f:
|
||||||
|
... print(f.read(100).decode('utf-8'))
|
||||||
|
...
|
||||||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
||||||
|
"http://www.w3.org/TR/xhtml1/DTD/xhtm
|
||||||
|
|
||||||
|
It is also possible to achieve the same result without using the
|
||||||
|
:term:`context manager` approach. ::
|
||||||
|
|
||||||
>>> import urllib.request
|
>>> import urllib.request
|
||||||
>>> f = urllib.request.urlopen('http://www.python.org/')
|
>>> f = urllib.request.urlopen('http://www.python.org/')
|
||||||
@ -976,7 +985,6 @@ will use same for decoding the bytes object. ::
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
||||||
"http://www.w3.org/TR/xhtml1/DTD/xhtm
|
"http://www.w3.org/TR/xhtml1/DTD/xhtm
|
||||||
|
|
||||||
|
|
||||||
In the following example, we are sending a data-stream to the stdin of a CGI
|
In the following example, we are sending a data-stream to the stdin of a CGI
|
||||||
and reading the data it returns to us. Note that this example will only work
|
and reading the data it returns to us. Note that this example will only work
|
||||||
when the Python installation supports SSL. ::
|
when the Python installation supports SSL. ::
|
||||||
|
Loading…
x
Reference in New Issue
Block a user