[Twisted-Python] Using cred, how to prevent the user repeat-login?

yangyouxiu yangyouxiu at gmail.com
Mon Mar 31 19:45:23 MDT 2014


In my Client-Server Application,I do not want two user login with the same login name.

For example, user1 logined with name "test1",then user2 try to login with "test1" too, the login of user2 should be denied. At now, my code could not achieve this goal,so what's the method.
Thanks all!

The following is server code:
-----------------------------------------------------------------------------
class simulationServer:
    def __init__(self):
        self.groups = {} # indexed by name
        self.groups['#admin']= Group('#admin')
        self.groups['#general']=Group('#general')
    def joinGroup(self, groupname, user):
        if groupname not in self.groups:
            self.groups[groupname] = Group(groupname)
        self.groups[groupname].addUser(user)
        return self.groups[groupname]
    def leaveGroup(self, groupname, user):
        self.groups[groupname].delUser(user)
        return self.groups[groupname]
    def getGroup(self, admin=False):
        if admin==True:
            return self.groups['#admin']
        return self.groups['#general']

class simulRealm:
    implements(portal.IRealm)
    def requestAvatar(self, avatarID, mind, *interfaces):
        assert pb.IPerspective in interfaces
        avatar = User(avatarID)
        avatar.server = self.server
        avatar.attached(mind)
        return pb.IPerspective, avatar, lambda a=avatar:a.detached(mind)
        
class User(pb.Avatar):
    def __init__(self, name):
        self.name = name
        self.group=None
        self.status=False  #False: general client user not sync yet 
        self.serverRoot=ServerRoot()
    def attached(self, mind):
        self.remote = mind
    def detached(self, mind):
        print '%s detached...'%self.name
        self.server.leaveGroup(self.group, self)
        self.server.messageClientUserupdated()
        self.remote = None
    def perspective_joinGroup(self, groupname):
        print 'joined Group on Server Machine '
        print groupname
        self.group=groupname
        self.server.joinGroup(groupname, self)
        self.server.messageClientUserupdated()
        return groupname
    def perspective_getServerRoot(self):
        return self.serverRoot
    def getClientRoot(self):
        return self.remote
    def perspective_updateStatus(self):
        self.status=True  # user sync

class Group(pb.Viewable):
    def __init__(self, groupname):
        self.name = groupname
        self.users = []
    def addUser(self, user):
        self.users.append(user)
    def delUser(self, user):
        self.users.remove(user)
    def getallUsers(self):
        return self.users
    def getnotSyncUsers(self):
        nosyncusers=[]
        if self.users:
            for user in self.users:
                if not user.status:
                    nosyncusers.append(user)
        return nosyncusers
    def getUsers(self):
            syncusers=[]
            if self.users:
                for user in self.users:
                    if user.status:
                        syncusers.append(user)
            return syncusers
-----------------------------------------------------------------------------
Client code:
def1 = factory.login(credentials.UsernamePassword(self.group+'_'+self.UserNameandPwd[0], self.UserNameandPwd[1]), client=self)
def1.addCallback(self.connected)
def1.addErrback(self.connectFailure)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://twistedmatrix.com/pipermail/twisted-python/attachments/20140401/a50566ac/attachment.html>


More information about the Twisted-Python mailing list