Simple TCP message framework

Padawan Learner newb at seehart.com
Sat Nov 23 04:27:23 EST 2013


Seems like the following pattern must be very common, solved a million 
times, but I can't seem to find anything this simple and ready to use. 
Basically I just want a simple python messaging layer that hides some of 
the messiness of the underlying sockets and user authentication. It 
would be asyncore under the hood of course.

Perhaps what I am looking for is hiding in plain sight, but there are so 
many asynchronous communication tools out there, it's hard to thumb 
through them all. So I'm not looking for such a list of toolkits to 
browse through. Just something simple that does the equivalent of the 
following:

Server:
     on_open(user) - called when a user logs in
     on_close(user) - called when a user disconnects
     on_recv(user, s) - called when a user sends a message
     send(user, s) - send a message to a user

Client:
     open(host, user, password) - open an authenticated session with the 
host
     close() - disconnect
     send(s) - send a message to the server
     on_recv(s): called when a message is received from the server

Methods starting with "on_" are callbacks intended to be implemented by 
derived classes.

Messages are atomic and delimited by '\n'. So concatenation of buffered 
messages is hidden. A message is received when complete (i.e. terminated 
with '\n'). Also, each message should be implicitly flushed, so that it 
will be received in a timely manner in the absence of additional messages.

Extra bonus for a compatible javascript implementation of the Client as 
well, so it can run in a browser.

Thanks in advance for any thoughts.
- Padawan





More information about the Python-list mailing list