【问题标题】:Keeping Track of Clients connect to Twisted PB Server跟踪连接到 Twisted PB 服务器的客户端
【发布时间】:2013-12-10 13:37:25
【问题描述】:

我正在使用 Twisted 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)

我不确定如何让客户端断开连接也被记录下来。如果有人可以提供帮助,我将不胜感激

【问题讨论】:

    标签: python twisted perspective-broker


    【解决方案1】:

    Twisted 触发 clientConnectionLost() 事件/方法。

    例子:

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

    有关示例和详细信息,请参阅:https://twistedmatrix.com/documents/11.1.0/core/howto/clients.html

    【讨论】:

    • 我试图在 PB 连接的服务器端跟踪连接的客户端,但我看不到这样做的方法
    • 这就是你的做法。为clientConnectMadeclientConnectionLost 创建方法,并在您连接的客户端的某处保留映射。
    猜你喜欢
    • 1970-01-01
    • 2019-01-09
    • 2015-09-30
    • 1970-01-01
    • 2016-07-07
    • 2013-07-30
    • 1970-01-01
    • 1970-01-01
    • 2014-07-04
    相关资源
    最近更新 更多