ASPN ActiveState Programmer Network
  ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups | Web Services
SEARCH
advanced | search help

Reference
ActivePython 2.4
Python Documentation
Library Reference
11. Internet Protocols and Support
11.25 asyncore -- Asynchronous socket handler
11.25.1 asyncore Example basic HTTP client

MyASPN >> Reference >> ActivePython 2.4 >> Python Documentation >> Library Reference >> 11. Internet Protocols and Support >> 11.25 asyncore -- Asynchronous socket handler
ActivePython 2.4 documentation


11.25.1 asyncore Example basic HTTP client

As a basic example, below is a very basic HTTP client that uses the dispatcher class to implement its socket handling:

class http_client(asyncore.dispatcher):
    def __init__(self, host,path):
        asyncore.dispatcher.__init__(self)
        self.path = path
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect( (host, 80) )
        self.buffer = 'GET %s HTTP/1.0\r\n\r\n' % self.path
        
    def handle_connect(self):
        pass
        
    def handle_read(self):
        data = self.recv(8192)
        print data
        
    def writable(self):
        return (len(self.buffer) > 0)
    
    def handle_write(self):
        sent = self.send(self.buffer)
        self.buffer = self.buffer[sent:]
See About this document... for information on suggesting changes.

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState 2004 All rights reserved