Re: [Twisted-Python] pb server how to get client IP address

Martin Stenhård <stenis@stenhard.net> writes:
Ah, yes, that is trickier. You need to get access to the Broker object (which is a Protocol subclass), and called methods are not generally given access to that. The Perspective/ViewPoint -style stuff gives invoked methods access to some initial Perspective object, but even that doesn't know who the Broker is. The method which invokes your perspective_* method knows, but it isn't telling. I poked around a bit and haven't found any way to get at it. If the client has given you access to one if its own objects (perhaps as the 'mind' reference in the realm.requestAvatar() call), then you can use it to get the Broker: mind.broker.transport.getPeer() Other than that, I don't see how to do it. The details of the connection are a little *too* abstracted out. Maybe someone else has an idea? sorry, -Brian PS: and no, inspect.stack()[1][0].f_locals['broker'] doesn't count..

Thanks Brian mind.broker.transport.getPeer() did job I use mind and I get the client IP successfully now. class MyPerspective(pb.Avatar): connections = 0 def __init__(self, name): self.name = name self.position = [0.0,0.0,0.0] self.rotation = [0.0,0.0,0.0] self.rotation2 = [0.0,0.0,0.0] self.velocity = [0.0,0.0,0.0] self.state = 0 def attached(self, mind): ip = mind.broker.transport.getPeer() print ip Thanks /Martin http://stenhard.net

Hi, what's the job of stopConsuming? I ran a grep in twisted dir, and didn't find anybody calls it. Besides, since registerProducer only happens at the consumer side, so I guess in theory the producers have no idea who are their consumers, right? Thank in advance for the clarification. -- Yun

Thanks Brian mind.broker.transport.getPeer() did job I use mind and I get the client IP successfully now. class MyPerspective(pb.Avatar): connections = 0 def __init__(self, name): self.name = name self.position = [0.0,0.0,0.0] self.rotation = [0.0,0.0,0.0] self.rotation2 = [0.0,0.0,0.0] self.velocity = [0.0,0.0,0.0] self.state = 0 def attached(self, mind): ip = mind.broker.transport.getPeer() print ip Thanks /Martin http://stenhard.net

Hi, what's the job of stopConsuming? I ran a grep in twisted dir, and didn't find anybody calls it. Besides, since registerProducer only happens at the consumer side, so I guess in theory the producers have no idea who are their consumers, right? Thank in advance for the clarification. -- Yun
participants (3)
-
Brian Warner
-
Martin Stenhård
-
Yun Mao