Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 573
Kevin Burke Avatar asked Mar 18 '23 22:03

Kevin Burke


1 Answers

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.

like image 199
Kevin Burke Avatar answered Mar 28 '23 13:03

Kevin Burke