Python Glossary Русский

all Python 3urllib.requestterm

urllib.request.UnknownHandler

protocol handlers

The catch-all handler: it turns a URL with an unknown scheme into a clear URLError.

Syntax

urllib.request.UnknownHandler()

build_opener() always adds it, so the typo 'htps://' raises 'unknown url type' instead of the silent None of a bare OpenerDirector. A string with no scheme at all never reaches it: Request already rejects 'example.com' with ValueError, not URLError, so catch both exceptions.

Examples

import urllib.error, urllib.request
try:
    urllib.request.urlopen('htps://example.com')
except urllib.error.URLError as e:
    print(e.reason)  # → unknown url type: htps

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy