Tips for Using Python to convert IP addresses

Source: Internet
Author: User
Tags unpack htons
This article mainly introduces some tips for using Python to convert IP addresses, including using related functions and anonymous functions in the socket module, for more information about how to use the Python Socket module, see the following description:

Socket. ntohl (x) // similar to the C language ntohl (x)

Converts a 32-bit positive integer from the network order to the host byte order.

Socket. ntohs (x) // similar to ntohs (x) in C language)

Converts a 16-bit positive integer from the network order to the host byte order.

Socket. htonl (x) // similar to the C language htonl (x)

Converts a 32-bit positive integer from the host's Byte Order to the network order.

Socket. htons (x) // similar to the C language htons (x)

Converts a 16-bit positive integer from the host's Byte Order to the network order.

Socket. inet_aton (ip_string) // depends on the C implementation of inet_aton

Convert an IPV4 address string (192.168.10.8) to a 32-bit binary format (a four-byte binary string). IPV6 is not supported. Inet_ton () supports IPV4/IPV6 address formats.

socket.inet_ntoa(packed_ip)

Convert the 32-bit IPV4 address to the standard dot number separator string of the IP address.

socket.inet_pton(address_family,ip_string)

Convert the IP address string to the packaged binary format. The address families are AF_INET and AF_INET6, which indicate IPV4 and IPV6 respectively.

socket.inet_ntop(address_family,packed_ip)

Convert a packaged IP address to a standard string expression, for example, "5aef: 2b: 8" or "127.0.0.1 ".

>>> Import socket >>> import struct >>> socket. ntohl (struct. unpack ("I", socket. inet_aton ("10.10.58.64") [0]) 168442432L >>> socket. inet_ntoa (struct. pack ("I", socket. htonl (168442432L) '10. 10.58.64 '> struct. unpack ("= I", socket. inet_aton ("190.10.58.64") (1077545662,) >>> socket. inet_ntoa (struct. pack ("= I", 1077545662) '100. 10.58.64 '# convert from the IP address string to the integer defIp2Int (ip_string): return struct. unpack ("! I ", socket. inet_aton (ip) [0 # convert from a number in the byte sequence of the network to an ip address (separated by dots) def Int2Ip (ip): return socket. inet_ntoa (struct. pack ("! I ", ip ))

You can also use lambda functions to convert ip addresses and numbers:
Ip to numeric

>>> ip2num = lambda x:sum([256**j*int(i) for j,i in enumerate(x.split('.')[::-1])])>>> ip2num('192.168.0.1')3232235521

The specific value of each index bit is obtained through inverted cutting index, that is, the value of j and I. Because the number range is 0 ~ 255 (256 in total), so the index bit is used to calculate the power, and then the value is the product and sum. The specific decomposition is as follows:

>>> [256**j*int(i) for j,i in enumerate(x.split('.')[::-1])][1, 0, 11010048, 3221225472]>>> for j,i in enumerate(x.split('.')[::-1]):...   print j,i...0 11 02 1683 192

Digital to IP

Or the above IP address, you can use a simple algorithm to convert numbers into IP addresses, the specific code is as follows:

>>> num2ip = lambda x: '.'.join([str(x/(256**i)%256) for i in range(3,-1,-1)])>>> num2ip(3232235521)'192.168.0.1

In the above example, the I value is [3, 2, 1, 0]. In fact, this part is also the index bit value. x is the sum of the above sum value 3232235521. After dividing the number score by the secondary index of 256, the remainder is 256, which is the corresponding value of each index bit.

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.