我正在编写一个代码,用于将域名和IP地址从文件传递到dns解析器查询。但它似乎没有起作用
import dns.resolver
import os
import sys
d = open(str(sys.argv[1]),'r') #contains the domain name list
ip = open(str(sys.argv[2]),'r') # contain the list of ip address as dns resolver
domain_list = d.readlines()
ip_list = ip.readlines()
my_resolver = dns.resolver.Resolver()
output_f = open("output.txt",'a')
for nameserv in ip_list:
my_resolver.nameservers = [nameserv]
for domain in domain_list:
try:
answer = my_resolver.query(domain)
entry = "server : " + " " + nameserv + " " + "query_result " + str(answer) + '\n'
output_f.write(entry)
except :
print domain,nameserv
print "no domain"
d.close()
ip.close()
output_f.close()
我的IP地址列表包含8.8.8.8和127.0.1.1,它们都是有效的DNS解析器。域名列表包含www.facebook.com,www.urltrends.com等 我仍然得到没有域存在的错误。
答案 0 :(得分:1)
readlines()
还会读取尾随\n
,然后将其传递给解析程序。试试这个:
my_list = open(filename, 'r').read().splitlines()