2013-12-10 56 views
1

我正在寫一些使用Twisted PB的備份軟件來獲取信息到服務器和從服務器,它都工作得很好。保持跟蹤的客戶端連接到扭曲的PB服務器

我希望能夠做的是跟蹤哪些客戶端連接到服務器。我設法獲取客戶端連接時記錄的連接的IP地址。最初,客戶端可以訪問只有一個方法的pb.Root對象,該方法返回另一個對象,用於訪問存儲的數據。

我想要做的是更新連接客戶端的連接詳細信息,以包含在發送到服務器的呼叫中發送的一些信息。

這裏是我的客戶端登錄

class RKRServerFactory(pb.PBServerFactory): 

    clientsConnected = {} 

    def buildProtocol(self, addr): 
     """ 
     Return a Broker attached to the factory (as the service provider). 
     """ 
     self.clientsConnected[addr.host] = None 
     print self.clientsConnected 
     proto = self.protocol(isClient=False, security=self.security) 
     proto.factory = self 
     proto.setNameForLocal("root", self.root.rootObject(proto)) 
     return proto 

代碼這裏是最初的連接方法

def __init__(self): 
    self.hostid = None 
    self.storage = None 
    self.databasepath = None 

def remote_connect(self, hostid): 
    self.hostid = hostid 
    self.databasepath = os.path.join(os.path.join("/media/098974ed-f717-4dd4-8306-7c4863e87e67/rkr_server_storage", hostid)) 
    try: 
     self.__initDatabase(self.databasepath) 
    except IOError, e: 
     return defer.fail(e) 
    self.storage = RKRStorage(self) 
    return defer.succeed(self.storage) 

我不知道該如何去太記錄的客戶端斷開連接的代碼。如果任何人都可以幫助,我會真的很感激它

回答

0

扭曲觸發器clientConnectionLost()事件/方法。

實施例:

def clientConnectionLost(self, connector, reason): 
    print 'Lost connection. Reason:', reason 

參見:https://twistedmatrix.com/documents/11.1.0/core/howto/clients.html示例和詳細信息。

+0

我想跟蹤PB連接的服務器端連接的客戶端,我不能看到這樣做的方式 – Deano123

+0

這是你的方式。爲「clientConnectMade」和「clientConnectionLost」創建方法,並在連接的客戶端的某處保存映射。 –