Created
May 11, 2016 06:21
-
-
Save limboinf/00123bf7e4151fde5c154643e14b8b8e to your computer and use it in GitHub Desktop.
Python IPv4 Address Convert.
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
# coding=utf-8 | |
""" | |
desc.. | |
:copyright: (c) 2015 by fangpeng. | |
:license: MIT, see LICENSE for more details. | |
""" | |
import socket | |
from binascii import hexlify # 以十六进制表示二进制数据 | |
def convert_ipv4_address(): | |
for ip_addr in ['127.0.0.1', '192.168.0.1']: | |
packed_ip_addr = socket.inet_aton(ip_addr) | |
unpacked_ip_addr = socket.inet_ntoa(packed_ip_addr) | |
print "IP Address: %s , Packed: %s, Unpacked:%s" % \ | |
(ip_addr, hexlify(packed_ip_addr), unpacked_ip_addr) | |
if __name__ == '__main__': | |
convert_ipv4_address() | |
# Out: | |
# IP Address: 127.0.0.1 , Packed: 7f000001, Unpacked:127.0.0.1 | |
# IP Address: 192.168.0.1 , Packed: c0a80001, Unpacked:192.168.0.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment