__MagicPower
Do one thing and do it best!

导航

 
#!/usr/bin/env python

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()

Description:

The Python socket library has utilities to deal with the various IP address formats.

Here, we will use two of them: inet_aton() and inet_ntoa(). Let us create the

convert_ip4_address() function,where inet_aton() and inet_ntoa() will be used for

the IP address conversion.We will use two sample IP addresses, 127.0.0.1 and 192.168.0.1.

 

TEST:

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
[Finished in 0.1s]

posted on 2015-12-21 14:56  __MagicPower  阅读(172)  评论(0编辑  收藏  举报