Python3学习(二十五):python获取本机的ip地址

import socket

def get_host_ip():
    """
    get the host ip
    :return: ip
    """
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(('8.8.8.8', 80))
        ip = s.getsockname()[0]
    finally:
        s.close()

    return ip

if __name__ == '__main__':
    print(get_host_ip())

猜你喜欢

转载自blog.csdn.net/liao392781/article/details/80857555