Ad

How Do I Get The External IP Of A Socket In Python?

- 1 answer

When I call socket.getsockname() on a socket object, it returns a tuple of my machine's internal IP and the port. However, I would like to retrieve my external IP. What's the cheapest, most efficient manner of doing this?

Ad

Answer

This isn't possible without cooperation from an external server, because there could be any number of NATs between you and the other computer. If it's a custom protocol, you could ask the other system to report what address it's connected to.

Ad
source: stackoverflow.com
Ad