Python 计算两个IP段之间的有效IP地址
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地址的更多相关文章
- Python datetime库计算两个时间点之间的分钟(秒、天)数
计算两个时间点之间的分钟数 import datetime def minNums(startTime, endTime): '''计算两个时间点之间的分钟数''' # 处理格式,加上秒位 start ...
- Java计算两个字符串日期之间的天数差
Java计算两个字符串日期之间的天数差 调用方法: public static void main(String[] args) throws ParseException { String a = ...
- PHP计算两组经纬度坐标之间的距离
定义π define('PI',3.1415926535898); define('EARTH_RADIUS',6378.137); 计算两组经纬度坐标 之间的距离 /** * 计算两组经纬度坐标 之 ...
- HTML5地理定位(已知经纬度,计算两个坐标点之间的距离)
事实上,地球上任意两个坐标点在地平线上的距离并不是直线,而是球面的弧线. 下面介绍如何利用正矢公式计算已知经纬度数据的两个坐标点之间的距离.半正矢公式也成为Haversine公式,它最早时航海学中的重 ...
- tftp 限制ip 限制ip段 或者多个ip段访问
1 限制单个ip访问 tftp 配置tftp信息 vi /etc/xinetd.d/tftp 在 service tftp配置信息中添加 only_form =ip 重启 service xinet ...
- 黄聪:中国大陆的所有IP段,中国电信所有IP段、中国铁通所有IP段、中国网通所有IP段。
中国大陆的所有IP段,中国电信所有IP段.中国铁通所有IP段.中国网通所有IP段. 中国大陆的所有IP段: 47.153.128.0 47.154.255.25558.14.0.0 58.25.255 ...
- python 计算两个日期相差多少个月
近期,由于业务需要计算两个日期之前相差多少个月.我在网上找了很久,结果发现万能的python,居然没有一个模块计算两个日期的月数,像Java.C#之类的高级语言,都会有(date1-date2).mo ...
- 计算两个latitude-longitude点之间的距离? (Haversine公式)
问题描述 如何计算纬度和经度指定的两点之间的距离?为了澄清,我想要距离公里;这些点使用WGS84系统,我想了解可用方法的相对准确性.最佳解决方案 这个link可能对您有帮助,因为它详细说明了使用Hav ...
- php计算两个经纬度地点之间的距离(转)
php计算两个指定的经纬度地点之间的距离,这个在做计算给定某个地点的经纬度,计算其附近的商业区,以及给定地点与附近各商业区之间的距离的时候,还是用的到的.下面是具体的函数代码以及用法示例. 关于如何获 ...
随机推荐
- JSP的页面连接和提交方式(web基础学习笔记六)
一.GET请求新页面 1.1.超链接请求新页面 <!-- 超链接到page2 --> <a href="page2.jsp">链接到page2</a& ...
- IDC 知识库
http://www.51idc.com/help/supportHelp.html Host key verification failed解决IIS错误信息--另一个程序正在使用此文件,进程无法访 ...
- Hat’s Words(字典树)
Problem Description A hat's word is a word in the dictionary that is the concatenation of exactly tw ...
- jquery-序列化表单
createTime--2016年9月25日08:54:48参考链接:http://www.w3school.com.cn/tags/html_ref_urlencode.htmljQuery的s ...
- maven Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4
maven Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4 CreateTime--201 ...
- 3DTouch - iOS新特性
概述 3DTouch是一种立体触控技术,被苹果称为新一代多点触控技术. 详细 代码下载:http://www.demodashi.com/demo/10708.html 6s和6s plus之后特有效 ...
- PHP-中文在计算机中的存储
经常我们打开外国网站的时候出现乱码,又或者打开很多非英语的外国网站的时候,显示的都是口口口口口的字符, WordPress程序是用的UTF-8,很多cms用的是GB2312. ● 为什么有这么多编码? ...
- no !/ in spec
问题: 在学习hibernate的过程中涉及到hibernate.cfg.xml和hibernate.hbm.xml配置文件的编写,写完之后配置文件出现黄色感叹号,鼠标悬停在感叹号上时显示no !/ ...
- C#将Access数据库导出为JSON
一个Access数据库包含若干首诗歌,每首诗有content.author.title.description四个字段 using System; using System.Data; using S ...
- Atom 检测php错误扩展linter-php