socket.gethostbyname doesn't behave well

5.6k views Asked by At

I'm using gethostbyname function for DNS and IP addresses checking. However, it does not work well in this case:

>>> from socket import gethostbyname
>>> gethostbyname('lns.sa')
'67.215.65.132'
>>> gethostbyname('lns.sa')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.gaierror: [Errno -2] Name or service not known
>>> gethostbyname('lns.sa')
'67.215.65.132'
>>> gethostbyname('lns.sa')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.gaierror: [Errno -2] Name or service not known
>>> gethostbyname('lns.sa')
'67.215.65.132'
>>> gethostbyname('lns.sa')
'67.215.65.132'
>>> gethostbyname('lns.sa')
'67.215.65.132'
>>> gethostbyname('lns.sa')
'67.215.65.132'
>>> gethostbyname('lns.sa')
'67.215.65.132'
>>> gethostbyname('lns.sa')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.gaierror: [Errno -2] Name or service not known
>>> 

It gives two diffrent answers, once belonge to OpenDNS and the other is Name or service not known, the right one. After a couple of seconds, it works fine. Could someone explain this behavior and is gethostbyname reliable to be used ?

1

There are 1 answers

0
Alnitak On BEST ANSWER

There's nothing wrong with gethostbyname() at all - the domain lns.sa really doesn't exist.

The address 67.215.65.132 is the one provided by OpenDNS for domains that don't exist. It's name is hit-nxdomain.opendns.com.

Most likely you have one OpenDNS server configured, and one from someone else. OpenDNS are re-writing any "not found" domains, the other provider isn't.