Python implements methods for calculating network segments based on IP address and subnet mask

Source: Internet
Author: User
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.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.