本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《
阿里云开发者社区用户服务协议
》和
《
阿里云开发者社区知识产权保护指引
》。如果您发现本社区中有涉嫌抄袭的内容,填写
侵权投诉表单
进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
with open(file,'r',encoding='utf-8') as f:
allip = f.readlines()
ip = [ ipaddress.IPv4Address(ip.strip('\n')) for ip in allip ]
if flag == '-h':
for i in ip:
print(hex(int(i)))
else:
for i in ip:
print(bin(int(i)))
except Exception as e:
print(e)
def hex_bin2ip(file,*args,**kwargs):
with open(file,'r',encoding='utf-8') as f:
allip = f.readlines()
for i in allip:
a = i.strip('\n')
print(ipaddress.IPv4Address(int(a,0)))
except Exception as e:
print(e)
if __name__ == '__main__':
flag = sys.argv[1]
file = sys.argv[2]
IP = {
'-b':ip2hex_bin,
'-h':ip2hex_bin,
'-i':hex_bin2ip
IP.get(flag)(file)
except Exception as e:
print(sys.argv[0],'[-b|-h|-i] file')
github链接地址:
https://github.com/babyshen/Python/blob/master/ip_conversion.py