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

Martin Stenhård stenis at stenhard.net
Fri Feb 13 15:10:14 MST 2004


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 at twistedmatrix.com
[mailto:twisted-python-admin at twistedmatrix.com] On Behalf Of Brian Warner
Sent: den 13 februari 2004 22:46
To: twisted-python at twistedmatrix.com
Cc: stenis at stenhard.net
Subject: Re: [Twisted-Python] pb server how to get client IP address

Martin Stenhård <stenis at 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 at twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python






More information about the Twisted-Python mailing list