码迷,mamicode.com
首页 > 编程语言 > 详细

python 将IP地址转换成打包后的32位格式

时间:2019-06-16 16:05:55      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:address   ack   pytho   int   soc   打包   bsp   %s   def   

 

python 2.7

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

 

python 将IP地址转换成打包后的32位格式

标签:address   ack   pytho   int   soc   打包   bsp   %s   def   

原文地址:https://www.cnblogs.com/sea-stream/p/11031509.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!