Converting an IPV4 address to different formats

Source: Internet
Author: User

#!/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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.