开发者

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.开发者_如何学C 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


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.)


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜