A summary of some operational tips for using Python to convert IP

Source: Internet
Author: User
Tags unpack htons
The Python socket module contains some useful IP conversion functions, which are described below:

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

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

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

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

SOCKET.HTONL (x)     //htonl similar to C language (x)

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

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

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

Socket.inet_aton (ip_string)//C implementation dependent on Inet_aton

The Convert IPV4 address string (192.168.10.8) becomes a 32-bit packaged binary format (a binary string of 4 bytes in length), and it does not support IPV6. Inet_pton () supports Ipv4/ipv6 address format.

Socket.inet_ntoa (PACKED_IP)

Converts a 32-bit packaged IPV4 address to a standard dot-delimited string representation of an IP address.

Socket.inet_pton (address_family,ip_string)

The converted IP address string is packaged in binary format. The address families are af_inet and Af_inet6, which represent 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)) ' 190.10.58.64 ' # Convert from IP address string to integer value Defip2int (ip_string):     return Struct.unpack ("! I ", Socket.inet_aton (IP)) [0# converts the number from the network byte order to the IP address (dot delimited) def int2ip (IP):     return Socket.inet_ntoa (Struct.pack ("! I ", IP))

You can also simply use lambda functions to achieve IP and digital cross-transfer:
IP to Digital

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

The exact value of each index bit, that is, the value of J,i, is derived from the reversed cut index. Since the number range is 0~255 (a total of 256), the index bit is then exponentiation, and the value of itself is calculated, and summed. 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, you can use a simple algorithm to convert the number to IP, 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 previous example, I was first calculated as [3, 2, 1, 0], which is actually the value of the index bit, and X is the sum of the values we calculated above 3232235521. After dividing the number of points by 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.