python根据域名循环遍历查找绑定IP

# encoding: UTF-8
import socket
 
rfile = open('c:\\ip.txt')
wfile = open('c:\\result.csv', 'w+')
for line in rfile:
    str = line.strip().split(';')
    for s in str:
        s = s.strip().split('/')
        s = s[0]
        try:
            result = socket.gethostbyname(s)
        except:
            result = "error"
        if result == 'error':
            www = 'www'
            nPos = s.find(www)
            if nPos == -1:
                s = 'www.' + s
            try:
                result = socket.gethostbyname(s)
            except:
                result = "error"
        wfile.write(s + ',' + result)
        wfile.write('\n')
        print s
rfile.close()
wfile.close()
原文地址:https://www.cnblogs.com/fighter/p/2567529.html