Python获取本机IP

除了用inetface库和分析ifconfig输出以外,一个比较简单的获取本机IP的方法:

import socket
def get_local_ip():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(("sdo.com",80))
    return s.getsockname()[0]

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

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>