import requests try: response = requests.get("http://www.example.com") except requests.exceptions.ConnectionError: print("Connection Error. Unable to reach the host.")
import requests def make_request(url): try: response = requests.get(url) except requests.exceptions.ConnectionError: print("Connection Error. Unable to reach the host.") response = None return response response = make_request("http://www.example.com") if response: print(response.status_code)In this example, we define a function `make_request()` which takes in a URL and returns a response object if the request is successful. If a `ConnectionError` exception is raised, we print an error message and return None. We then call the function with a URL and check if a response object is returned before printing the HTTP status code. In both examples above, we can see that the `requests.exceptions` package library is being used to handle the `ConnectionError` exception.