首页 > 其他 > 详细

Converting an IPv4 address to different formats

时间:2015-12-21 16:01:00      阅读:193      评论:0      收藏:0      [点我收藏+]
#!/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]

Converting an IPv4 address to different formats

原文:http://www.cnblogs.com/magicpower/p/5063464.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!