import urllib, re, os, socket def getWanIp(): httext = '' try: httext = urllib.urlopen('http://checkip.dyndns.org').read() except: return '0.0.0.0' # Search for a pattern that looks like an IP address m = re.search(r"""\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}""", httext) if m: return m.group() else: return '0.0.0.0' def getLocalIPs(): tuple = socket.gethostbyaddr(socket.gethostname()) return tuple[2] if __name__ == '__main__': wanIp = getWanIp() print 'WAN IP:', wanIp for ip in getLocalIPs(): print 'Local IP:', ip