def _add (method, url, params = None, auth = None, headers = {}, callback = None, meta = None, proxy = None): global _que, _initialized, _dns_query_req, _dns_reqs, _workers if not _initialized: configure () if not meta: meta = {} meta ['req_id'] = _que.req_id meta ['req_method'] = method meta ['req_callback'] = callback _que.add ((method, url, params, auth, headers, meta, proxy)) # DNS query for caching and massive if not lifetime._polling and _dns_reqs < _workers: host = urlparse (url) [1].split (":")[0] if host not in _dns_query_req: _dns_query_req [host] = None _dns_reqs += 1 adns.query (host, "A", callback = lambda x: None) if dns.qsize (): dns.pop_all () asyncore.loop (0.1, count = 2)
def loop(): dns.pop_all() while asyncore.socket_map: dns.pop_all() asyncore.loop(timeout=1, count=1) if not sum([ isinstance(r, dns.TCPClient) for r in asyncore.socket_map.values() ]): break
def testCluster(): sc = cluster_manager.ClusterManager("tt", [ "hq.lufex.com:5000 1", ], logger=logger.screen_logger()) clustercall = task.TaskCreator(sc, logger.screen_logger()) s = clustercall.Server("/websocket/echo", "Hello WS Cluster", "ws", auth=("app", "1111"), mapreduce=False) threading.Thread(target=__reduce, args=(s, )).start() while 1: asyncore.loop(timeout=1, count=2) if len(asyncore.socket_map) == 1: break
def testSocketPool(): sc = socketpool.SocketPool(logger.screen_logger()) clustercall = task.TaskCreator(sc, logger.screen_logger()) s = clustercall.Server("http://hq.lufex.com:5000/websocket/echo", "Hello WS Sock Pool", "ws", auth=("app", "1111")) #s = clustercall.Server ("http://210.116.122.187:3424/rpc2", "admin/whddlgkr") #s.bladese.util.status ("openfos.v2") threading.Thread(target=__reduce, args=(s, )).start() while 1: asyncore.loop(timeout=1, count=2) print(asyncore.socket_map) if len(asyncore.socket_map) == 1: break
def pop_all(self): # DNS query maybe not allowed delay between request and send # maybe they just drop response packet for delaying with self.lock: queue, self.queue = self.queue[:], [] count = len(queue) while queue: name, args = queue.pop(0) self.handler.handle_request(name, **args) if (not count and not self.has_job()): return map = {} with self.lock: for client in self.udps: map[client._fileno] = client fds = list(map.keys()) # maybe 2 is enough safeguard = count * 2 while self.has_job() and safeguard: safeguard -= 1 asyncore.loop(0.1, map, count=1) if safeguard % 5 == 0: self.maintern(time.time()) self.maintern(time.time()) for fd in fds: if fd not in map: # resync try: del asyncore.socket_map[fd] except KeyError: pass
else: print("FAILED") if __name__ == "__main__": from rs4 import logger import pprint create_pool(PUBLIC_DNS_SERVERS, logger.screen_logger()) for i in range(4): #query ("www.microsoft.com", protocol = "udp", callback = _print, qtype="a") #query ("www.cnn.com", protocol = "udp", callback = _print, qtype="a") #query ("www.gitlab.com", protocol = "udp", callback = _print, qtype="a") #query ("www.alexa.com", protocol = "udp", callback = _print, qtype="a") #query ("www.yahoo.com", protocol = "udp", callback = _print, qtype="a") #query ("www.github.com", protocol = "udp", callback = _print, qtype="a") #query ("www.google.com", protocol = "udp", callback = _print, qtype="a") #query ("www.amazon.com", protocol = "udp", callback = _print, qtype="a") #query ("www.almec.com", protocol = "udp", callback = _print, qtype="a") #query ("www.alamobeauty.com", protocol = "udp", callback = _print, qtype="a") #query ("www.alphaworld.com", protocol = "udp", callback = _print, qtype="a") #query ("www.allrightsales.com", protocol = "udp", callback = _print, qtype="a") query("www.glasteel.com", protocol="udp", callback=_print, qtype="a") pop_all() print('------------------------') while 1: pop_all() asyncore.loop(timeout=1, count=1) print('UNFINISHED', pool.jobs())
def handle_close(self): self.close() self.consumer.close() class WhoisConsumer: def __init__(self, host): self.text = "" self.host = host def feed(self, text): self.text = self.text + text def abort(self): print(self.host, "=>", "failed") def close(self): print(self.host, "=>") print(self.text) # # try it out for host in []: consumer = WhoisConsumer(host) request = WhoisRequest(consumer, host, "whois.internic.net") # loop returns when all requests have been processed asyncore.loop()