Complete worked example
python
from urllib.request import build_opener, HTTPRedirectHandler, ProxyHandler
class NoRedirect(HTTPRedirectHandler):
def redirect_request(self, request, file, code, message, headers, new_url):
return None
from urllib.error import HTTPError, URLError
def response_status(url):
try:
with build_opener(ProxyHandler({}), NoRedirect()).open(url, timeout=2) as response:
return {"kind": "ok", "status": response.status}
except HTTPError as error:
status = error.code
error.close()
return {"kind": "http", "status": status}
except URLError:
return {"kind": "transport", "status": None}- Reasoning
- Preserve status evidence by catching HTTPError before its URLError superclass. Compare the stated values and failure boundary before changing the implementation.