安裝
先下載源碼,地址: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
類似如上所示,通過(guò)version方法可以的判斷輸入的IP是IPv4還是IPv6 。
2、網(wǎng)段計(jì)算輸出
代碼:
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()方法可以計(jì)算網(wǎng)段的IP個(gè)數(shù)。
strNormal()方法指定不同wantprefixlen參數(shù)可以定制不同類型的輸出。上面輸出類似如下:
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、格式轉(zhuǎn)換
實(shí)例介紹幾個(gè)常用方法,包括方向解析名稱、IP類型、IP進(jìn)制轉(zhuǎn)換、網(wǎng)絡(luò)地址網(wǎng)段地址轉(zhuǎn)換。
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() #轉(zhuǎn)換成整型格式 print ip.strHex() #轉(zhuǎn)換成十六進(jìn)制格式 print ip.strBin() #轉(zhuǎn)換成二進(jìn)制格式 #網(wǎng)絡(luò)地址、網(wǎng)段地址格式轉(zhuǎn)換 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地址和網(wǎng)段是否包含于另一個(gè)網(wǎng)段中,如下:
>>> '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
判斷兩個(gè)網(wǎng)段是否存在重疊,如下:
>>> 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: #網(wǎng)絡(luò)地址 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: #單個(gè)地址 print('reverse address: %s' % ips.reverseNames()[0]) print('hexadecimal: %s' % ips.strHex()) print('binary: %s' % ips.strBin()) print('iptype: %s' % ips.iptype())
運(yùn)行結(jié)果:
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模塊用法
一個(gè)自動(dòng)識(shí)別IP地址、子網(wǎng)、方向解析、IP類型等信息的腳本
#!/usr/bin/env python # -*- coding: utf-8 -*- def ip(): try: from IPy import IP ###加載模塊 ip_s = raw_input('請(qǐng)輸入IP地址或者網(wǎng)段地址:' )###輸入一個(gè)IP地址或者網(wǎng)段 ips = IP(ip_s) #定義元素 if len(ips) > 1: #如果len出來(lái)的數(shù)字大于1,那么就是一個(gè)網(wǎng)段 print('網(wǎng)絡(luò)地址: %s' % ips.net()) print('子網(wǎng)掩碼: %s' % ips.netmask()) print('網(wǎng)絡(luò)廣播地址: %s' % ips.reverseNames() [0]) print('網(wǎng)絡(luò)子網(wǎng)數(shù): %s' % len(ips)) else: ###否則就是一個(gè)地址 print('IP反向解析: %s' % ips.reverseNames() [0]) print('十六進(jìn)制地址: %s' % ips.strHex()) print('二進(jìn)制地址: %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
運(yùn)行效果:
[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 請(qǐng)輸入IP地址或者網(wǎng)段地址: 192.168.1.0/24 網(wǎng)絡(luò)地址: 192.168.1.0 子網(wǎng)掩碼: 255.255.255.0 網(wǎng)絡(luò)廣播地址: 1.168.192.in-addr.arpa. 網(wǎng)絡(luò)子網(wǎng)數(shù): 256 [root@mylinuxer python]# python python.py 請(qǐng)輸入IP地址或者網(wǎng)段地址: 192.168.1.1 IP反向解析: 1.1.168.192.in-addr.arpa. 十六進(jìn)制地址: 0xc0a80101 二進(jìn)制地址: 11000000101010000000000100000001 地址類型: PRIVATE [root@mylinuxer python]# python python.py 請(qǐng)輸入IP地址或者網(wǎng)段地址: 116.213.249.211 IP反向解析: 211.249.213.116.in-addr.arpa. 十六進(jìn)制地址: 0x74d5f9d3 二進(jìn)制地址: 01110100110101011111100111010011 地址類型: PUBLIC
總結(jié)
以上所述是小編給大家介紹的Python中IP地址處理IPy模塊的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
