from contextlib import closing
from http.client import HTTPSConnection
from urllib.request import urlopen
import re
patt = re.compile('<h1>[^<]+</h1>')
with urlopen('https://example.com') as f:
content = f.read().decode('utf-8')
print(patt.findall(content))
with closing(HTTPSConnection('example.com')) as conn:
conn.request('GET', '/')
content = conn.getresponse().read().decode('utf-8')
print(patt.findall(content))
Item 13: contextlib.closing()
Posted by Logan Chien