python ip 地址转换

本文涉及的产品
公网NAT网关,每月750个小时 15CU
简介:

# 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
相关文章
|
1月前
|
缓存 API 定位技术
使用Python调用百度地图API实现地址查询
使用Python调用百度地图API实现地址查询
104 0
|
1月前
|
网络协议 网络安全 数据库
python验证公网ip与内网ip
python验证公网ip与内网ip
27 0
|
1月前
|
数据采集 安全 数据安全/隐私保护
python怎么获取免费代理IP
python怎么获取免费代理IP
49 0
|
3月前
|
Android开发 Python
Python封装ADB获取Android设备wifi地址的方法
Python封装ADB获取Android设备wifi地址的方法
61 0
|
2月前
|
网络协议 网络性能优化 Python
在Python中进行TCP/IP网络编程
在Python中进行TCP/IP网络编程
34 6
|
6天前
|
数据挖掘 API 数据安全/隐私保护
python请求模块requests如何添加代理ip
python请求模块requests如何添加代理ip
|
7天前
|
存储 关系型数据库 MySQL
Python搭建代理IP池实现存储IP的方法
Python搭建代理IP池实现存储IP的方法
|
7天前
|
Python
Python动态IP代理防止被封的方法
Python动态IP代理防止被封的方法
|
7天前
|
存储 API Python
python之代理ip的配置与调试
python之代理ip的配置与调试
|
7天前
|
存储 Python
用Python实现批量下载文件——代理ip排除万难
用Python实现批量下载文件——代理ip排除万难