ash
ash

Reputation: 652

IP address and hostname of remote peer in Twisted TCP client

Writing a Jabber client that uses Twisted Words. I would like to know an IP address and hostname of remote peer. xmlstream.transport.getPeer().host return hostname, while it should return IP address (xmlstream is an instance of twisted.words.protocols.jabber.xmlstream.XmlStream).

Update: Yes, my Twisted is rather outdated, from Ubuntu Hardy package:

$ python -c "import twisted; print twisted.__version__"
2.5.0

Upvotes: 1

Views: 2976

Answers (2)

ash
ash

Reputation: 652

Ok, I'll answer my own question. Had to go to underlying socket to get IP address:

ip, port = self.xmlstream.transport.socket.getpeername()

It's likely a bug Twisted TCP client that getPeer doesn't work correctly.

And I use getPeer() to get hostname. Yes, it's bad - I'm relying on old and buggy behaviour. Please enlighten me - how do it properly in new version of Twisted?

Upvotes: 1

Glyph
Glyph

Reputation: 31860

You've run into a bug fixed 3 years ago, in Twisted 8.2.0. You should upgrade to a more recent version of Twisted. (I would suggest the recently-announced prerelease of Twisted 11.0; now's a good time to test!) If you've found this bug in a more recent version of Twisted, you should reopen that bug, and explain how to reproduce it!

If you need to support whatever older version of Twisted you're using, rather than socket, which is an accidentally-exposed implementation detail of your ITransport implementation, you should use getHandle, which is at least the documented, public way to get at this implementation-dependent stuff, like this:

self.xmlstream.transport.getHandle().getpeername()

Note that this will not work on some reactors, notably the IOCP reactor, which uses something other than BSD sockets for its network communication.

(Jean-Paul already noted most of this in a comment, but I felt this should show up as an answer.)

Upvotes: 6

Related Questions