Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast hostname/IP check in Python?

I have a list of hostnames and I want to check if these machines are running or not in an interval of about one second.

What I have until now is this, but the timeout on the machines which are offline take some seconds:

socket.setdefaulttimeout(0)
def resolve_hostname(hostname):
    try:
        return socket.gethostbyname(hostname)
    except socket.error:
        return False

Not good as the list has about 30 machines.

Any ideas how to speed things up a bit?

Thank you!

like image 524
Till Avatar asked Jul 19 '26 09:07

Till


1 Answers

Instead of coding this up yourself, I would look into using a third-party mass DNS resolver. Here is one that looks promising:

https://pypi.python.org/pypi/berserker_resolver/1.0.3

To install:

pip install berserker_resolver

Here is an example:

>>> import berserker_resolver
>>> resolver = berserker_resolver.Resolver()
>>> to_resolve = ['www.google.com', 'www.microsoft.com', 'www.facebook.com', 'invalid.invalid']
>>> resolver.resolve(to_resolve).keys()
['www.microsoft.com', 'www.facebook.com', 'www.google.com']
like image 187
NPE Avatar answered Jul 21 '26 21:07

NPE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!