码迷,mamicode.com
首页 > 编程语言 > 详细

python 协程 demo

时间:2019-03-12 18:02:53      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:reload   for   isp   one   import   head   rev   and   pac   

 

# -*- coding: UTF-8 -*-
 
import gevent
from gevent import socket
from gevent import event
 
rev=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
rev.bind((‘‘,53))
ip=[]
cur=0
 
def preload():
    for i in open(ip):
        ip.append(i)
    print("load "+str(len(ip))+"ip")
 
def send_request(data):
    global cur
    ret=rev.sendto(data,(ip[cur],53))
    cur=(cur+1)%len(ip)
 
class Cache:
    def __init__(self):
        self.c={}
    def get(self,key):
        return self.c.get(key,None)
    def set(self,key,value):
        self.c[key]=value
    def remove(self,key):
        self.c.pop(key,None)
 
cache=Cache()
 
def handle_request(s,data,addr):
    req=dnslib.DNSRecord.parse(data)
    qname=str(req.q.qname)
    qid=req.header.id
    ret=cache.get(qname)
    if ret:
        ret=dnslib.DNSRecord.parse(ret)
        ret.header.id=qid;
        s.sendto(ret.pack(),addr)
    else:
        e=event.Event()
        cache.set(qname+"e",e)
        send_request(data)
        e.wait(60)
        tmp=cache.get(qname)
        if tmp:
            tmp=dnslib.DNSRecord.parse(tmp)
            tmp.header.id=qid;
            s.sendto(tmp.pack(),addr)
 
def handle_response(data):
    req=dnslib.DNSRecord.parse(data)
    qname=str(req.q.qname)
    print(qname)
    cache.set(qname,data)
    e=cache.get(qname+"e")
    cache.remove(qname+"e")
    if e:
        e.set()
        e.clear()
 
def handler(s,data,addr):
    req=dnslib.DNSRecord.parse(data)
    if req.header.qr:
        handle_response(data)
    else:handle_request(s,data,addr)
 
def main():
    preload()
    while True:
        data,addr=rev.recvfrom(8192)
        gevent.spawn(handler,rev,data,addr)
 
if __name__ == __main__:
    main()

 

python 协程 demo

标签:reload   for   isp   one   import   head   rev   and   pac   

原文地址:https://www.cnblogs.com/sea-stream/p/10518304.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!