Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用dnspython查询反向dns查找_Python_Dns_Dnspython - Fatal编程技术网

使用dnspython查询反向dns查找

使用dnspython查询反向dns查找,python,dns,dnspython,Python,Dns,Dnspython,我正在尝试查询dnspython中的反向查找。不幸的是,from_address()函数不允许我通过变量传递IP。你知道为什么吗 #!/usr/bin/env python import dns.resolver,dns.reversename with open("test", "r") as ips: for ip in ips: ip = str(ip) n = dns.reversename.from_address(ip) p

我正在尝试查询dnspython中的反向查找。不幸的是,from_address()函数不允许我通过变量传递IP。你知道为什么吗

#!/usr/bin/env python

import dns.resolver,dns.reversename

with open("test", "r") as ips:
    for ip in ips:
        ip = str(ip)
        n = dns.reversename.from_address(ip)
        print str(dns.resolver.query(n,"PTR")[0])

我是python新手;如果有人能帮忙就好了

我真的怀疑您是否仍在处理此问题,但如果您打印出每个IP,您会意识到dns.reversename.from_address()不喜欢的每个IP中都有一个换行符:

192.168.1.1

192.168.1.2

192.168.1.3
这会导致一个异常:

dns.exception.SyntaxError: Text input is malformed.
您可以轻松更改行:

ip = str(ip)
致:

它将去掉所有的空白(在格式良好的IP地址列表中应该没有空白),给您留下以下内容:

192.168.1.1
192.168.1.2
192.168.1.3

如果您遇到相同的文本格式异常,并且您的IP地址格式正确,这应该可以解决您的问题。很抱歉,我迟到了2年,我在Google上搜索dns.reversename.from_address()时偶然发现了这个问题。如果您的IP列表格式不好,您可以使用类似的方法来筛选出不好的IP。

您的代码对我来说适用于硬编码的IP列表。您确定文件的内容与您所想的一样吗?IP地址包含在文件的外伸部分。当我手动键入IP地址时,它可以正常工作。但是,t肯定有问题文件。是否需要特殊编码?是否识别为文本?
192.168.1.1
192.168.1.2
192.168.1.3