树莓派使用python3获取网卡IP地址
今天捣鼓树莓派遇到到问题,用Python3获得本地IP地址,首先想到用socket.gethostbyname,不废话,直接上代码!
import socket
import fcntl
import struct
def get_ip_address(ethname):
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915,
struct.pack(b'256s',b'wlan0')
)[20:24])
print get_ip_address("lo")
print get_ip_address("eth0")
print get_ip_address("wlan0")
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/cy.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/hanx.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/huaix.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/tx.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/se.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/wx.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/zk.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/shui.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/kuk.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/lh.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/gz.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/ku.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/kel.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/yiw.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/yun.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/jy.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/dy.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/gg.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/fn.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/fendou.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/shuai.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/kl.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/pj.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/fan.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/lw.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/qiang.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/ruo.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/ws.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/ok.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/gy.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/qt.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/cj.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/aini.gif)
![](https://cdn.hlhasd.com/wp-content/themes/tow/images/smilies/bu.gif)