代码:
# -*- coding:utf-8 -*-
import socket
import sys
host_file = 'host.txt'
host_open = open(host_file, 'r')
host_list = []
for line in host_open:
host_list.append(line.rstrip())
host_open.close()
#rint host_list
hosts_file = open('hosts.txt','w') #输出结果在hosts.txt文件中
for i in host_list:
try:
ip = socket.gethostbyname(i)
print ip, i
hosts_file.write(ip + i + 'n')
except:
print i
hosts_file.close()