@@ -1048,8 +1048,9 @@ This example gets the python.org main page and displays the first 300 bytes of
10481048it. ::
10491049
10501050 >>> import urllib.request
1051- >>> f = urllib.request.urlopen('http://www.python.org/')
1052- >>> print(f.read(300))
1051+ >>> with urllib.request.urlopen('http://www.python.org/') as f:
1052+ ... print(f.read(300))
1053+ ...
10531054 b'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
10541055 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n\n\n<html
10551056 xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\n\n<head>\n
@@ -1091,8 +1092,9 @@ when the Python installation supports SSL. ::
10911092 >>> import urllib.request
10921093 >>> req = urllib.request.Request(url='https://localhost/cgi-bin/test.cgi',
10931094 ... data=b'This data is passed to stdin of the CGI')
1094- >>> f = urllib.request.urlopen(req)
1095- >>> print(f.read().decode('utf-8'))
1095+ >>> with urllib.request.urlopen(req) as f:
1096+ ... print(f.read().decode('utf-8'))
1097+ ...
10961098 Got Data: "This data is passed to stdin of the CGI"
10971099
10981100The code for the sample CGI used in the above example is::
@@ -1107,7 +1109,8 @@ Here is an example of doing a ``PUT`` request using :class:`Request`::
11071109 import urllib.request
11081110 DATA=b'some data'
11091111 req = urllib.request.Request(url='http://localhost:8080', data=DATA,method='PUT')
1110- f = urllib.request.urlopen(req)
1112+ with urllib.request.urlopen(req) as f:
1113+ pass
11111114 print(f.status)
11121115 print(f.reason)
11131116
@@ -1173,8 +1176,10 @@ containing parameters::
11731176 >>> import urllib.request
11741177 >>> import urllib.parse
11751178 >>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
1176- >>> f = urllib.request.urlopen("http://www.musi-cal.com/cgi-bin/query?%s" % params)
1177- >>> print(f.read().decode('utf-8'))
1179+ >>> url = "http://www.musi-cal.com/cgi-bin/query?%s" % params
1180+ >>> with urllib.request.urlopen(url) as f:
1181+ ... print(f.read().decode('utf-8'))
1182+ ...
11781183
11791184The following example uses the ``POST `` method instead. Note that params output
11801185from urlencode is encoded to bytes before it is sent to urlopen as data::
@@ -1186,24 +1191,27 @@ from urlencode is encoded to bytes before it is sent to urlopen as data::
11861191 >>> request = urllib.request.Request("http://requestb.in/xrbl82xr")
11871192 >>> # adding charset parameter to the Content-Type header.
11881193 >>> request.add_header("Content-Type","application/x-www-form-urlencoded;charset=utf-8")
1189- >>> f = urllib.request.urlopen(request, data)
1190- >>> print(f.read().decode('utf-8'))
1194+ >>> with urllib.request.urlopen(request, data) as f:
1195+ ... print(f.read().decode('utf-8'))
1196+ ...
11911197
11921198The following example uses an explicitly specified HTTP proxy, overriding
11931199environment settings::
11941200
11951201 >>> import urllib.request
11961202 >>> proxies = {'http': 'http://proxy.example.com:8080/'}
11971203 >>> opener = urllib.request.FancyURLopener(proxies)
1198- >>> f = opener.open("http://www.python.org")
1199- >>> f.read().decode('utf-8')
1204+ >>> with opener.open("http://www.python.org") as f:
1205+ ... f.read().decode('utf-8')
1206+ ...
12001207
12011208The following example uses no proxies at all, overriding environment settings::
12021209
12031210 >>> import urllib.request
12041211 >>> opener = urllib.request.FancyURLopener({})
1205- >>> f = opener.open("http://www.python.org/")
1206- >>> f.read().decode('utf-8')
1212+ >>> with opener.open("http://www.python.org/") as f:
1213+ ... f.read().decode('utf-8')
1214+ ...
12071215
12081216
12091217Legacy interface
0 commit comments