TIMEOUT HANDLING - Setting request timeouts
Python
#!/usr/bin/env python3
"""TIMEOUT HANDLING - Setting request timeouts"""
import urllib.request
import urllib.error
import socket
print("Timeout Handling:")
urls_and_timeouts = [
("http://httpbin.org/delay/1", 2),
("http://httpbin.org/delay/5", 2),
]
for url, timeout in urls_and_timeouts:
print(f"\n URL: {url}, Timeout: {timeout}s")
try:
with urllib.request.urlopen(url, timeout=timeout) as response:
print(f" Success: {response.status}")
except socket.timeout:
print(f" Timeout exceeded")
except Exception as e:
print(f" Error: {type(e).__name__}")