This example describes a Python implementation method that calculates a network segment based on an IP address and subnet mask. Share to everyone for your reference. Specific as follows:
This code is tested in the Linux environment 2.6.6python version!
#!/usr/bin/env python#_*_encoding:utf-8_*_#input your IP address and netmask to figure out your network. #申明: This script is interactive, by default Please execute Python network.pyfrom IPy import ipinput_ip = raw_input (' Please enter IP address: ') List1 = Input_ip.split ('. ') If Len (list1)! = 4: print "The IP address you entered is not valid, please re-enter!" " exit () for I in List1: if i.isdigit () = = True and int (i) >=0 and int (i) <= 255: Pass else: prin T "The IP address you entered is not valid, please re-enter!" " exit () Input_netmask = raw_input (' Please enter subnet mask: ') List2 = Input_netmask.split ('. ') If Len (list2)! = 4: print "The subnet mask you entered is not legal, please re-enter!" 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 not valid, please re-enter! " Exit () print "Your network segment is:%s" (IP (INPUT_IP). Make_net (Input_netmask))
Hopefully this article will help you with Python programming.