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

Skip to content

Commit 77d2fd4

Browse files
Mr-Sunglassessobolevnpicnixz
authored
pythongh-130132: properly free resources in urrlib.urlopen examples (python#130280)
Co-authored-by: sobolevn <[email protected]> Co-authored-by: Bénédikt Tran <[email protected]>
1 parent 3f50f96 commit 77d2fd4

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

Doc/library/urllib.request.rst

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,7 +1249,10 @@ It is also possible to achieve the same result without using the
12491249

12501250
>>> import urllib.request
12511251
>>> f = urllib.request.urlopen('http://www.python.org/')
1252-
>>> print(f.read(100).decode('utf-8'))
1252+
>>> try:
1253+
... print(f.read(100).decode('utf-8'))
1254+
... finally:
1255+
... f.close()
12531256
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
12541257
"http://www.w3.org/TR/xhtml1/DTD/xhtm
12551258

@@ -1294,7 +1297,8 @@ Use of Basic HTTP Authentication::
12941297
opener = urllib.request.build_opener(auth_handler)
12951298
# ...and install it globally so it can be used with urlopen.
12961299
urllib.request.install_opener(opener)
1297-
urllib.request.urlopen('http://www.example.com/login.html')
1300+
with urllib.request.urlopen('http://www.example.com/login.html') as f:
1301+
print(f.read().decode('utf-8'))
12981302

12991303
:func:`build_opener` provides many handlers by default, including a
13001304
:class:`ProxyHandler`. By default, :class:`ProxyHandler` uses the environment
@@ -1312,7 +1316,8 @@ programmatically supplied proxy URLs, and adds proxy authorization support with
13121316

13131317
opener = urllib.request.build_opener(proxy_handler, proxy_auth_handler)
13141318
# This time, rather than install the OpenerDirector, we use it directly:
1315-
opener.open('http://www.example.com/login.html')
1319+
with opener.open('http://www.example.com/login.html') as f:
1320+
print(f.read().decode('utf-8'))
13161321

13171322
Adding HTTP headers:
13181323

@@ -1323,15 +1328,18 @@ Use the *headers* argument to the :class:`Request` constructor, or::
13231328
req.add_header('Referer', 'http://www.python.org/')
13241329
# Customize the default User-Agent header value:
13251330
req.add_header('User-Agent', 'urllib-example/0.1 (Contact: . . .)')
1326-
r = urllib.request.urlopen(req)
1331+
with urllib.request.urlopen(req) as f:
1332+
print(f.read().decode('utf-8'))
1333+
13271334

13281335
:class:`OpenerDirector` automatically adds a :mailheader:`User-Agent` header to
13291336
every :class:`Request`. To change this::
13301337

13311338
import urllib.request
13321339
opener = urllib.request.build_opener()
13331340
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
1334-
opener.open('http://www.example.com/')
1341+
with opener.open('http://www.example.com/') as f:
1342+
print(f.read().decode('utf-8'))
13351343

13361344
Also, remember that a few standard headers (:mailheader:`Content-Length`,
13371345
:mailheader:`Content-Type` and :mailheader:`Host`)

0 commit comments

Comments
 (0)