newbie 2, back with a revenge. asyncore

Fredrik Lundh effbot at
Sat Feb 5 16:20:58 CET 2000

Arnaud Fontaine <arnaud at> wrote:
> As I use asyncore sockets with no problem for servers, there is
> something I don't get in 'client' mode.
> Can someone explain me how to use, for exemple, the httpclient exemple
> provided in the lib documentation ?

here's a more complete and bug-free version of the example
in the library reference.  you'll find more examples examples
(including improved http client classes) in the eff-bot guide.

import asyncore, socket

class http_client(asyncore.dispatcher):

    def __init__(self, host, path):
        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):

    def handle_read(self):
        data = self.recv(8192)
        print data

    def handle_close(self):

    def writeable(self):
        return (len(self.buffer) > 0)

    def handle_write(self):
        if self.buffer:
            sent = self.send(self.buffer)
            self.buffer = self.buffer[sent:]

c = http_client("", "/index.html")



<!-- (the eff-bot guide to) the standard python library:

More information about the Python-list mailing list