1.将IPv4地址转换为32位二进制格式,用做底层网络函数。

 import socket
from binascii import hexlify def convert_IPv4_address(): # 定义convert_IPv4_address()函数
for ip_addr in ['127.0.0.1', '192.168.0.1']:
packed_ip_addr = socket.inet_aton(ip_addr) # 将对应的IP地址转换为32—bit的封装包
unpacked_ip_addr = socket.inet_ntoa(packed_ip_addr) # 将对应的32-bit的封装包转换为IP地址的标准点号分隔字符串
print("IP Address: %s => packed: %s, Unpacked: %s" % (ip_addr, hexlify(packed_ip_addr), unpacked_ip_addr)) if __name__ == '__main__':
convert_IPv4_address()

2. .inet_aton()和inet_ntoa()及hexlify()解释

 def inet_aton(string):  # real signature unknown; restored from __doc__
"""
inet_aton(string) -> bytes giving packed 32-bit IP representation Convert an IP address in string format (123.45.67.89) to the 32-bit packed
binary format used in low-level network functions.
"""
return b""
"""将ip地址的4段地址分别进行2进制转化,输出用16进制表示
>>> import socket
>>> from binascii import hexlify
>>> a = socket.inet_aton('192.168.1.1')
>>> c= hexlify(a)
>>> c
b'c0a80101'
""" def inet_ntoa(packed_ip): # real signature unknown; restored from __doc__
"""
inet_ntoa(packed_ip) -> ip_address_string Convert an IP address from 32-bit packed binary format to string format
"""
pass
"""转换32位打包的IPV4地址转换为IP地址的标准点号分隔字符串表示。""" def hexlify(data): # known case of binascii.hexlify
"""
Hexadecimal representation of binary data. The return value is a bytes object.
"""
return b""
"""用十六进制形式表示二进制"""
05-01 02:51