not grokking asyncore

Ron Johnson ron.l.johnson at home.com
Fri May 11 02:54:08 EDT 2001


Van Gale wrote:

> 
> "Ron Johnson" <ron.l.johnson at home.com> wrote:
> 
>> Since the server can send messages to the client at random and
>> irregular interevals, how can the client accept input from the
>> user at the same time that it is "stalled" at a select() ?
> 
>> The server side I think I understand, at least from the perspective
>> of *my* needs: the server sits at a select() until it detects (an)
>> incoming message(s) from client(s); it then processes the message,
>> sends messages back to the sending client(s) and also possibly other
>> clients, then goes back to the select().
> 
> I think you are understanding the server side correctly, and there's
> plenty of server applications in the Medusa package to use as a base for
> your
> server.  One minor point though, the "sending back to the clients" is also
> done through the select.  Data being sent out from one of your connection
> objects sits in a buffer and is sent out in chunks each time the select on
> the socket says "ready to send".

So, I downloaded Medusa and looked thru a lot of the source code,
and all the servers seem to use asynchat; not simply asyncore.

Here is a small server and client.  What do I do in handle_accept()
with conn & addr?  If I were to use asynchat, I would call, for
example "self.channel_class (self, conn, addr)", but what do I
do if I simply use asyncore?

If I run this server & client, then the server displays 
"client #1 - 192.168.1.1:2850", showing that handle_accept()
was called, but 'top ''o handle_read' is never printed.

On the client side, this is printed:
   -----sent
   -----slept
   Traceback (most recent call last):
     File "tst.non.cli6.py", line 9, in ?
       sock.send(j)
   socket.error: (32, 'Broken pipe')

I'm just not really sure what to do with the conn.  Sam Rushing
says "the source is the documentation", but when class methods 
override other classes, things get pretty murky, pretty quick.

Thanks much,
Ron

===============  SERVER ==============
import asyncore , socket , string , sys
class nblock_srvr (asyncore.dispatcher):
    def __init__ (self, port):
        asyncore.dispatcher.__init__ (self)
        self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
        self.set_reuse_addr()
        self.bind ((socket.gethostname(), port))
        self.listen(5)
        self.count = 0
    def handle_accept (self):
        conn, addr = self.accept()
        self.count += 1
        print 'client #%d - %s:%d' % (self.count, addr[0], addr[1])
    def handle_read (self):
        print 'top ''o handle_read'
        data = self.recv (10)
        print data
if __name__ == '__main__':
    nblock_srvr (50000)
    asyncore.loop()

=============  CLIENT =================

import socket, select, time, sys
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET,  socket.SO_REUSEADDR, 1)
sock.connect(('rebel', 50000))
j = sys.argv[1] + '\n'
for i in range(10):
    sock.send(j)
    print '-----sent'
    time.sleep(5)
    print '-----slept'
sock.close()
====================================

-- 
 Ron Johnson, Jr.        Home: ron.l.johnson at home.com
 Jefferson, LA  USA      http://ronandheather.dhs.org
 "Is Python better or worse than Perl?"
  "Perl is worse than Python because people wanted it 
   worse." -Larry Wall, 10/14/1998



More information about the Python-list mailing list