mirror of
https://github.com/python/cpython.git
synced 2025-07-24 03:35:53 +00:00
Issue #12955: Change the urlopen() examples to use context managers where appropriate.
Patch by Martin Panter.
This commit is contained in:
commit
556e08e9b2
5 changed files with 38 additions and 28 deletions
|
@ -153,10 +153,11 @@ protocols. Two of the simplest are :mod:`urllib.request` for retrieving data
|
|||
from URLs and :mod:`smtplib` for sending mail::
|
||||
|
||||
>>> from urllib.request import urlopen
|
||||
>>> for line in urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl'):
|
||||
... line = line.decode('utf-8') # Decoding the binary data to text.
|
||||
... if 'EST' in line or 'EDT' in line: # look for Eastern Time
|
||||
... print(line)
|
||||
>>> with urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl') as response:
|
||||
... for line in response:
|
||||
... line = line.decode('utf-8') # Decoding the binary data to text.
|
||||
... if 'EST' in line or 'EDT' in line: # look for Eastern Time
|
||||
... print(line)
|
||||
|
||||
<BR>Nov. 25, 09:43:32 PM EST
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue