程序正在打印内存值而不是实际值



这里有点像脑残。我使用的是nslookup软件包,我想获得一个域的ip地址(在本例中是谷歌(。当我运行下面的代码时,我得到的输出是:<0x00000204C9001548处的nslookup.nslookup.DNSresponse对象>quot;而不是ip地址。

代码:

import nslookup
theLook = nslookup.Nslookup()
print(theLook.dns_lookup(domain="google.com"))

是的,我知道这可能是一个愚蠢的问题,但我发誓,我一直在寻找答案,却找不到适合我的答案。我也认为我以前可能能够解决这个问题,但我不记得是怎么解决的。谢谢你的回答。

您要求打印一个对象,该对象调用Nslookup((的内部__repr__。响应在.answer属性中,因此:

print(theLook.dns_lookup(domain="google.com").answer)

应该做你需要做的事。

该文档还提供了更多详细信息:https://pypi.org/project/nslookup/

返回一个包含两个数组的对象:

response_full:完整的DNS响应字符串

答案:解析的DNS答案(IP列表或SOA字符串(

但您也可以访问dict以获取更多详细信息

import nslookup
theLook = nslookup.Nslookup()
for x in theLook.__dict__:
for m in theLook.__dict__[x]:
print(m)

你会得到这样的输出

google.com. 151 IN A 74.125.24.102
google.com. 151 IN A 74.125.24.101
google.com. 151 IN A 74.125.24.139
google.com. 151 IN A 74.125.24.138
google.com. 151 IN A 74.125.24.100
google.com. 151 IN A 74.125.24.113
74.125.24.102
74.125.24.101
74.125.24.139
74.125.24.138
74.125.24.100
74.125.24.113

最新更新