python 将IP地址转换成打包后的32位格式
2020-12-13 02:19
标签:address ack pytho int soc 打包 bsp %s def python 2.7 输出 python 将IP地址转换成打包后的32位格式 标签:address ack pytho int soc 打包 bsp %s def 原文地址:https://www.cnblogs.com/sea-stream/p/11031509.html#!/usr/bin/env python
# Python Network Programming Cookbook -- Chapter - 1
# This program requires Python 2.7 or any later version
import socket
from binascii import hexlify
def convert_ip4_address():
for ip_addr in [‘127.0.0.1‘, ‘192.168.0.1‘]:
packed_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_ip4_address()
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
上一篇:webservice发送字符串