Well, you can't, really.
There are a couple of techniques to do a qualified guess though.
The first one is to try a dns lookup on the ip address. I.e. gethostbyaddr(). Then check the country part of the domain. This will, in many cases, be correct.
Alternately, you can trace the route to the ip address and log hostnames on the way. Say if the hop before the final host has a domain ending with .dk, it's quite likely that the target ip is physically in denmark, even if it resolves to .nz. 🙂