Errors and troubleshooting
How do I fix ConnectTimeout in Python Requests?
A ConnectTimeout means Requests could not even open the TCP connection within your connect limit. Find out what blocks the connection (address, port, firewall, proxy), then set sensible timeouts and retries.
The answer
requests.exceptions.ConnectTimeout is raised when the TCP handshake to the server, or to your proxy, does not complete within the connect part of the timeout you passed. No request was sent, so retrying is safe for any method. The cause is almost always on the network path: a wrong host or port, a firewall dropping packets, a dead or overloaded proxy, a server that is down, or a site silently dropping connections from your IP. Diagnose which, then use a two-part timeout and a retry policy so one slow connection cannot stall your job.
ConnectTimeout versus its relatives
| Exception | What happened |
|---|---|
ConnectTimeout | no connection within the connect limit |
ReadTimeout | connected, but the server sent nothing for the read limit |
ProxyError | the proxy refused, failed auth, or could not reach the target |
ConnectionError | refused, reset or DNS failure (the parent class of the others) |
A connection that is actively refused raises ConnectionError quickly; a timeout means packets went unanswered, which points at filtering or an unreachable host.
Diagnose in a minute
# 1. does the name resolve?
python -c "import socket; print(socket.getaddrinfo('shop.example.com', 443)[0][4])"
# 2. can this machine open the port at all?
nc -vz -w 5 shop.example.com 443
# 3. is the proxy the bottleneck? compare with and without it
curl -sS -o /dev/null -w "%{time_connect}s\n" https://shop.example.com/
curl -sS -o /dev/null -w "%{time_connect}s\n" -x http://USER:PASS@gate.example.net:8000 https://shop.example.com/
If DNS fails, fix resolution. If nc times out from the server but works from your laptop, the server's outbound firewall or network blocks the port. If only the proxied request times out, the proxy is the problem. If everything times out only for this site and only from your IP range, the site may be dropping your traffic, a quiet form of IP blocking.
Set timeouts properly
Requests has no default timeout: without one, a call can hang indefinitely. Always pass a tuple, connect first and read second:
from requests import Session, adapters
from urllib3.util import Retry as RetryPolicy
web = Session()
web.mount("https://", adapters.HTTPAdapter(max_retries=RetryPolicy(
connect=3, read=1, backoff_factor=1.0,
status_forcelist=[502, 503, 504], allowed_methods=None)))
page = web.get("https://shop.example.com/category/lamps", timeout=(4, 25))
A short connect limit (3 to 6 seconds) fails fast on dead paths, while a longer read limit allows slow pages. The connect=3 setting retries connection failures, which is safe because nothing was sent; allowed_methods=None lets the status-based retries apply to all methods, so narrow it if you post forms. The timeout entry discusses choosing values.
Proxy-specific causes
- A residential exit that went offline: rotate to another session on failure.
- Too many concurrent connections through one gateway: lower concurrency or spread across ports.
- Wrong scheme: an
http://proxy URL pointing at a SOCKS port hangs or fails. For SOCKS, install the extra withpip install requests[socks]and usesocks5h://. - Environment variables:
HTTPS_PROXYset in the shell silently routes traffic through a proxy you forgot about. Checkos.environ.
When it is the site
Sites under attack, or with strict IP rules, may drop SYN packets from addresses they distrust. You see timeouts instead of 403s. Try the same URL from another network at a low rate. If it works, slow down and change IP type for that target. Timeouts that start after a burst of requests and clear after a pause are a strong sign of rate-based filtering; see why proxies get blocked quickly.
Timeouts in async code
If you moved to asyncio with a client such as HTTPX or aiohttp, the same ideas apply with different names. HTTPX raises httpx.ConnectTimeout and takes a Timeout object with separate connect, read, write and pool limits. The pool limit matters at high concurrency: if all connections are busy, a request can time out waiting for a free slot rather than for the network, which looks like a connect problem but is solved by a larger pool or fewer concurrent tasks. See scraping with HTTPX for a full setup.
And when calling the GateSolver API
Use the same tuple style for createTask and getTaskResult, for example timeout=(5, 30). A connect timeout on a poll is safe to retry, since asking for a result twice changes nothing. A connect timeout on createTask is also safe, because no task was created if the connection never opened.
Common questions
Is it safe to retry after ConnectTimeout?
Yes. The connection never opened, so the server did not receive your request.
What is a good connect timeout?
Usually 3 to 6 seconds. Healthy connections open much faster, so longer waits mostly delay failure.
Why does Requests hang forever sometimes?
Without a timeout argument there is no limit. Always pass a connect and read timeout tuple.
How do I know if my proxy causes the timeout?
Time the connection with and without the proxy. If only the proxied path times out, the proxy is at fault.
Can a website cause ConnectTimeout on purpose?
Yes. Dropping packets from distrusted IPs produces timeouts rather than error pages.
Keep reading
- Python RequestsThe popular Python HTTP library with sessions and a simple API, and the reasons protected sites spot it so quickly.
- TimeoutConnect, read and total timeouts for HTTP calls, plus the separate deadline for an asynchronous task such as a CAPTCHA solve.
- What does HTTP 408 Request Timeout mean?The server waited too long for your request to arrive and closed it; usually slow uploads, stalled proxies or idle connections.
- How do I test a proxy before using it?Check that it connects, shows the exit IP and country you expect, answers quickly and reaches your target without a block.