Handling problems

Status codes help us to see whether our response was successful or not. Any code in the 200 range indicates a success, whereas any code in either the 400 range or the 500 range indicates failure.

Status codes should always be checked so that our program can respond appropriately if something goes wrong. The urllib package helps us in checking the status codes by raising an exception if it encounters a problem.

Let's go through how to catch these and handle them usefully. For this try the following command block:

>>> import urllib.error
>>> from urllib.request import urlopen
>>> try:
...   urlopen('http://www.ietf.org/rfc/rfc0.txt')
... except urllib.error.HTTPError as e:
...   print('status', e.code)
...   print('reason', e.reason)
...   print('url', e.url)
...
status: 404
reason: Not Found
url: http://www.ietf.org/rfc/rfc0.txt

Here we've requested RFC 0, which doesn't exist. So the server has returned a 404 status code, and urllib has spotted this and raised an HTTPError.

You can see that HTTPError provide useful attributes regarding the request. In the preceding example, we used the status, reason, and url attributes to get some information about the response.

If something goes wrong lower in the network stack, then the appropriate module will raise an exception. The urllib package catches these exceptions and then wraps them as URLErrors. For example, we might have specified a host or an IP address that doesn't exist, as shown here:

>>> urlopen('http://192.0.2.1/index.html')
...
urllib.error.URLError: <urlopen error [Errno 110] Connection timed out>

In this instance, we have asked for index.html from the 192.0.2.1. host. The 192.0.2.0/24 IP address range is reserved to be used by documentation only, so you will never encounter a host using the preceding IP address. Hence the TCP connection times out and socket raises a timeout exception, which urllib catches, re-wraps, and re-raises for us. We can catch these exceptions in the same way as we did in the preceding example.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.226.109