How to calculate the CIDR Block Based on the IP address and subnet mask in Python

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

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.