码迷,mamicode.com
首页 > 其他好文 > 详细

Converting an IPv4 address to different formats

时间:2015-12-21 16:01:03      阅读:173      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!