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

yipeiwu_com6年前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微信跳一跳系列之棋子定位像素遍历

前言 在前几篇博客中,分别就棋子的颜色识别、模板匹配等定位方式进行了介绍和实践,这一篇博客就来验证一下github中最热门的跳一跳外挂中采用的像素遍历的方法。 方法说明 像素遍历的实质依...

对python中执行DOS命令的3种方法总结

1. 使用os.system("cmd") 特点是执行的时候程序会打出cmd在Linux上执行的信息。 import os os.system("ls") 2. 使用Popen...

pandas 条件搜索返回列表的方法

pandas中常用的一件事情就是对特定条件进行搜索,那么这里介绍使用pandas搜索方式,本案例使用的pandas是anaconda中的,可以下载任意的anaconda进行安装都会带有。...

Python3单行定义多个变量或赋值方法

Python3单行定义多个变量或赋值方法

你甚至可以在一行内将多个值赋值给多个变量 >>> a , b = 45, 54 >>> a 45 >>> b 54 这个技巧用...

django celery redis使用具体实践

django celery redis使用具体实践

环境准备 python3.5.4 windows redis pip install celery pip install redis windows下启动redir...