Download Current Version: simpleCS.py (2005-04-10)

This module allows for really easy Server-With-Multiple-Clients type programs. To use it, you create a class for the server, with a bunch of methods in it. This looks something like:

 

class TestServer:
def onConnect(self,client):
print 'Connection from',client.addr
client.welcome()
def hello(self,client,x):
print 'hello',x
client.data(12345678)

class TestClient:
def welcome(self):
print 'welcome'
self.server.hello('world')
def data(self,x):
print x

The two programs run on different computers, but they can call methods on each other just like they are on the same system.

All method calls across the network are non-blocking (i.e. they do not wait for the message to be sent before returning). This also means they will not return anything. To return a value, you generally end up doing something like:

 

  class MyServer:
data=10
def getData(self,client):
client.setData(self.data)

class MyClient:
data=None
def doSomething(self):
self.server.getData()
def setData(self,value):
self.data=value

To start the server and clients, use

  simpleCS.startServer(handler,port=6000,separateThread=False)
simpleCS.startClient(handler,addr='127.0.0.1',port=6000,separateThread=False)

The separateThread argument is there to make it easy to run the client or server in a separate thread, which I find can be handy depending on what I'm doing with it. You can stop them with stopServer and stopClient.

The client objects that the server works with also get a client.addr variable which gives their IP address. The server also gets a self.clients method which is a list of all clients currently connected.

History