Skip to content

Instantly share code, notes, and snippets.

@lowstz
Created April 2, 2013 11:34
Show Gist options
  • Save lowstz/5291586 to your computer and use it in GitHub Desktop.
Save lowstz/5291586 to your computer and use it in GitHub Desktop.
ip转换
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def ip2int(ip):
return sum(
int(i)*(256**pos)
for pos, i in enumerate(reversed( ip.split('.')))
)
def int2ip(num):
s = hex(num).lstrip('0x').zfill(8)
t = iter(s)
return '.'.join(str(int(a+b,16)) for a,b in zip(t,t))
if __name__ == "__main__":
ip = '7.233.233.172'
print ip2int(ip)
print int2ip(ip2int(ip))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment