python學習-IPy模組

來源:互聯網
上載者:User

標籤:IPy

用於處理IPv4和IPv6地址和網路的類和工具

IPy - 用於處理IPv4和IPv6地址和網路的類和工具。

#! /env python3#coding=utf-8#使用pip install IPy,最新版本為0.83'''IP地址、網段的基本處理'''import IPyfrom IPy import IP #調用IPip_v4 = IP('192.168.1.0/24').version() #判斷ipv4位址區段的類型ip_v6 = IP('::1').version() #判斷ipv6位址區段的類型print (ip_v4)print (ip_v6)ip = IP('192.168.1.0/24') #輸入192.168.1.0/24網段print (ip.len()) #192.168.1.0/24的網段的IP個數for x in ip:    print (x)  #輸出192.168.1.0/24網段所有的ip清單''''解析名稱、IP類型、IP轉換等'''ip_adr = IP('192.168.1.20')print (ip_adr.reverseNames()) #反向解析地址格式ip_adr_type = IP('8.8.8.8').iptype() #判斷8.8.8.8為公網地址還是私網地址print (ip_adr_type)ip_adr_int = IP('8.8.8.8').int() #將地址轉換為整數形式ip_adr_hex = IP('8.8.8.8').strHex() #轉換為16進位格式ip_adr_bin = IP('8.8.8.8').strBin() #轉換為二進位格式print (ip_adr_int)print (ip_adr_hex)print (ip_adr_bin)'''網路位址轉譯,例如根據IP與掩碼生產網段格式'''ip_net = IP('192.168.1.0')print (ip_net.make_net('255.255.255.0')) #擷取192.168.1.0與掩碼255.255.255.0計算,CDR格式,192.168.1.0/24print (IP('192.168.1.0/255.255.255.0',make_net=True)) #將192.168.1.0/255.255.255.0地址格式轉換為192.168.1.0/24print (IP('192.168.1.0-192.168.1.255',make_net=True)) #將192.168.1.0-192.168.1.255位址區段轉換為192.168.1.0/24格式'''通過strNormal方法指定不同wanprefixlen參數以制定不同的輸出類型網路。輸出類型為字串'''print ((IP('192.168.1.0/24')).strNormal(0)) #wanltprefixlen=0,無返回,例如192.168.1.0print ((IP('192.168.1.0/24')).strNormal(1)) #wanltprefixlen=1,prefix格式,如192.168.1.0/24print ((IP('192.168.1.0/24')).strNormal(2)) #wanltprefixlen=2,decimalnetmask格式,如192.168.1.0/255.255.255.0;print ((IP('192.168.1.0/24')).strNormal(3)) #wanltprefixlen=3,lastIP格式,如192.168.1.0-192.168.1.255'''多網路計算方法'''IP('10.0.0.0/24') < IP('12.0.0.0/24')#判斷10.0.0.0/24網段小於12.0.0.0/24,輸出結果為TRUEIP('192.168.1.100') in IP('192.168.1.0/24')#判斷192.168.1.100地址是否在192.168.1.0/24網段中,輸出結果為TRUE'''判斷兩個網段是否存在存在重疊,採用IPy提供的overlaps方法'''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,代表為不存在重疊

根據輸入的IP或者子網返回網路、掩碼、廣播、反向解析,子網數等資訊輸出結果

#! /env python3#coding=utf-8'''根據輸入的IP或者子網返回網路,掩碼,廣播,方向解析,子網數,ip類型等資訊'''from IPy import IPip_s = input('Please input an IP or net-range: ')   #接收使用者輸入,參數為IP地址或者網路地址ips = IP(ip_s)if len(ips) > 1:      #為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:    #為單個IP地址    print('reverse address: %s' % ips.reverseNames()[0])  #輸出IP反向解析print('hexadecimal: %s' % ips.strHex())    #16進位地址格式print('binary ip: %s' % ips.strBin())    #二進位地址格式print('iptype: %s' % ips.iptype())            #輸出地址類型,為公網還是私網


python學習-IPy模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.