Skip to content

Instantly share code, notes, and snippets.

@longfin
Created February 12, 2012 14:08
Show Gist options
  • Save longfin/1808715 to your computer and use it in GitHub Desktop.
Save longfin/1808715 to your computer and use it in GitHub Desktop.
asyncore
# http://docs.python.org/library/asyncore.html#asyncore-example-basic-http-client
import asyncore, socket
class HTTPClient(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):
print self.recv(8192)
def writable(self):
return (len(self.buffer) > 0)
def handle_write(self):
sent = self.send(self.buffer)
self.buffer = self.buffer[sent:]
client = HTTPClient('www.python.org', '/')
asyncore.loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment