[Twisted-Python] Can I get the interface's IP addr from where the datagram is received?

Eric Hsu nkeric at gmail.com
Wed Jul 6 22:44:30 EDT 2005

2005/7/6, Tommi Virtanen <tv at twistedmatrix.com>:
> Assumption: you really need to know the address the packet was sent to,
> and not just "(one of) my IP address(es)".

Hi Tommi, thank you for your reply!

I'm having my server running on a machine which has two ethernet
interfaces, one has an intranet ip address like "", another
one has a public internet ip address like "".

I want my server to provide public services to the outside. One of its
function is to allocate some ports for the clients, once the
allocation has been done, the server has to send back an address pair
(ip:port) to the client.

I have written a method:

def getServerIP():
    return socket.getaddrinfo(socket.gethostname(), None)[0][4][0]

It worked properly several days ago and returned me the public address
( of that machine, however, yesterday, it kept returning
the intranet address ( which was useless for the clients
on the internet. I can't figure out why...

I've tried to use the self.transport.getHost(), however, the result
was something like this: IPv4Address(UDP, '', 6677). What's
wrong with this method?

Is it so hard to know the ip address from which the data my server received?

I'm very new to twisted and not a native english speaker, so please
bear with me :p

- Eric

More information about the Twisted-Python mailing list