Python 获取IP地址

import socket

def get_host_ip():
    """
    查询本机ip地址
    :return:
    """
    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())

 

posted @ 2020-09-03 13:50  QQmini  阅读(343)  评论(0编辑  收藏  举报