Python获取本机IP

Sat 05 June 2010
  • 把戏 tags:
  • linux
  • python published: true comments: true

除了用inetface库和分析ifconfig输出以外,一个比较简单的获取本机IP的方法:
[cc lang="python"]
import socket
def get_local_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("sdo.com",80))
return s.getsockname()[0]
[/cc]

参考 http://stackoverflow.com/questions/166506/finding-local-ip-addresses-in-python