Created
February 12, 2012 14:08
-
-
Save longfin/1808715 to your computer and use it in GitHub Desktop.
asyncore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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