试图从IP和主机名列表中获取主机名--当它解析IP地址时,我没有得到想要的返回,它仍然只是返回一个IP。
示例: 192.10.20.1输出:查找主机名192.10.20.1,而不是将其转换为主机名。
def reverse_dns(IP):
try:
socket.gethostbyaddr(IP)
print ('Found hostname', IP)
return (True)
except socket.error :
print (IP)
return (False)
def get_hostname (x) :
hostname= []
device = 'is blank'
for device in x :
if reverse_dns(device) :
hostname.append(device)
return (hostname)
df['hostname'] = df['ips'].apply(get_hostname)发布于 2022-10-27 20:03:51
您需要返回gethostbyaddr()返回的主机名。然后
def reverse_dns(IP):
try:
hostname, aliaslist, ipaddrlist = socket.gethostbyaddr(IP)
print ('Found hostname', IP)
return hostname
except socket.error :
print (IP)
return (False)
def get_hostname (x) :
hostname= []
for device in x :
hostname.append(reverse_dns(device) or 'is blank')
return (hostname)
df['ips'] = df['hostname'].apply(get_hostname)https://stackoverflow.com/questions/74227491
复制相似问题