Created
April 2, 2013 11:34
-
-
Save lowstz/5291586 to your computer and use it in GitHub Desktop.
ip转换
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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