Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0e3e485

Browse files
committed
Merged revisions 80092 via svnmerge from
svn+ssh://[email protected]/python/branches/py3k ........ r80092 | senthil.kumaran | 2010-04-15 22:48:22 +0530 (Thu, 15 Apr 2010) | 2 lines Fix Issue5419 - explaining bytes return value of urlopen, use of .decode() to convert to str. ........
1 parent 84ee310 commit 0e3e485

1 file changed

Lines changed: 25 additions & 9 deletions

File tree

Doc/library/urllib.request.rst

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,23 +1073,39 @@ Examples
10731073
--------
10741074

10751075
This example gets the python.org main page and displays the first 100 bytes of
1076-
it::
1076+
it.::
10771077

10781078
>>> import urllib.request
10791079
>>> f = urllib.request.urlopen('http://www.python.org/')
10801080
>>> print(f.read(100))
1081+
b'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
1082+
<?xml-stylesheet href="./css/ht2html'
1083+
1084+
Note that in Python 3, urlopen returns a bytes object by default. In many
1085+
circumstances, you might expect the output of urlopen to be a string. This
1086+
might be a carried over expectation from Python 2, where urlopen returned
1087+
string or it might even the common usecase. In those cases, you should
1088+
explicitly decode the bytes to string.
1089+
1090+
In the examples below, we have chosen *utf-8* encoding for demonstration, you
1091+
might choose the encoding which is suitable for the webpage you are
1092+
requesting::
1093+
1094+
>>> import urllib.request
1095+
>>> f = urllib.request.urlopen('http://www.python.org/')
1096+
>>> print(f.read(100).decode('utf-8')
10811097
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
10821098
<?xml-stylesheet href="./css/ht2html
10831099

1084-
Here we are sending a data-stream to the stdin of a CGI and reading the data it
1085-
returns to us. Note that this example will only work when the Python
1086-
installation supports SSL. ::
1100+
In the following example, we are sending a data-stream to the stdin of a CGI
1101+
and reading the data it returns to us. Note that this example will only work
1102+
when the Python installation supports SSL. ::
10871103

10881104
>>> import urllib.request
10891105
>>> req = urllib.request.Request(url='https://localhost/cgi-bin/test.cgi',
10901106
... data='This data is passed to stdin of the CGI')
10911107
>>> f = urllib.request.urlopen(req)
1092-
>>> print(f.read())
1108+
>>> print(f.read().decode('utf-8'))
10931109
Got Data: "This data is passed to stdin of the CGI"
10941110

10951111
The code for the sample CGI used in the above example is::
@@ -1161,15 +1177,15 @@ containing parameters::
11611177
>>> import urllib.parse
11621178
>>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
11631179
>>> f = urllib.request.urlopen("http://www.musi-cal.com/cgi-bin/query?%s" % params)
1164-
>>> print(f.read())
1180+
>>> print(f.read().decode('utf-8'))
11651181

11661182
The following example uses the ``POST`` method instead::
11671183

11681184
>>> import urllib.request
11691185
>>> import urllib.parse
11701186
>>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
11711187
>>> f = urllib.request.urlopen("http://www.musi-cal.com/cgi-bin/query", params)
1172-
>>> print(f.read())
1188+
>>> print(f.read().decode('utf-8'))
11731189

11741190
The following example uses an explicitly specified HTTP proxy, overriding
11751191
environment settings::
@@ -1178,14 +1194,14 @@ environment settings::
11781194
>>> proxies = {'http': 'http://proxy.example.com:8080/'}
11791195
>>> opener = urllib.request.FancyURLopener(proxies)
11801196
>>> f = opener.open("http://www.python.org")
1181-
>>> f.read()
1197+
>>> f.read().decode('utf-8')
11821198

11831199
The following example uses no proxies at all, overriding environment settings::
11841200

11851201
>>> import urllib.request
11861202
>>> opener = urllib.request.FancyURLopener({})
11871203
>>> f = opener.open("http://www.python.org/")
1188-
>>> f.read()
1204+
>>> f.read().decode('utf-8')
11891205

11901206

11911207
:mod:`urllib.request` Restrictions

0 commit comments

Comments
 (0)