#!/usr/bin/env pythonimport socketfrom binascii import hexlifydef convert_ipv4_address (): For ip_addr in [' 127.0.0.1 ', ' 192.168.0.1 ']:p acked_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'll use the Them:inet_aton () and Inet_ntoa (). Let us create the
Convert_ip4_address () Function,where Inet_aton () and Inet_ntoa () would be used for
The IP address conversion. We'll use the 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