#! /usr/bin/env python
import asyncore, socket, sys
class http_client(asyncore.dispatcher):
def __init__(self, host, path):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect( (host, 80) )
self.buffer = 'GET %s HTTP/1.0\r\n\r\n' % path
def handle_connect(self):
pass
def handle_close(self):
self.close()
def handle_read(self):
a = self.recv(8192)
if len(a):
print len(a), ' ',
sys.stdout.flush()
def writable(self):
return (len(self.buffer) > 0)
def handle_write(self):
sent = self.send(self.buffer)
self.buffer = self.buffer[sent:]
a = dict((cl, cl) for cl in [http_client('www.python.org', '/') for _ in xrange(10)])
#asyncore.loop(timeout=0.1, use_poll=True, map=a) # always hangs up
#asyncore.loop(timeout=0.1, use_poll=True) # works fine