本文實例講述了Python實現根據IP地址和子網掩碼算出網段的方法。分享給大家供大家參考。具體如下:
該代碼在Linux環境2.6.6python版本測試通過!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
#!/usr/bin/env python #_*_encoding:utf-8_*_ #Input your ip address and netmask to figure out your network . #申明:此腳本為交互式,默認情況下請執行python network.py from IPy import IP input_IP = raw_input ( '請輸入ip地址:' ) list1 = input_IP.split( '.' ) if len (list1) ! = 4 : print "您輸入的ip地址不合法,請重新輸入!" exit() for i in list1: if i.isdigit() = = True and int (i) > = 0 and int (i) < = 255 : pass else : print "您輸入的ip地址不合法,請重新輸入!" exit() input_Netmask = raw_input ( '請輸入子網掩碼:' ) list2 = input_Netmask.split( '.' ) if len (list2) ! = 4 : print "您輸入的子網掩碼不合法,請重新輸入!" exit() for i in list2: if i.isdigit() = = True and int (i) > = 0 and int (i) < = 255 : pass else : print "您輸入的子網掩碼不合法,請重新輸入!" exit() print "您所在的網段為:%s" % (IP(input_IP).make_net(input_Netmask)) |
希望本文所述對大家的Python程序設計有所幫助。