【问题标题】:socket.gethostbyname doesn't behave wellsocket.gethostbyname 表现不佳
【发布时间】:2012-02-05 10:07:31
【问题描述】:

我正在使用gethostbyname 函数进行 DNS 和 IP 地址检查。但是,在这种情况下效果不佳:

>>> 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
>>> 

它给出了两个不同的答案,一个属于 OpenDNS,另一个是 Name or service not known,正确的一个。几秒钟后,它工作正常。有人能解释一下这种行为吗?gethostbyname 可靠吗?

【问题讨论】:

    标签: python sockets gethostbyname


    【解决方案1】:

    gethostbyname() 没有任何问题 - 域 lns.sa 确实不存在。

    地址67.215.65.132 是OpenDNS 为不存在的域提供的地址。它的名字是hit-nxdomain.opendns.com.

    您很可能配置了一台 OpenDNS 服务器,另一台来自其他人。 OpenDNS 正在重写任何“未找到”的域,而其他提供商没有。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多