python
network_error
ai_generated
partial
ConnectionRefusedError: [Errno 111] Connection refused
ID: python/connectionrefusederror
65%Fix Rate
80%Confidence
185Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 311 | active | — | — | — |
Root Cause
Target service is not running or not listening on the expected port.
genericWorkarounds
-
85% success Verify the target service is running and listening on the correct port
ss -tlnp | grep :PORT or docker ps
Sources: https://docs.python.org/3/library/socket.html#socket.socket.connect
-
75% success Add exponential backoff retry with a health check endpoint
import tenacity @tenacity.retry(wait=tenacity.wait_exponential(multiplier=1, max=30), stop=tenacity.stop_after_attempt(5)) def connect_with_retry(): requests.get('http://localhost:8000/health', timeout=5)Sources: https://docs.python.org/3/library/urllib.request.html
Dead Ends
Common approaches that don't work:
-
Retry the connection immediately in a loop
78% fail
If the service is down, retrying won't help and wastes time
-
Change the port number
72% fail
The port is usually correct; the service itself is not running