asyncore asynchat

Michael Welsh mwelsh at
Tue Jul 15 13:32:56 CEST 2003

In order to learn sockets in Python I am trying to write a simple group chat 
server and client.  I need a little nudge, please.  My question contains some 
GUI but only as decoration.  The root question is asyncore / asynchat.  I 
have read that twisted makes this all simple, but, I wanted to get my hands 
dirty here for educational purposes.

I've managed to build a small echo server with asyncore and asynchat that can 
handle any number of connections and repeat to all clients what was sent in 
by individual clients.   The server seems to work well.

The GUI client, wxPython, creates a socket on open.  The user types some text 
and clicks [SEND].  The send event puts the text out to the socket then reads 
the response and adds it to list control.  Everything works well.  

I'm now ready for the next step, to disconnect the send data from the recieve 
data.  So a user who does not send text, still gets what others may have 
written.  I need to somehow poll the thread so "add to list control" can be 
triggered on recieving the data.

I've tried several approaches and can't seem to get it right.  First I tried 
setting up a separate threading.Thread that created the socket.  This way I 
could put a poll on the socket.  I even created (borrowed) a custom event so 
when data was found, it could notify the GUI and call the "add to list 
control"  But I must have been doing it wrong because it kept locking up in 
the loop..  

I knew from the server side that asynchat has built in helpers with 
collect_incoming_data() and found_terminator().  (my terminator is appended 
when data is sent to the server)  I could then use the asyncore.poll() in my 
threading.Thread to raise the custom event that sends data to the GUI, when 
something comes over the socket.

I think I have the concepts right, but the execution breaks down here.  I 
tried having the threading.Thread create an asyn_chat but I keep getting 
errors that I assume the asyncore.dispatcher takes care of.  So... maybe I 
should use both core and chat on the client as well as the server.  The only 
examples of dispatcher I can find open up an asyn_chat on "listen".  The 
client needs to initiate the communication when it starts.  The server is 
listening and will respond.  I won't know on which port the response will 
come back.

Here's some bits showing what I'm trying to do.  You may find some of it 
familiar.  I hope I didn't clip anything that may have been needed  If anybody 
has time to point out where I'm going wrong it would be very helpful.  Thank 
you if you were patient enough to get this far...

import threading
import time
import socket
import asyncore
import asynchat
from   wxPython.wx import *

# server is listening on ...

class MyTest(wxFrame):
    def __init__(self, parent, ID, title):
        # Initialize wxFrame
        wxFrame.__init__(self, .....

        # start the thread = NetworkThread(self)

        # build rest of GUI... works fine

# the network thread communicates back to the main
# GUI thread via this sythetic event
class NetworkEvent(wxPyEvent):
    def __init__(self,msg=""):
        self.msg = msg

wxEVT_NETWORK = 2000
def EVT_NETWORK(win, func):
    win.Connect(-1, -1, wxEVT_NETWORK, func)

class NetworkThread(threading.Thread):
    def __init__(self,win):
        threading.Thread.__init__(self) = win
        self.keep_going = true
        self.running    = false
        self.MySock = NetworkServer(REMOTE_HOST, REMOTE_PORT, 
        self.event_loop = EventLoop()

    def push(self,msg):
    def is_running(self):
        return self.running
    def stop(self):
        self.keep_going = 0
    def check_status(self,el,time):
        if not self.keep_going:
    def received_a_line(self,m):
    def run(self):
        self.running = true
        # loop here checking every 0.5 seconds for shutdowns etc..
        # server has shutdown
        self.send_event("Closed down network")
        self.running = false

    # send a synthetic event back to our GUI thread
    def send_event(self,m):
        evt = NetworkEvent(m)
        del evt

class NetworkServer (asyncore.dispatcher):
    def __init__ (self, host, port, handler=None):
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect((host, port))

    def handle_connect (self):
        # I thought, maybe, the server would trigger this... nope
        ChatSocket (host, port, self.handler)

    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:]

    # send a synthetic event back to our GUI thread
    def send_event(self,m):
        evt = NetworkEvent(m)
        del evt

class ChatSocket(asynchat.async_chat):
    def __init__(self, host, port, handler=None):
        asynchat.async_chat.__init__ (self, port)

    def collect_incoming_data(self, data):

    def found_terminator(self):
        if self.handler:
            print 'warning: unhandled message: ', = ''

class EventLoop:
    socket_map = asyncore.socket_map
    def __init__ (self): = {}

    def go (self, timeout=5.0):
        events =
        while self.socket_map:
            print 'inner-loop'
            now = int(time.time())
            for k,v in events.items():
                if now >= k:
                    v (self, now)
                    del events[k]
            asyncore.poll (timeout)

    def schedule (self, delta, callback):
        now = int (time.time())[now + delta] = callback

    def unschedule (self, callback, all=1):
        "unschedule a callback"
        for k,v in
            if v is callback:
                if not all:

# -----------------------------------------
# Run App
# -------------------------------------------
class TestApp(wxApp):
    def OnInit(self):
        frame = MyTest(None, -1, "Test APP")
        return true

app = TestApp(0)

More information about the Python-list mailing list