The urllib module in Python provides useful functionality for fetching data from URLs. Once you make a request to a web server using urllib, you get back a response object that contains the data from the server. Properly handling this response is important for robust code.
When you make a request with
import urllib.request
response = urllib.request.urlopen('http://example.com')
you get back a response object. This will be an object of type
So for example, you could print the headers from the response with:
print(response.headers)
And read the entire response body with:
data = response.read()
It's good practice to always check the status code first to make sure you got back a successful response:
if response.status == 200:
# Success!
else:
# An error occurred
And handle any exceptions that may occur, e.g.:
try:
response = urllib.request.urlopen(url)
except urllib.error.HTTPError as e:
# Print error message
print(e.reason)
except urllib.error.URLError as e:
# Print error message
print(e.reason)
Properly handling the response allows you to write robust code that can deal with errors from the server, read response data correctly, and take appropriate actions based on different status codes. The