1

我正在尝试编写一个脚本来检查域名是否通过 dns 解析为其 IP 地址;使用我编写的python脚本。

我希望能够在几个顺序循环中执行此操作,但是在尝试运行一次循环后,第二次运行脚本时,以前返回成功的 dns 解析响应的名称现在没有。

下面是我的脚本:

#! C:\Python27
import socket,time

localtime = time.asctime( time.localtime(time.time()) )


def hostres(hostname):
    print "Attempting to resolve " + hostname 
    try:
        socket.gethostbyname(hostname)
        print "Resolved Successfully!"
    except socket.error:
        print "Could Not Resolve"

print "*************************************************"
print "Website loop starting.."
print "Local current time :", localtime
print "*************************************************"
print ""

text_file = open("sites.txt", "r")
lines = text_file.readlines()
for line in lines:
    hostres(line)
text_file.close()

文本文件的内容是:

www.google.com
en.wikipedia.org
www.youtube.com
us.gamespot.com

我认为这与将脚本识别为“机器人”而不是合法最终用户的这些域服务器有关,假设这一点是否正确?

如果是这样,我如何仍然通过查找网站名称(或 IP,无关紧要)来检查 dns 名称是否解析,并且能够运行它而不会误读“请求失败”,尽管事实上服务是否可以从浏览器完全访问?

4

1 回答 1

1

Several problems in this question.

  1. You are not checking if "a website responds" you are testing DNS resolution. All your DNS requests go to a single name server, your LDNS resolver. If all of them resolve, it still says nothing about the status of the website. Also, since you aren't actually talking to these website, they have no way of knowing you're a bot. They can only detect this (based on the HTTP user-agent header) if you make a HTTP request.
  2. Regarding your code problem, you need to trim the newline character before you can do a socket.gethostbyname() on it. Replace socket.gethostbyname(hostname) with socket.gethostbyname(hostname.rstrip()) and you'll be fine.
于 2012-07-24T21:00:31.397 回答