1. 程式人生 > >多執行緒批量域名資訊查詢

多執行緒批量域名資訊查詢

寫了一個多執行緒的批量域名資訊查詢,data.txt儲存需要的查詢域名。查詢結束後,生成的結果會分別儲存在以域名為的檔名的檔案中

#!/usr/bin/python
# -*- coding: utf-8 -*-
#by zsdlove
import whois
import json
import threadpool
def domain(dm):  #獲取域名資訊
    pfile = open("info%s.txt"%dm, "a+")
    info=whois.whois(dm)
    dminfo=json.loads(str(info))
    #print "域名:",dminfo["domain_name"]
if (isinstance(dminfo["domain_name"], list)): print "域名:".decode('utf-8'), str(dminfo["domain_name"][0]) pfile.write(str(dminfo["domain_name"][0])) pfile.write('\n') else: print "域名:".decode('utf-8'), str(dminfo["domain_name"]) pfile.write(str(dminfo["domain_name"
])) pfile.write('\n') print "國家:".decode('utf-8'),str(dminfo["country"]) pfile.write(str(dminfo["country"])) pfile.write('\n') print "公司:".decode('utf-8'),str(dminfo["org"]) pfile.write(str(dminfo["org"])) pfile.write('\n') print "地址:".decode('utf-8'),str(dminfo["address"
]) pfile.write(str(dminfo["address"])) pfile.write('\n') #print "公司建立時間:",dminfo["creation_date"] if (isinstance(dminfo["creation_date"], list)): print "公司建立時間:".decode('utf-8'), str(dminfo["creation_date"][0]) pfile.write(str(dminfo["creation_date"][0])) pfile.write('\n') else: print "公司建立時間:".decode('utf-8'), str(dminfo["creation_date"]) pfile.write(str(dminfo["creation_date"])) pfile.write('\n') #print "電子郵箱:",dminfo["emails"] if (isinstance(dminfo["emails"], list)): for email in dminfo["emails"]: print "電子郵箱:".decode('utf-8'), email pfile.write(email) pfile.write('\n') else: print "電子郵箱:".decode('utf-8'), str(dminfo["emails"]) pfile.write(str(dminfo["emails"])) pfile.write('\n') #print "最新更新時間:",dminfo["updated_date"] if (isinstance(dminfo["updated_date"], list)): print "最新更新時間:".decode('utf-8'), str(dminfo["updated_date"][0]) else: print "最新更新時間:".decode('utf-8'), str(dminfo["updated_date"]) #print "到期時間:",dminfo["expiration_date"] if (isinstance(dminfo["expiration_date"], list)): print "到期時間:".decode('utf-8'), str(dminfo["expiration_date"][0]) pfile.write(str(dminfo["expiration_date"][0])) pfile.write('\n') else: print "到期時間:".decode('utf-8'), str(dminfo["expiration_date"]) pfile.write(str(dminfo["expiration_date"])) pfile.write('\n') print "whois伺服器:".decode('utf-8'),str(dminfo["whois_server"]) pfile.write(str(dminfo["whois_server"])) pfile.write('\n') #print isinstance(dminfo["name_servers"],tuple) #print type(dminfo["name_servers"]) if(isinstance(dminfo["name_servers"],list)): for dnsserver in dminfo["name_servers"]: print "dns 伺服器:".decode('utf-8'),dnsserver pfile.write(dnsserver) pfile.write('\n') else: print "dns伺服器:".decode('utf-8'),str(dminfo["name_servers"]) if __name__ == "__main__": #dm=raw_input("請輸入一個域名:".decode('utf-8')) dmfile = open('data.txt') dm = [] for line in dmfile.readlines(): dm.append(line.strip('\n'))#去除換行符 dmfile.close() pool = threadpool.ThreadPool(20) requests = threadpool.makeRequests(domain, dm) [pool.putRequest(req) for req in requests] pool.wait()