首页 > 代码库 > python IP地址十六进制二进制间相互转换
python IP地址十六进制二进制间相互转换
# -*- coding:utf-8 -*- import ipaddress,sys def ip2hex_bin(file,*args,**kwargs): try: with open(file,‘r‘,encoding=‘utf-8‘) as f: allip = f.readlines() ip = [ ipaddress.IPv4Address(ip.strip(‘\n‘)) for ip in allip ] if flag == ‘-i‘: 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): try: 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__‘: try: flag = sys.argv[1] file = sys.argv[2] IP = { ‘-i‘:ip2hex_bin, ‘-b‘:ip2hex_bin, ‘-h‘:hex_bin2ip } IP.get(flag)(file) except Exception as e: print(sys.argv[0],‘[-i|-b|-h] file‘)
github链接地址:https://github.com/babyshen/Python/blob/master/ip_conversion.py
本文出自 “baby神” 博客,请务必保留此出处http://babyshen.blog.51cto.com/8405584/1875142
python IP地址十六进制二进制间相互转换
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。