[Twisted-Python] Why does this simple server not work?

apocalypznow apocalypznow at gmail.com
Mon Nov 13 14:01:27 EST 2006


The following is run but almost immediately terminates without any 
errors.  WHY?  I used "twistd -noy server.tac".
Here is the code:
****************************
import twisted
from twisted.spread import pb
from twisted.cred import checkers,portal
from twisted.internet import reactor
from twisted.python import threadable
from twisted.python import usage
import threading
import socket

clients={}

class ClientRemoteObject(pb.Avatar):
     global clients
     def __init__(self):
         print "ClientRemoteObject initialized."

     def attached(self):
         print "Client attached"
         return self

     def detached(self):
         print "Client detached"

     def perspective_initialDataExchange(self,clientreference,username):
         print "Client reference attached for:" + username
         clients[username]=clientreference
         data={}
         data['connected']=1
         return data

     def perspective_broadcastMessage(self,msg):
         for user in clients:
             clients[user].callRemote("messageCB",msg + "s")



class DefaultRealm:
     __implements__ = portal.IRealm

     def requestAvatar(self, avatarID, mind, *interfaces):
         assert pb.IPerspective in interfaces
         clientremoteobj = ClientRemoteObject()
         clientremoteobj.attached()
         return pb.IPerspective, clientremoteobj, lambda 
a=clientremoteobj:a.detached()

def shutdownCleanUp():
     print "Twisted reactor shutting down."



application = twisted.application.service.Application("server01")
myService = twisted.application.service.IServiceCollection(application)

reactor.addSystemEventTrigger('before','shutdown',shutdownCleanUp)

p = portal.Portal(DefaultRealm())
dbu=twisted.cred.checkers.InMemoryUsernamePasswordDatabaseDontUse()
dbu.addUser("user1","pass1")
p.registerChecker(dbu)


myServer = twisted.application.internet.TCPServer(81,pb.PBServerFactory(p))
myServer.setServiceParent(myService)
****************
It used to work with Twisted 1.3rc1, but it does not run with Twisted 
2.2 (or 2.3 or 2.4).  I am using Python 2.3, and running this from a DOS 
prompt in Windows.






More information about the Twisted-Python mailing list