Subject: Re: newbie 2, back with a revenge. asyncore - DN [1]


"Fredrik Lundh" <effbot@telia.com> - 05 Feb 2000 - comp.lang.python

 Arnaud Fontaine <arnaud@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
 -->

Last modified
2000-02-10

(195.108.246.50)

Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated