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()
posted @ 2012-06-28 14:15  fighter  阅读(1764)  评论(0编辑  收藏  举报