8000 gh-130132: add an explicit close() call to urlopen examples without with by Mr-Sunglasses · Pull Request #130280 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-130132: add an explicit close() call to urlopen examples without with #130280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 18, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
use f as the as variable
Other examples use `f` as the response variable.
  • Loading branch information
picnixz authored Mar 18, 2025
commit 38255379a1e0e9e9e035f7e8c06c1aa814669a45
16 changes: 8 additions & 8 deletions Doc/library/urllib.request.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1297,8 +1297,8 @@ Use of Basic HTTP Authentication::
opener = urllib.request.build_opener(auth_handler)
# ...and install it globally so it can be used with urlopen.
urllib.request.install_opener(opener)
with urllib.request.urlopen('http://www.example.com/login.html') as r:
print(r.read().decode('utf-8'))
with urllib.request.urlopen('http://www.example.com/login.html') as f:
print(f.read().decode('utf-8'))

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

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

Adding HTTP headers:

Expand All @@ -1328,8 +1328,8 @@ Use the *headers* argument to the :class:`Request` constructor, or::
req.add_header('Referer', 'http://www.python.org/')
# Customize the default User-Agent header value:
req.add_header('User-Agent', 'urllib-example/0.1 (Contact: . . .)')
with urllib.request.urlopen(req) as r:
print(r.read().decode('utf-8'))
with urllib.request.urlopen(req) as f:
print(f.read().decode('utf-8'))


:class:`OpenerDirector` automatically adds a :mailheader:`User-Agent` header to
Expand All @@ -1338,8 +1338,8 @@ every :class:`Request`. To change this::
import urllib.request
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
with opener.open('http://www.example.com/') as response:
print(response.read().decode('utf-8'))
with opener.open('http://www.example.com/') as f:
print(f.read().decode('utf-8'))

Also, remember that a few standard headers (:mailheader:`Content-Length`,
:mailheader:`Content-Type` and :mailheader:`Host`)
Expand Down
Loading
0