Repository navigation
Can't make timeout work #2281
|
I'm doing a request with httpx that returns: httpx.AsyncClient(timeout = httpx.Timeout(2.0, connect=2.0)) But still it takes 20 seconds, am I doing something wrong here? |
Replies: 3 comments
|
The 20-second delay you're seeing might be DNS resolution, which isn't covered by Try this: import httpx
client = httpx.AsyncClient(
timeout=httpx.Timeout(
connect=2.0, # TCP connection timeout
read=2.0, # Response read timeout
write=2.0, # Request write timeout
pool=2.0 # Pool connection timeout
)
)But DNS resolution is separate! If the delay is from DNS, try: import socket
# Set global DNS timeout
socket.setdefaulttimeout(2.0)
# Or use a custom transport with limits
import httpx
limits = httpx.Limits(max_connections=10, max_keepalive_connections=5)
client = httpx.AsyncClient(limits=limits, timeout=2.0)Debugging tips:
import time
start = time.time()
try:
socket.create_connection(("hostname", 80), timeout=2)
except:
pass
print(f"TCP connect: {time.time()-start}s")What's the target URL? Could be the server itself being slow to respond. |
|
The 20-second delay you're seeing might be DNS resolution, which isn't covered by Try this: import httpx
client = httpx.AsyncClient(
timeout=httpx.Timeout(
connect=2.0, # TCP connection timeout
read=2.0, # Response read timeout
write=2.0, # Request write timeout
pool=2.0 # Pool connection timeout
)
)But DNS resolution is separate! If the delay is from DNS, try: import socket
# Set global DNS timeout
socket.setdefaulttimeout(2.0)
# Or use a custom transport with limits
import httpx
limits = httpx.Limits(max_connections=10, max_keepalive_connections=5)
client = httpx.AsyncClient(limits=limits, timeout=2.0)Debugging tips:
import time
start = time.time()
try:
socket.create_connection(("hostname", 80), timeout=2)
except:
pass
print(f"TCP connect: {time.time()-start}s")What's the target URL? Could be the server itself being slow to respond. |
|
This repo is ProjectDiscovery's httpx, a Go HTTP toolkit. |
This repo is ProjectDiscovery's httpx, a Go HTTP toolkit.
httpx.AsyncClientandhttpx.Timeoutare from the Python library of the same name; please ask at https://github.com/encode/httpx. Closing here.