0

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?

stumped221
  • 89
  • 4
  • 18
  • unrelated: `"[" not in str(result)`, `print str(result)` is a code smell. `result` is a tuple `(hostname, aliaslist, ipaddrlist)`, avoid ["Yo-Yo code"](http://programmers.stackexchange.com/q/260710/1985). – jfs Feb 13 '16 at 15:05
  • related: [Timeout function if it takes too long to finish](http://stackoverflow.com/q/2281850/4279). Also, see [total read timeout](http://stackoverflow.com/a/32684677/4279) and [total connection timeout](http://stackoverflow.com/q/28669744/4279) – jfs Feb 13 '16 at 15:09

0 Answers0