请编写一个函数实现将IP地址转换成一个整数。(3分)

如 面试题小结01---实现将IP地址转换成一个整数10.3.9.12 

 转换规则为:
        10            00001010

         3            00000011

         9            00001001

        12            00001100

再将以上二进制拼接起来计算十进制结果:00001010 00000011 00001001 00001100 = ?
#用Python获取本机ip地址
from socket import gethostbyname_ex, gethostname

local_IP_list = gethostbyname_ex(gethostname())
local_IP = gethostbyname_ex(gethostname())[2][2]
print(local_IP_list)
print(local_IP)


本题答案:
def ipfunc(ip):
    a = ip.split('.')
    s = ''
    l = []
    for i in a:
        i = bin(int(i))[2:]
        i = i.rjust(8, '0')
        l.append(i)
    s = s.join(l)
    return s


ipfunc(local_IP) 

相关文章:

  • 2021-07-31
  • 2022-12-23
  • 2021-06-26
  • 2022-12-23
  • 2021-12-22
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-23
  • 2021-06-24
  • 2021-12-09
  • 2022-01-19
  • 2022-01-03
  • 2022-12-23
相关资源
相似解决方案