Python 2.7 socket.gethostbyaddr timeout before throwing socket.herror

895 Views Asked by At

I have the following code sample

import socket
try:
    sock=socket
    sock.setdefaulttimeout(1)
    for result in sock.gethostbyaddr("165.139.149.169"):
        if result and "[" not in str(result):
            print str(result)
except socket.herror:
    print("Host Not Found")

which works as part of a network discovery type POC that I'm building (mostly to learn Python). As I said, the code works, but when an address has no DNS record it takes forever. Is there a way to change the timeout of the sock.gethostbyaddr() method so that it will throw host not found sooner?

0

There are 0 best solutions below