This article mainly introduces how to use Python to calculate network segments based on IP addresses and subnet masks. It involves Python character string operation techniques based on the Linux platform and has some reference value, for more information about how to use Python to calculate network segments based on IP addresses and subnet masks, see the following example. Share it with you for your reference. The details are as follows:
This code is tested in Linux 2.6.6python!
#! /Usr/bin/env python # _ * _ encoding: UTF-8 _ * _ # Input your ip address and netmask to figure out your network. # declaration: This script is interactive. By default, run python network. pyfrom IPy import IPinput_IP = raw_input ('enter IP Address: ') list1 = input_IP.split ('. ') if len (list1 )! = 4: print "the IP address you entered is illegal. Please enter it again! "Exit () for I in list1: if I. isdigit () = True and int (I)> = 0 and int (I) <= 255: pass else: print "the IP address you entered is invalid. Please enter it again! "Exit () input_Netmask = raw_input ('Enter the subnet mask: ') list2 = input_Netmask.split ('. ') if len (list2 )! = 4: print "The subnet mask you entered is illegal. Please enter it again! "Exit () for I in list2: if I. isdigit () = True and int (I)> = 0 and int (I) <= 255: pass else: print "The subnet mask you entered is invalid. Please enter it again! "Exit () print" your network segment is: % s "% (IP (input_IP). make_net (input_Netmask ))
I hope this article will help you with Python programming.