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

Martin Stenhård <stenis@stenhard.net> writes:
I have a question on how to get ip address of the clients that connects to the server.
From the Protocol object, the .transport offers a pair of methods called getHost() and getPeer(). So something like this should work: ==== BEGIN from twisted.internet import reactor, protocol class MyProtocol(protocol.Protocol): def connectionMade(self): print "connection from", self.transport.getPeer() f = protocol.ServerFactory() f.protocol = MyProtocol reactor.listenTCP(9111, f) reactor.run() ==== END When I run this on my system, and connect with 'nc localhost 9111', I get: % /tmp/p.py connection from ('INET', '127.0.0.1', 38002) The value it returns depends upon the kind of transport, but TCP connections will give you a tuple of ('INET', hostname, port), where 'hostname' is a dotted-quad IP address. I'll add this to the FAQ. cheers, -Brian

Hi again
From the Protocol object, the .transport offers a pair of methods called getHost() and getPeer().
Yes this works find in my server for my admin interface (simple telenet bases) that use protocol.ServerFactory() and then use LineReceiver. But how do I use getPeer() in a Perspective Broker server for example http://www.twistedmatrix.com/documents/examples/pbbenchserver.py Excuse me questions but I new to Twisted. /Martin http://stenhard.net -----Original Message----- From: twisted-python-admin@twistedmatrix.com [mailto:twisted-python-admin@twistedmatrix.com] On Behalf Of Brian Warner Sent: den 13 februari 2004 22:46 To: twisted-python@twistedmatrix.com Cc: stenis@stenhard.net Subject: Re: [Twisted-Python] pb server how to get client IP address Martin Stenhård <stenis@stenhard.net> writes:
I have a question on how to get ip address of the clients that connects to the server.
From the Protocol object, the .transport offers a pair of methods called getHost() and getPeer(). So something like this should work:
==== BEGIN from twisted.internet import reactor, protocol class MyProtocol(protocol.Protocol): def connectionMade(self): print "connection from", self.transport.getPeer() f = protocol.ServerFactory() f.protocol = MyProtocol reactor.listenTCP(9111, f) reactor.run() ==== END When I run this on my system, and connect with 'nc localhost 9111', I get: % /tmp/p.py connection from ('INET', '127.0.0.1', 38002) The value it returns depends upon the kind of transport, but TCP connections will give you a tuple of ('INET', hostname, port), where 'hostname' is a dotted-quad IP address. I'll add this to the FAQ. cheers, -Brian _______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

Hi again
From the Protocol object, the .transport offers a pair of methods called getHost() and getPeer().
Yes this works find in my server for my admin interface (simple telenet bases) that use protocol.ServerFactory() and then use LineReceiver. But how do I use getPeer() in a Perspective Broker server for example http://www.twistedmatrix.com/documents/examples/pbbenchserver.py Excuse me questions but I new to Twisted. /Martin http://stenhard.net -----Original Message----- From: twisted-python-admin@twistedmatrix.com [mailto:twisted-python-admin@twistedmatrix.com] On Behalf Of Brian Warner Sent: den 13 februari 2004 22:46 To: twisted-python@twistedmatrix.com Cc: stenis@stenhard.net Subject: Re: [Twisted-Python] pb server how to get client IP address Martin Stenhård <stenis@stenhard.net> writes:
I have a question on how to get ip address of the clients that connects to the server.
From the Protocol object, the .transport offers a pair of methods called getHost() and getPeer(). So something like this should work:
==== BEGIN from twisted.internet import reactor, protocol class MyProtocol(protocol.Protocol): def connectionMade(self): print "connection from", self.transport.getPeer() f = protocol.ServerFactory() f.protocol = MyProtocol reactor.listenTCP(9111, f) reactor.run() ==== END When I run this on my system, and connect with 'nc localhost 9111', I get: % /tmp/p.py connection from ('INET', '127.0.0.1', 38002) The value it returns depends upon the kind of transport, but TCP connections will give you a tuple of ('INET', hostname, port), where 'hostname' is a dotted-quad IP address. I'll add this to the FAQ. cheers, -Brian _______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (2)
-
Brian Warner
-
Martin Stenhård