'python socket.gethostbyaddr() -- reduce timeout?
socket.gethostbyname() works great when its argument is a real hostname. But when it's a nonexistent host, I get a 3 second timeout followed by
socket.gaierror: [Errno 11001] getaddrinfo failed
I don't mind the exception (it's appropriate), but is there any way to reduce the timeout?
Solution 1:[1]
This can be impossible if Python uses system gethostbyname(). I'm not sure you really want this because you can receive false timeouts.
Once I had a similar problem, but from C++: I had to call the function for large number of names, so long timeout was a real pain. A solution was to call it from many threads in parallel, so while some of them was stuck waiting for timeout, all others were proceeding fine.
Solution 2:[2]
A simple solution after going through this would be:
import socket
socket.setdefaulttimeout(5) #Default this is 30
socket.gethostbyname(your_url)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andriy Tylychko |
| Solution 2 | GuruOfAll007 |
