使用python获取Beagleboard的IP地址 [英] Getting IP address of the beagleboard using python

查看:111
本文介绍了使用python获取Beagleboard的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SIOCGIFADDR = 0x8915

def getIpAddr(iface = 'eth0'):

     ifreq = struct.pack('16sH14s', iface, socket.AF_INET, '\x00'*14)
     try:
         res = fcntl.ioctl(sock, SIOCGIFADDR, ifreq)
     except:
         return None   
     ip = struct.unpack('16sH2x4s8x', res)[2]
     return socket.inet_ntoa(ip)

在每个步骤中,函数的返回值是多少? 而且,什么是SIOCGIFADDR? 另外,为什么在unpack()函数之后使用[2]?

At each step what are the return values of the functions? And, what is SIOCGIFADDR? Also, why [2] has been used following the unpack() function?

推荐答案

SIOCGIFADDR:代表获取Internet接口地址,表示'eth0'. 这是写入地址0x8915的CPU宏. 您无法访问该cpu地址,因此必须使用参数来进行打包和解包功能 "16sH2x4s8x" 您要从机器获取的IP地址有4个字段,例如"192.168.5.20" 因此,同样需要(4 * 4)16来搜索包拆包功能的更多字段.

SIOCGIFADDR : is stands for get internet interface address means 'eth0'. This is CPU macro which is written at address 0x8915. You cant take access of that cpu address so you have to go through pack and unpack function using parameters "16sH2x4s8x" IP address which u want from machine it have 4 fields like "192.168.5.20" so that (4*4) 16 is required likewise search more fields of pack unpack functions.

这篇关于使用python获取Beagleboard的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆