当前位置:Gxlcms > Python > ip、数字的互转

ip、数字的互转

时间:2021-07-01 10:21:17 帮助过:49人阅读

ip、数字互转

# ip ==> 数字
>>> ip2num = lambda x:sum([256**j*int(i) for j,i in enumerate(x.split('.')[::-1])])
>>> ip2num('192.168.0.1')

3232235521

# 数字 ==> ip # 数字范围[0, 255^4]
>>> num2ip = lambda x: '.'.join([str(x/(256**i)%256) for i in range(3,-1,-1)])
>>> num2ip(3232235521)

'192.168.0.1'

人气教程排行