Python IP处理模块IPy(转载)

简介: 安装先下载源码,地址:ps://pypi.python.org/pypi/IPy/">https://pypi.python.org/pypi/IPy/ ,然后解压后使用命令python setup.py install安装。

安装

先下载源码,地址:ps://pypi.python.org/pypi/IPy/">https://pypi.python.org/pypi/IPy/ ,然后解压后使用命令python setup.py install安装。

使用

1、显示IP类型

IP('192.168.1.1').version()
4
IP('::1').version()
6
类似如上所示,通过version方法可以的判断输入的IP是IPv4还是IPv6 。

2、网段计算输出

代码:

from IPy import IP

ip=IP('192.168.0.0/28')
print ip.len()
for x in ip:
print x

print ip.strNormal(0)
print ip.strNormal(1)
print ip.strNormal(2)
print ip.strNormal(3)
len()方法可以计算网段的IP个数。

strNormal()方法指定不同wantprefixlen参数可以定制不同类型的输出。上面输出类似如下:

16
192.168.0.0
192.168.0.1
192.168.0.2
192.168.0.3
......
192.168.0.15
192.168.0.0
192.168.0.0/28
192.168.0.0/255.255.255.240
192.168.0.0-192.168.0.15
3、格式转换

实例介绍几个常用方法,包括方向解析名称、IP类型、IP进制转换、网络地址网段地址转换。

ip=IP('192.168.0.1')
print ip.reverseNames() #反向解析地址格式

print ip.iptype() #显示IP地址类型,私有还是公有
ip=IP('8.8.8.8')
print ip.iptype()

print ip.int() #转换成整型格式
print ip.strHex() #转换成十六进制格式
print ip.strBin() #转换成二进制格式

网络地址、网段地址格式转换

print (IP('192.168.1.0').make_net('255.255.255.0'))
print (IP('192.168.1.0/255.255.255.0',make_net=True))
print (IP('192.168.1.0-192.168.1.255',make_net=True))
4、地址比较

判断IP地址和网段是否包含于另一个网段中,如下:

'192.168.1.1' in IP('192.168.1.0/24')
True
IP('192.168.1.0/24') in IP('192.168.0.0/16')
True
判断两个网段是否存在重叠,如下:

IP('192.168.0.0/23').overlaps('192.168.1.0/24')
1
IP('192.168.1.0/24').overlaps('192.168.2.0')
0
其中1表示存在重叠,0表示不存在重叠。

举例

代码:

coding:utf-8

from IPy import IP

ip_s=raw_input("please input an IP or net-range: ")
ips=IP(ip_s)

if len(ips)>1: #网络地址
print('net: %s' % ips.net())
print('netmask: %s' % ips.netmask())
print('broadcast: %s' % ips.broadcast())
print('reverse address: %s' % ips.reverseNames()[0])
print('subnet: %s' % len(ips))
else: #单个地址
print('reverse address: %s' % ips.reverseNames()[0])

print('hexadecimal: %s' % ips.strHex())
print('binary: %s' % ips.strBin())
print('iptype: %s' % ips.iptype())
运行结果:

C:\Users\admin\workspace\zhangnq>python IPy_test2.py
please input an IP or net-range: 192.168.1.1
reverse address: 1.1.168.192.in-addr.arpa.
hexadecimal: 0xc0a80101
binary: 11000000101010000000000100000001
iptype: PRIVATE

C:\Users\admin\workspace\zhangnq>python IPy_test2.py
please input an IP or net-range: 8.8.8.8
reverse address: 8.8.8.8.in-addr.arpa.
hexadecimal: 0x8080808
binary: 00001000000010000000100000001000
iptype: PUBLIC

C:\Users\admin\workspace\zhangnq>python IPy_test2.py
please input an IP or net-range: 192.168.1.0/28
net: 192.168.1.0
netmask: 255.255.255.240
broadcast: 192.168.1.15
reverse address: 0.1.168.192.in-addr.arpa.
subnet: 16
hexadecimal: 0xc0a80100
binary: 11000000101010000000000100000000
iptype: PRIVATE

ipy模块用法

一个自动识别IP地址、子网、方向解析、IP类型等信息的脚本

!/usr/bin/env python

-- coding: utf-8 --

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">def ip():
try:
from IPy import IP ###加载模块
ip_s = raw_input('请输入IP地址或者网段地址:' )###输入一个IP地址或者网段
ips = IP(ip_s) #定义元素
if len(ips) > 1: #如果len出来的数字大于1,那么就是一个网段
print('网络地址: %s' % ips.net())
print('子网掩码: %s' % ips.netmask())
print('网络广播地址: %s' % ips.reverseNames() [0])
print('网络子网数: %s' % len(ips))
else: ###否则就是一个地址
print('IP反向解析: %s' % ips.reverseNames() [0])
print('十六进制地址: %s' % ips.strHex())
print('二进制地址: %s' % ips.strBin())
print('地址类型: %s' % ips.iptype())
print time.strftime("%Y-%m-%d %H:%M:%S")
#code
except Exception, e:
logging.info("error:" + str(e) + "\n" + traceback.format_exc())
print traceback.format_exc()
finally:
pass</pre>

运行效果:

[root@mylinuxer python]# 192.168.1.0/24
-bash: 192.168.1.0/24: No such file or directory
[root@mylinuxer python]# python python.py
请输入IP地址或者网段地址: 192.168.1.0/24
网络地址: 192.168.1.0
子网掩码: 255.255.255.0
网络广播地址: 1.168.192.in-addr.arpa.
网络子网数: 256

[root@mylinuxer python]# python python.py
请输入IP地址或者网段地址: 192.168.1.1
IP反向解析: 1.1.168.192.in-addr.arpa.
十六进制地址: 0xc0a80101
二进制地址: 11000000101010000000000100000001
地址类型: PRIVATE

[root@mylinuxer python]# python python.py
请输入IP地址或者网段地址: 116.213.249.211
IP反向解析: 211.249.213.116.in-addr.arpa.
十六进制地址: 0x74d5f9d3
二进制地址: 01110100110101011111100111010011
地址类型: PUBLIC

目录
相关文章
|
1月前
|
SQL 关系型数据库 数据库
Python SQLAlchemy模块:从入门到实战的数据库操作指南
免费提供Python+PyCharm编程环境,结合SQLAlchemy ORM框架详解数据库开发。涵盖连接配置、模型定义、CRUD操作、事务控制及Alembic迁移工具,以电商订单系统为例,深入讲解高并发场景下的性能优化与最佳实践,助你高效构建数据驱动应用。
276 7
|
1月前
|
监控 安全 程序员
Python日志模块配置:从print到logging的优雅升级指南
从 `print` 到 `logging` 是 Python 开发的必经之路。`print` 调试简单却难维护,日志混乱、无法分级、缺乏上下文;而 `logging` 支持级别控制、多输出、结构化记录,助力项目可维护性升级。本文详解痛点、优势、迁移方案与最佳实践,助你构建专业日志系统,让程序“有记忆”。
215 0
|
1月前
|
JSON 算法 API
Python中的json模块:从基础到进阶的实用指南
本文深入解析Python内置json模块的使用,涵盖序列化与反序列化核心函数、参数配置、中文处理、自定义对象转换及异常处理,并介绍性能优化与第三方库扩展,助你高效实现JSON数据交互。(238字)
308 4
|
1月前
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
227 0
|
1月前
|
XML JSON 数据处理
超越JSON:Python结构化数据处理模块全解析
本文深入解析Python中12个核心数据处理模块,涵盖csv、pandas、pickle、shelve、struct、configparser、xml、numpy、array、sqlite3和msgpack,覆盖表格处理、序列化、配置管理、科学计算等六大场景,结合真实案例与决策树,助你高效应对各类数据挑战。(238字)
164 0
|
2月前
|
安全 大数据 程序员
Python operator模块的methodcaller:一行代码搞定对象方法调用的黑科技
`operator.methodcaller`是Python中处理对象方法调用的高效工具,替代冗长Lambda,提升代码可读性与性能。适用于数据过滤、排序、转换等场景,支持参数传递与链式调用,是函数式编程的隐藏利器。
116 4
|
2月前
|
存储 数据库 开发者
Python SQLite模块:轻量级数据库的实战指南
本文深入讲解Python内置sqlite3模块的实战应用,涵盖数据库连接、CRUD操作、事务管理、性能优化及高级特性,结合完整案例,助你快速掌握SQLite在小型项目中的高效使用,是Python开发者必备的轻量级数据库指南。
276 0
|
3月前
|
存储 安全 数据处理
Python 内置模块 collections 详解
`collections` 是 Python 内置模块,提供多种高效数据类型,如 `namedtuple`、`deque`、`Counter` 等,帮助开发者优化数据处理流程,提升代码可读性与性能,适用于复杂数据结构管理与高效操作场景。
317 0
|
4月前
|
数据安全/隐私保护 Python
抖音私信脚本app,协议私信群发工具,抖音python私信模块
这个实现包含三个主要模块:抖音私信核心功能类、辅助工具类和主程序入口。核心功能包括登录
|
4月前
|
数据采集 机器学习/深度学习 边缘计算
Python爬虫动态IP代理报错全解析:从问题定位到实战优化
本文详解爬虫代理设置常见报错场景及解决方案,涵盖IP失效、403封禁、性能瓶颈等问题,提供动态IP代理的12种核心处理方案及完整代码实现,助力提升爬虫系统稳定性。
345 0

推荐镜像

更多
下一篇
oss云网关配置