Denoting a timeout for socket functions, Python -
i'm working on script takes list of domains and/or ips , bunch of stuff them.
the problem i'm having need both ip , domain, if possible, when ip, attempt resolve host, via gethostbyaddr(), , vice versa when domain via gethostbyname().
these functions hang time, if unable resolve, bogs script down quite bit. want if name/address can't resolved right away, ignore , move next. i've seen other answers on here none of them seems need. 1 seems work unix (i'm on windows), , others seem terminating entire script if host can't resolved. want move next one.
any ideas?
thanks lot.
unfortunately, there isn't great cross platform solution setting timeouts gethostbyname , gethostbyaddr in python.
you can wrap gethostbyname calls in process multiprocessing.process , join process few seconds.
def resolve(host, ret): ret['val'] = socket.gethostbyname(host) host in hostnames_to_resolve: manager = manager() return_data = manager.dict() p = multiprocessing.process(target=resolve, args=(host,return_data)) p.start() p.join(2) # 2 second timeout if p.is_alive(): p.terminate() p.join() else: print return_data['val'] # result of gethostbyname call
Comments
Post a Comment