请编写一个函数实现将IP地址转换成一个整数。(3分)
如 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)
如 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)