婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識(shí)庫 > python 實(shí)現(xiàn)IP子網(wǎng)計(jì)算

python 實(shí)現(xiàn)IP子網(wǎng)計(jì)算

熱門標(biāo)簽:深圳網(wǎng)絡(luò)外呼系統(tǒng)代理商 騰訊地圖標(biāo)注有什么版本 申請(qǐng)辦個(gè)400電話號(hào)碼 柳州正規(guī)電銷機(jī)器人收費(fèi) 千呼ai電話機(jī)器人免費(fèi) 400電話辦理費(fèi)用收費(fèi) 高德地圖標(biāo)注字母 鎮(zhèn)江人工外呼系統(tǒng)供應(yīng)商 外呼系統(tǒng)前面有錄音播放嗎

0. 前言

IP地址目前存在兩個(gè)版本:IPv4和IPv6,平常我們見到最多的就是IPv4了,如192.168.1.1/24,當(dāng)然,IPv4地址池資源緊缺,IPv6已悄然大量部署了。

我們?cè)谠O(shè)計(jì)網(wǎng)絡(luò)架構(gòu)時(shí)必須要對(duì)設(shè)備互聯(lián)地址、環(huán)回地址、業(yè)務(wù)地址進(jìn)行規(guī)劃,那怎么規(guī)劃?給你一個(gè)A類地址你怎么辦?最重要是不是得計(jì)算?口算怕不準(zhǔn)確吧?心算行不行,就不怕你沒這本事,哈哈!

下面請(qǐng)用python幫你搞定這一切吧!

1. ipaddress模塊介紹

1.1 IP主機(jī)地址

說明:不帶掩碼

怎么判斷是ipv4地址,還是ipv6地址呢?使用ipaddress.ip_address() 函數(shù)可以來知曉:

>>> ipaddress.ip_address('192.168.1.1')
IPv4Address('192.168.1.1')
>>> ipaddress.ip_address('192.168.1.1').version
4

>>> ipaddress.ip_address('fe80::1')
IPv6Address('fe80::1')
>>> ipaddress.ip_address('fe80::1').version
6

如果帶上掩碼就會(huì)報(bào)錯(cuò):

>>> ipaddress.ip_address('192.168.1.1/32')
Traceback (most recent call last):
 File "stdin>", line 1, in module>
 File "/usr/lib/python3.5/ipaddress.py", line 54, in ip_address
 address)
ValueError: '192.168.1.1/32' does not appear to be an IPv4 or IPv6 address

1.2 定義網(wǎng)絡(luò)

說明:表示網(wǎng)段

一個(gè)IP地址,通常由網(wǎng)絡(luò)號(hào)+網(wǎng)絡(luò)前綴組成,如192.168.1.0/24,可以通過ipaddress.ip_network函數(shù)來表示,缺省情況下,python只能識(shí)別網(wǎng)絡(luò)號(hào),如果是IP主機(jī)就會(huì)報(bào)錯(cuò),當(dāng)然你可以通過strict=False來避免。

>>> ipaddress.ip_network('192.168.1.0/24')
IPv4Network('192.168.1.0/24')

#缺省,輸入主機(jī)位就會(huì)報(bào)錯(cuò)
>>> ipaddress.ip_network('192.168.1.1/24')
Traceback (most recent call last):
 File "stdin>", line 1, in module>
 File "/usr/lib/python3.5/ipaddress.py", line 74, in ip_network
 return IPv4Network(address, strict)
 File "/usr/lib/python3.5/ipaddress.py", line 1536, in __init__
 raise ValueError('%s has host bits set' % self)
ValueError: 192.168.1.1/24 has host bits set #提示是主機(jī)IP

#修改位非嚴(yán)格模式,缺省為strict=True
>>> ipaddress.ip_network('192.168.1.1/24' , strict=False)
IPv4Network('192.168.1.0/24') #返回網(wǎng)絡(luò)號(hào)

1.3 主機(jī)接口

說明:表示接口地址(ip/掩碼)
一般在路由器、交換機(jī)、防火墻接口上配置IP地址,格式如192.168.1.1/24,如果使用以上ipaddress.ip_address()和ipaddress.ip_network函數(shù)的話,就不太好表示,那么可以通過ipaddress.ip_interface()函數(shù)類表示。

>>> ipaddress.ip_interface('192.168.1.1/24')
IPv4Interface('192.168.1.1/24')

1.4 檢查address/network/interface對(duì)象

1.4.1 檢查IP版本(v4或者v6):

>>> ipaddress.ip_address('192.168.1.1').version
4
>>> ipaddress.ip_address('fe80::1').version
6

1.4.2 從接口IP獲取網(wǎng)段

>>> ipaddress.ip_interface('192.168.1.1/24').network
IPv4Network('192.168.1.0/24')

>>> ipaddress.ip_interface('fe80::/64').network
IPv6Network('fe80::/64')

1.4.3 計(jì)算網(wǎng)段有多少個(gè)IP地址

>>> ipaddress.ip_network('192.168.1.0/24').num_addresses
256

>>> ipaddress.ip_network('fe80::/64').num_addresses
18446744073709551616

1.4.4 計(jì)算網(wǎng)段有多少個(gè)可用IP地址

>>> net = ipaddress.ip_network('192.168.1.0/24')
>>> for x in net.hosts():
...  print(x)
... 
192.168.1.1
192.168.1.2
 ...
192.168.1.100
192.168.1.101
 ...
192.168.1.254

>>> [x for x in net.hosts()][0]  #獲取第一個(gè)可用IP
IPv4Address('192.168.1.1')
>>> [x for x in net.hosts()][-1] #獲取最后一個(gè)可用IP
IPv4Address('192.168.1.254')

1.4.5 獲取掩碼與反掩碼

>>> ipaddress.ip_network('192.168.1.1/24' , strict=False).netmask
IPv4Address('255.255.255.0') #獲取掩碼

>>> ipaddress.ip_network('192.168.1.1/24' , strict=False).hostmask
IPv4Address('0.0.0.255') #獲取反掩碼

1.6 獲取網(wǎng)絡(luò)號(hào)與廣播地址

>>> ipaddress.ip_network('192.168.1.1/24' , strict=False).network_address
IPv4Address('192.168.1.0')  #獲取網(wǎng)絡(luò)號(hào)

>>> ipaddress.ip_network('192.168.1.1/24' , strict=False).broadcast_address
IPv4Address('192.168.1.255') #獲取廣播地址

1.7 異常處理

如果遇到IP地址格式不符合要求等這些情況,那怎么處理呢?

#錯(cuò)誤顯示,報(bào)"ValueError"
>>> ipaddress.ip_network('192.168.1.1/24')
Traceback (most recent call last):
 File "stdin>", line 1, in module>
 File "/usr/lib/python3.5/ipaddress.py", line 74, in ip_network
 return IPv4Network(address, strict)
 File "/usr/lib/python3.5/ipaddress.py", line 1536, in __init__
 raise ValueError('%s has host bits set' % self)
ValueError: 192.168.1.1/24 has host bits set

#通過try-except語句來處理異常情況
>>> import ipaddress
>>> def cal_ip(net):
...  try:
...   net = ipaddress.ip_network(net)
...   print(net)
...  except ValueError:
...   print('您輸入格式有誤,請(qǐng)檢查!')
... 
>>> cal_ip(net = '192.168.1.1/24')
您輸入格式有誤,請(qǐng)檢查!

2. 計(jì)算IP子網(wǎng)代碼演示

2.1 完整代碼

#!/usr/bin/env python3
#-*- coding:UTF-8 -*-
#歡迎關(guān)注微信公眾號(hào):點(diǎn)滴技術(shù)

import ipaddress

def cal_ip(ip_net):
 try:
  net = ipaddress.ip_network(ip_net, strict=False)
  print('IP版本號(hào): ' + str(net.version))
  print('是否是私有地址: ' + str(net.is_private))
  print('IP地址總數(shù): ' + str(net.num_addresses))
  print('可用IP地址總數(shù): ' + str(len([x for x in net.hosts()])))
  print('網(wǎng)絡(luò)號(hào): ' + str(net.network_address))
  print('起始可用IP地址: ' + str([x for x in net.hosts()][0]))
  print('最后可用IP地址: ' + str([x for x in net.hosts()][-1]))
  print('可用IP地址范圍: ' + str([x for x in net.hosts()][0]) + ' ~ ' + str([x for x in net.hosts()][-1]))
  print('掩碼地址: ' + str(net.netmask))
  print('反掩碼地址: ' + str(net.hostmask))
  print('廣播地址: ' + str(net.broadcast_address))
 except ValueError:
  print('您輸入格式有誤,請(qǐng)檢查!')

if __name__ == '__main__':
 ip_net = '192.168.1.1/24'
 cal_ip(ip_net)

2.2 運(yùn)行結(jié)果

IP版本號(hào): 4
是否是私有地址: True
IP地址總數(shù): 256
可用IP地址總數(shù): 254
網(wǎng)絡(luò)號(hào): 192.168.1.0
起始可用IP地址: 192.168.1.1
最后可用IP地址: 192.168.1.254
可用IP地址范圍: 192.168.1.1 ~ 192.168.1.254
掩碼地址: 255.255.255.0
反掩碼地址: 0.0.0.255
廣播地址: 192.168.1.255

3. 碎碎語

怎么樣,學(xué)完之后是不是很亢奮,不需要借助其他工具進(jìn)行計(jì)算了吧,用python就幫你搞定了。

3.1 官方參考文檔

https://docs.python.org/3.8/howto/ipaddress.html

以上就是python 實(shí)現(xiàn)IP子網(wǎng)計(jì)算的詳細(xì)內(nèi)容,更多關(guān)于python IP子網(wǎng)計(jì)算的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:
  • Python實(shí)現(xiàn)的根據(jù)IP地址計(jì)算子網(wǎng)掩碼位數(shù)功能示例
  • Python實(shí)現(xiàn)根據(jù)IP地址和子網(wǎng)掩碼算出網(wǎng)段的方法

標(biāo)簽:合肥 大慶 平頂山 烏蘭察布 海南 烏蘭察布 哈爾濱 郴州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python 實(shí)現(xiàn)IP子網(wǎng)計(jì)算》,本文關(guān)鍵詞  python,實(shí)現(xiàn),子網(wǎng),計(jì)算,python,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python 實(shí)現(xiàn)IP子網(wǎng)計(jì)算》相關(guān)的同類信息!
  • 本頁收集關(guān)于python 實(shí)現(xiàn)IP子網(wǎng)計(jì)算的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 晋城| 芷江| 普兰店市| 闽清县| 威海市| 沙湾县| 望江县| 黄陵县| 故城县| 朔州市| 彰化县| 宣威市| 冷水江市| 方城县| 土默特右旗| 鄂托克前旗| 西昌市| 克什克腾旗| 霍州市| 社会| 丰镇市| 龙南县| 瓦房店市| 西乌珠穆沁旗| 盐山县| 德保县| 甘南县| 讷河市| 东辽县| 华亭县| 青岛市| 江口县| 连州市| 宜兰县| 从江县| 仁怀市| 金塔县| 夹江县| 梨树县| 漳平市| 普洱|