Python实现根据IP地址和子网掩码算出网段的方法

yipeiwu_com5年前Python基础

本文实例讲述了Python实现根据IP地址和子网掩码算出网段的方法。分享给大家供大家参考。具体如下:

该代码在Linux环境2.6.6python版本测试通过!

#!/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程序设计有所帮助。

相关文章

Python遍历目录并批量更换文件名和目录名的方法

本文实例讲述了Python遍历目录并批量更换文件名和目录名的方法。分享给大家供大家参考,具体如下: #encoding=utf-8 #author: walker #date: 20...

Python生成器(Generator)详解

通过列表生成式,我们可以直接创建一个列表。但是,受到内存限制,列表容量肯定是有限的。而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那后...

python flask解析json数据不完整的解决方法

当使用Python的flask框架来开发网站后台,解析前端Post来的数据,通常都会使用request.form来获取前端传过来的数据,但是如果传过来的数据比较复杂,其中右array,而...

python内存监控工具memory_profiler和guppy的用法详解

python2.7在内存管理上相比python3还是有些坑的,其释放后的内存仍然保留在python的内存池中,不被系统所用。python循环引用的变量不会被回收,这会导致程序越运行,占用...

浅谈Pandas 排序之后索引的问题

如下所示: In [1]: import pandas as pd ...: df=pd.DataFrame({"a":[1,2,3,4,5],"b":[5,4,3,2,1]})...