python ip 地址转换

# vim: tabstop=4 shiftwidth=4 softtabstop=4


# Copyright (c) 2011 X.commerce, a business unit of eBay Inc.

# Copyright 2010 United States Government as represented by the

# Administrator of the National Aeronautics and Space Administration.

# All Rights Reserved.

#

# Licensed under the Apache License, Version 2.0 (the "License"); you may

# not use this file except in compliance with the License. You may obtain

# a copy of the License at

#

# http://www.apache.org/licenses/LICENSE-2.0

#

# Unless required by applicable law or agreed to in writing, software

# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT

# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the

# License for the specific language governing permissions and limitations

# under the License.

"""


"""





from socket import inet_aton ,inet_ntoa

from struct import unpack , pack

import sys

import math

from nova import log as logging

from nova import flags

from nova import utils



LOG = logging.getLogger(__name__)


FLAGS = flags.FLAGS



def long2ip(ip):

""" long into ip str type """

return inet_ntoa(pack("!L", ip))



def num2masklen(n):

""" Computeing number size to masklen """

return 32 - int(math.log(n,2))


def ip2long(ip_addr):

""" Ip into long type """

return unpack("!L", inet_aton(ip_addr))[0]


def unsigned32(n):

return n & 0xffffffff


def masklen2maskbit(n):

""" Computing mask is int """

return unsigned32(~((1 << (32-n)) - 1))


def masklen2size(n):

""" Computing network bit : 1 << (32 - n )"""

return (1 << (32-n))


def load_used_ip_list():

subnet = dict()

cidrs = list()

cidrs = []#[u'172.122.0.0/29', u'172.122.0.8/29', u'172.122.0.32/28',u'172.122.0.48/28', u'172.122.0.240/28',u'172.122.1.240/28']

for net in cidrs:

ip_str = net.split('/')[0]

netBit = net.split('/')[1]

subnet[ip_str] = dict()

subnet[ip_str] = get_net_info(ip_str,int(netBit))

return subnet


def be_used(ip):

""" """

#for item in used_ip_list:

#used_ip_list = [{'net': 2893676544, 'bcast': 2893676799}]

#print net , used_net[net]

used_net = load_used_ip_list()

for net in used_net:

if ip >= used_net[net]['net'] and ip <= used_net[net]['bcast']:

return True

return False


def get_net_info(ip_str, masklen):

""" From openstack db read used cidr """

netInfo = dict()

netInfo['ip'] =ip2long(ip_str)

netInfo['maskbit'] = masklen2maskbit(masklen)

netInfo['size'] = masklen2size(masklen)

netInfo['net'] = netInfo['ip'] & netInfo['maskbit']

netInfo['bcast'] = netInfo['net'] + netInfo['size'] - 1

print netInfo

return netInfo


def work():

# Initializes the network range

big_ip_str = '172.122.0.0'

big_masklen = 16

big_ip = ip2long(big_ip_str)

big_maskbit = masklen2maskbit(big_masklen)

big_len = masklen2size(big_masklen)

big_net = big_ip & big_maskbit

big_bcast = big_net + big_len - 1



masklen = num2masklen(4)

ip = big_net

maskbit = masklen2maskbit(masklen)

n = masklen2size(masklen)


while(ip <= big_bcast):

net = ip & maskbit

bcast = net + n - 1

if be_used(net) or be_used(bcast):

ip = bcast + 1

print ""+long2ip(ip)+"---"+str(n)+"--"+long2ip(bcast)

continue

else:

#print "============++++++++++>"+long2ip(ip)+"/"+str(masklen)

return long2ip(ip)+"/"+str(masklen)

break



if __name__ == "__main__":

sc = work()

print sc ,'sc'



本文转自 swq499809608 51CTO博客,原文链接:http://blog.51cto.com/swq499809608/1298760
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值