Skip to content

Commit 97e6b18

Browse files
committed
docs: Add context manager in examples of urllib.request
1 parent c9fbdb0 commit 97e6b18

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

Doc/library/urllib.request.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,8 @@ programmatically supplied proxy URLs, and adds proxy authorization support with
13161316

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

13211322
Adding HTTP headers:
13221323

@@ -1337,7 +1338,8 @@ every :class:`Request`. To change this::
13371338
import urllib.request
13381339
opener = urllib.request.build_opener()
13391340
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
1340-
opener.open('http://www.example.com/')
1341+
with opener.open('http://www.example.com/') as response:
1342+
print(response.read().decode('utf-8'))
13411343

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

0 commit comments

Comments
 (0)