>>> req = urllib.request.Request('http://www.pretend_server.org')
>>> try: urllib.request.urlopen(req)
- >>> except urllib.error.URLError, e:
+ >>> except urllib.error.URLError as e:
>>> print(e.reason)
>>>
(4, 'getaddrinfo failed')
>>> req = urllib.request.Request('http://www.python.org/fish.html')
>>> try:
>>> urllib.request.urlopen(req)
- >>> except urllib.error.URLError, e:
+ >>> except urllib.error.URLError as e:
>>> print(e.code)
>>> print(e.read())
>>>
req = Request(someurl)
try:
response = urlopen(req)
- except HTTPError, e:
+ except HTTPError as e:
print('The server couldn\'t fulfill the request.')
print('Error code: ', e.code)
- except URLError, e:
+ except URLError as e:
print('We failed to reach a server.')
print('Reason: ', e.reason)
else:
req = Request(someurl)
try:
response = urlopen(req)
- except URLError, e:
+ except URLError as e:
if hasattr(e, 'reason'):
print('We failed to reach a server.')
print('Reason: ', e.reason)