Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Asyncore works now

By: a guest on Dec 13th, 2011  |  syntax: Python  |  size: 0.87 KB  |  hits: 75  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #! /usr/bin/env python
  2.  
  3. import asyncore, socket, sys
  4.  
  5. mymap = {}
  6.  
  7. class http_client(asyncore.dispatcher):
  8.  
  9.     def __init__(self, host, path):
  10.         asyncore.dispatcher.__init__(self, map=mymap)
  11.         self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
  12.         self.connect( (host, 80) )
  13.         self.buffer = 'GET %s HTTP/1.0\r\n\r\n' % path
  14.  
  15.     def handle_connect(self):
  16.         pass
  17.  
  18.     def handle_close(self):
  19.         self.close()
  20.  
  21.     def handle_read(self):
  22.         a = self.recv(8192)
  23.         if len(a):
  24.             print len(a), ' ',
  25.             sys.stdout.flush()
  26.  
  27.     def writable(self):
  28.         return (len(self.buffer) > 0)
  29.  
  30.     def handle_write(self):
  31.         sent = self.send(self.buffer)
  32.         self.buffer = self.buffer[sent:]
  33.  
  34. a = [http_client('www.python.org', '/') for _ in xrange(10)]
  35.  
  36. asyncore.loop(use_poll=True, map=mymap)
  37.  
  38.