If it won't be simple, it simply won't be. [Hire me, source code] by Miki Tebeka, CEO, 353Solutions

Friday, July 10, 2009

What's My IP?

#!/usr/bin/env python
'''Find local machine IP (cross platform)'''

from subprocess import Popen, PIPE
from sys import platform
import re

"darwin" : "/sbin/ifconfig",
"linux" : "/sbin/ifconfig",
"linux2" : "/sbin/ifconfig",
"win32" : "ipconfig",

def my_ip():
command = COMMANDS.get(platform, "")
assert command, "don't know how to get IP for current platform"

pipe = Popen([command], stdout=PIPE)
output = pipe.stdout.read()

for ip in re.findall("(\d+\.\d+\.\d+\.\d+)", output):
if ip.startswith("127.0.0"):
return ip

if __name__ == "__main__":
print my_ip()
Post a Comment

Blog Archive