首页 > 代码库 > 批量域名dig---使用协程

批量域名dig---使用协程

#!/usr/bin/env python
# _*_coding:utf-8_*_

from gevent import monkey
import gevent
import time
import dns.resolver
monkey.patch_socket()

class MyGeven:

    def __init__(self):
        self.data = http://www.mamicode.com/{}

    def get_dig_domain(self, domain):
                res = dns.resolver.query(domain, 1)
        for i in res.response.answer:
                for j in i.items:
                 if j.rdtype == 5:
               find_res = j.to_text().find(‘***dns.com‘)
                         self.data[domain] = 0 if find_res != -1 else -1
                 else:
                         self.data[domain] = -1
                break

    def gprobe(self, domain_list):
        jobs = []
        for domain in domain_list:
            jobs.append(gevent.spawn(self.get_dig_domain, domain))
        gevent.joinall(jobs)

if __name__ == ‘__main__‘:
    start_ts = time.time()
    print "start....."
    domain_list = [
            "www.baidu.com",
                "www.qq.com",
                "www.163.com",
            "dl.3366.com"
    ]
    obj = MyGeven()
    obj.gprobe(domain_list)
    print "请求数:", len(domain_list), "耗时:", time.time() - start_ts, "s"
    print "域名dig:", obj.data

本文出自 “开发与运维” 博客,谢绝转载!

批量域名dig---使用协程