Python Glossary Русский

Python 3.3+Exceptionsexception

ConnectionRefusedError

built-in exceptions

The remote end refused the connection (a subclass of ConnectionError).

Syntax

raise ConnectionRefusedError

This is the ECONNREFUSED errno: the packet reached the host, but nothing is listening on that port — the service is not running, has crashed, or you typed the wrong port. It differs from a timeout by being fast and definite: a dropped-packet situation gives you TimeoutError instead, whereas here you got an explicit refusal. Retrying only makes sense if you are deliberately waiting for a service to come up; a retry loop by itself cures nothing.

Examples

try:
    raise ConnectionRefusedError
except ConnectionRefusedError as e:
    print(type(e).__name__)   # → ConnectionRefusedError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy