1. 程式人生 > >1.Python中IP地址處理IPy模塊

1.Python中IP地址處理IPy模塊

exc rom 進制轉換 使用 targe adc put efi mes

安裝

先下載源碼,地址: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 -*-

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


運行效果:

[[email protected] python]# 192.168.1.0/24
-bash: 192.168.1.0/24: No such file or directory
[[email protected] 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

[[email protected] python]# python python.py
請輸入IP地址或者網段地址: 192.168.1.1
IP反向解析: 1.1.168.192.in-addr.arpa.
十六進制地址: 0xc0a80101
二進制地址: 11000000101010000000000100000001
地址類型: PRIVATE

[[email protected] python]# python python.py
請輸入IP地址或者網段地址: 116.213.249.211
IP反向解析: 211.249.213.116.in-addr.arpa.
十六進制地址: 0x74d5f9d3
二進制地址: 01110100110101011111100111010011
地址類型: PUBLIC

1.Python中IP地址處理IPy模塊