I came across a nice code snippet today to accomplish this. Naturally, this doesn’t work for NAT.

def GetPublicIpAddress():
 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 s.connect(('google.com', 80))
 return s.getsockname()[0]

Another great snippet for finding the IP address of a given network interface is in the ASPN Python Cookbook.