树莓派读取ip

# -*- coding:utf-8 -*-
#@author:YouLei
#@time:2020/05/27=11:20
import time
import socket
time.sleep(2)

#获取本级制定接口的ip地址
def get_ip_address():
        s =socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
        #print(s)
        s.connect(("1.1.1.1",80))
        #print(s)
        ipaddr=s.getsockname()[0]
        s.close()
        return ipaddr

try:
    ip=get_ip_address()#获取 wlan0的网卡
    print("wln0_IP:",ip)
except:
    print("错误:没有连接Wi-Fi!!!!")
    time.sleep(5)
    exit()
time.sleep(10)


"""

#!/bin/sh

python3 /home/pi/tools/ip.py 
echo "ok"

1、修改后缀sh变成command:hello.sh->hello.command

2、修改可执行权限:chmod u+x hello.command
"""
原文地址:https://www.cnblogs.com/penuel/p/13534739.html