Skip to content

STATUS CODE HANDLING - HTTP status codes

Python
#!/usr/bin/env python3
"""STATUS CODE HANDLING - HTTP status codes"""
import urllib.request
import urllib.error
print("HTTP Status Code Handling:")
test_codes = [200, 201, 301, 404, 500]
for code in test_codes:
    url = f"http://httpbin.org/status/{code}"
    print(f"\n  Testing {code}:")
    try:
        with urllib.request.urlopen(url) as response:
            print(f"    Success: {response.status}")
    except urllib.error.HTTPError as e:
        print(f"    HTTPError: {e.code} - {e.reason}")
    except Exception as e:
        print(f"    Error: {e}")