newbie 2, back with a revenge. asyncore

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


Arnaud Fontaine <arnaud at crao.net> 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):
        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 handle_close(self):
        self.close()

    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("www.python.org", "/index.html")

asyncore.loop()

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->







More information about the Python-list mailing list