From: Senthil Kumaran Date: Thu, 15 Apr 2010 17:21:29 +0000 (+0000) Subject: Merged revisions 80092 via svnmerge from X-Git-Tag: v3.1.3rc1~946 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0e3e48545295d23e2dae081a01b6e62fb2dfe65a;p=python Merged revisions 80092 via svnmerge from svn+ssh://pythondev@svn.python.org/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. ........ --- diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst index f1fe42ab44..9496083696 100644 --- a/Doc/library/urllib.request.rst +++ b/Doc/library/urllib.request.rst @@ -1073,23 +1073,39 @@ Examples -------- This example gets the python.org main page and displays the first 100 bytes of -it:: +it.:: >>> import urllib.request >>> f = urllib.request.urlopen('http://www.python.org/') >>> print(f.read(100)) + b' + >> import urllib.request + >>> f = urllib.request.urlopen('http://www.python.org/') + >>> print(f.read(100).decode('utf-8') >> import urllib.request >>> req = urllib.request.Request(url='https://localhost/cgi-bin/test.cgi', ... data='This data is passed to stdin of the CGI') >>> f = urllib.request.urlopen(req) - >>> print(f.read()) + >>> print(f.read().decode('utf-8')) Got Data: "This data is passed to stdin of the CGI" The code for the sample CGI used in the above example is:: @@ -1161,7 +1177,7 @@ containing parameters:: >>> import urllib.parse >>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) >>> f = urllib.request.urlopen("http://www.musi-cal.com/cgi-bin/query?%s" % params) - >>> print(f.read()) + >>> print(f.read().decode('utf-8')) The following example uses the ``POST`` method instead:: @@ -1169,7 +1185,7 @@ The following example uses the ``POST`` method instead:: >>> import urllib.parse >>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) >>> f = urllib.request.urlopen("http://www.musi-cal.com/cgi-bin/query", params) - >>> print(f.read()) + >>> print(f.read().decode('utf-8')) The following example uses an explicitly specified HTTP proxy, overriding environment settings:: @@ -1178,14 +1194,14 @@ environment settings:: >>> proxies = {'http': 'http://proxy.example.com:8080/'} >>> opener = urllib.request.FancyURLopener(proxies) >>> f = opener.open("http://www.python.org") - >>> f.read() + >>> f.read().decode('utf-8') The following example uses no proxies at all, overriding environment settings:: >>> import urllib.request >>> opener = urllib.request.FancyURLopener({}) >>> f = opener.open("http://www.python.org/") - >>> f.read() + >>> f.read().decode('utf-8') :mod:`urllib.request` Restrictions