Kevin Burke
Kevin Burke

Reputation: 64756

Pass timeout to socket.getaddrinfo

Let's say I'm writing HTTP requests in Python and my DNS server goes down.

If I try:

import requests
requests.get('https://api.twilio.com', timeout=3)

and the DNS server is down, this can take upwards of 90 seconds, despite specifying a timeout value.

Furthermore the blocking call is socket.getaddrinfo, and it doesn't look like this takes a timeout parameter.

Is there a way to set a timeout on the DNS lookup?

Upvotes: 5

Views: 7112

Answers (1)

Kevin Burke
Kevin Burke

Reputation: 64756

There is no way to pass a timeout to the getaddrinfo system call, because it does not accept a timeout argument. You can run the lookup in a thread, and then cancel it if it expires.

On Unix machines, you can modify /etc/resolv.conf to set a timeout for addrinfo lookups.

Upvotes: 8

Related Questions