如何利用python批量生成本地ip的实例详解
1个回答
展开全部
import sys, socket
try:
ipLines = open('ip.txt').readlines()
results = []
for ip in ipLines:
result = socket.gethostbyaddr(ip.replace('\n',''))
results.append(result[0])
print "Primary hostname:"
print " " + result[0]
print "\nAddresses:"
for item in result[2]:
print " " + item
wr = open('hostname.txt','w+')
for host in results:
wr.write(host)
wr.write('\n')
wr.close()
except socket.herror, e:
print "Couldn't look up name:", e
try:
ipLines = open('ip.txt').readlines()
results = []
for ip in ipLines:
result = socket.gethostbyaddr(ip.replace('\n',''))
results.append(result[0])
print "Primary hostname:"
print " " + result[0]
print "\nAddresses:"
for item in result[2]:
print " " + item
wr = open('hostname.txt','w+')
for host in results:
wr.write(host)
wr.write('\n')
wr.close()
except socket.herror, e:
print "Couldn't look up name:", e
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询