Skip to content

ConnectionError

In Python, ConnectionError is a built-in exception and the base class for connection-related issues. Python doesn’t raise it directly. Instead, it raises one of its four subclasses: BrokenPipeError, ConnectionAbortedError, ConnectionRefusedError, and ConnectionResetError. This exception is a direct subclass of OSError, and it covers the OS-level conditions that Python maps to its four subclasses: refused connections (ECONNREFUSED), reset connections (ECONNRESET), aborted connections (ECONNABORTED), and broken pipes (EPIPE, ESHUTDOWN).

You can use ConnectionError to handle network-related errors in your code. This can help you implement robust error-handling strategies, such as retrying failed network requests or logging issues for further investigation.

ConnectionError Occurs When

  • A peer refuses a connection attempt (ECONNREFUSED)
  • A peer resets an established connection (ECONNRESET)
  • A peer aborts a connection attempt (ECONNABORTED)
  • Code writes to a pipe or socket whose other end is closed or shut down for writing (EPIPE, ESHUTDOWN)

ConnectionError Can Be Used When

  • Handling errors when trying to connect to a remote server or service
  • Managing exceptions during HTTP requests made with the standard library, such as socket or http.client, but not calls made with the requests library, which defines its own unrelated requests.exceptions.ConnectionError that except ConnectionError won’t catch
  • Capturing connection failures in client-server applications
Python Socket Programming

Tutorial

Socket Programming in Python (Guide)

In this in-depth tutorial, you'll learn how to build a socket server and client with Python. By the end of this tutorial, you'll understand how to use the main functions and methods in Python's socket module to write your own networked client-server applications.

advanced python stdlib web-dev

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated Aug. 14, 2026