通过udp包协议头获取本机ip

import socket
 
def get_host_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

参考:https://blog.csdn.net/weixin_40539892/article/details/79103254