Can anyone think of an algorithm to put all of the addresses between two others and put them in a list (using python)? For example:

findIPs('111.111.111.0', '111.111.111.3')

Should return ('111.111.111.0', '111.111.111.1', '111.111.111.2', '111.111.111.3'). My main problem arises from when corresponding blocks are identical otherwise I could just use nested for loops with the range function

方法一:

try:
from ipaddress import ip_address
except ImportError:
from ipaddr import IPAddress as ip_address def findIPs(start, end):
start = ip_address(start)
end = ip_address(end)
result = []
while start <= end:
result.append(str(start))
start += 1
return result print(findIPs('111.111.111.0', '111.111.111.3'))

打印结果:

方法二:

import struct
import socket def findIPs(start, end):
ipstruct = struct.Struct('>I')
start, = ipstruct.unpack(socket.inet_aton(start))
end, = ipstruct.unpack(socket.inet_aton(end))
return [socket.inet_ntoa(ipstruct.pack(i)) for i in range(start, end+1)] print(findIPs('111.111.111.0', '111.111.111.3'))

原文地址:https://stackoverflow.com/questions/17220308/how-to-find-all-ip-addresses-between-2-ip-addresses

Python 计算两个IP段之间的有效IP地址的更多相关文章

  1. Python datetime库计算两个时间点之间的分钟(秒、天)数

    计算两个时间点之间的分钟数 import datetime def minNums(startTime, endTime): '''计算两个时间点之间的分钟数''' # 处理格式,加上秒位 start ...

  2. Java计算两个字符串日期之间的天数差

    Java计算两个字符串日期之间的天数差 调用方法: public static void main(String[] args) throws ParseException { String a = ...

  3. PHP计算两组经纬度坐标之间的距离

    定义π define('PI',3.1415926535898); define('EARTH_RADIUS',6378.137); 计算两组经纬度坐标 之间的距离 /** * 计算两组经纬度坐标 之 ...

  4. HTML5地理定位(已知经纬度,计算两个坐标点之间的距离)

    事实上,地球上任意两个坐标点在地平线上的距离并不是直线,而是球面的弧线. 下面介绍如何利用正矢公式计算已知经纬度数据的两个坐标点之间的距离.半正矢公式也成为Haversine公式,它最早时航海学中的重 ...

  5. tftp 限制ip 限制ip段 或者多个ip段访问

    1 限制单个ip访问 tftp 配置tftp信息 vi /etc/xinetd.d/tftp 在 service tftp配置信息中添加  only_form =ip 重启 service xinet ...

  6. 黄聪:中国大陆的所有IP段,中国电信所有IP段、中国铁通所有IP段、中国网通所有IP段。

    中国大陆的所有IP段,中国电信所有IP段.中国铁通所有IP段.中国网通所有IP段. 中国大陆的所有IP段: 47.153.128.0 47.154.255.25558.14.0.0 58.25.255 ...

  7. python 计算两个日期相差多少个月

    近期,由于业务需要计算两个日期之前相差多少个月.我在网上找了很久,结果发现万能的python,居然没有一个模块计算两个日期的月数,像Java.C#之类的高级语言,都会有(date1-date2).mo ...

  8. 计算两个latitude-longitude点之间的距离? (Haversine公式)

    问题描述 如何计算纬度和经度指定的两点之间的距离?为了澄清,我想要距离公里;这些点使用WGS84系统,我想了解可用方法的相对准确性.最佳解决方案 这个link可能对您有帮助,因为它详细说明了使用Hav ...

  9. php计算两个经纬度地点之间的距离(转)

    php计算两个指定的经纬度地点之间的距离,这个在做计算给定某个地点的经纬度,计算其附近的商业区,以及给定地点与附近各商业区之间的距离的时候,还是用的到的.下面是具体的函数代码以及用法示例. 关于如何获 ...

随机推荐

  1. socket编程—— 服务器遇到Broken Pipe崩溃

    我写了一个服务器程序, 在Linux下测试时, 总是莫名退出. 最后跟踪到是write调用导致退出. 用gdb执行程序, 退出时提示"Broken pipe". 最后问题确定为, ...

  2. UVA 624 CD(DP + 01背包)

    CD You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music i ...

  3. svn log笔记

    背景: svn,版本 1.7.8 以下所有命令直接和svn服务器进行交互并没有checkout代码到本地 1.查询分支从拉出来到现在的变更: svn log branche_url --stop-on ...

  4. Openerp 7.0消息推送

    在一个文档的state变化时,需要将变化情况告知关注用户,通过研究account.invoice的代码,发现是经过如下过程实现此功能的: 1.添加一个消息阶段: <record id=" ...

  5. webservice系统学习笔记9-使用契约优先的方式的一个服务端demo(隐式传Header信息)

    服务器端: 1.编写wsdl文件 <?xml version="1.0" encoding="UTF-8" standalone="no&quo ...

  6. python之函数用法isinstance()

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法isinstance() #isinstance() #说明:返回一个布尔值,判断数据 ...

  7. 基于 Vue BootStrap的迷你Chrome插件

    代码地址如下:http://www.demodashi.com/demo/14306.html 安装 安装 Visual Studio Code 和Chrome, 自行FQ 详细安装这里略过 安装包管 ...

  8. HighCharts/Highstock使用小结,使用汉化及中文帮助文档

       此文档是本人在开发过程图形报表时使用HighCharts所遇到的问题及解决方案 .最后附上有HighCharts中文帮助文档 HighCharts  版本:Highcharts-3.0.1 Hi ...

  9. Toast连续弹出的问题

    public class CommUtils { private static Toast toast = null; public static void showToast(int text) { ...

  10. 如何显示Firefox插件(Plugins)的完整路径并删除

    在Firefox里面,插件firefox只提供禁用选项,无法直接卸载. 要卸载插件,可以按照下面的步骤: 1.在地址栏内,输入 about:config,回车:在过滤器那一栏输入 plugin.exp ...