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 pass详细介绍及实例代码

Python pass的用法: 空语句 do nothing 保证格式完整 保证语义完整 以if语句为例,在c或c++/Java中: if(true) ; //do...

使用TensorFlow实现二分类的方法示例

使用TensorFlow实现二分类的方法示例

使用TensorFlow构建一个神经网络来实现二分类,主要包括输入数据格式、隐藏层数的定义、损失函数的选择、优化函数的选择、输出层。下面通过numpy来随机生成一组数据,通过定义一种正负...

pygame游戏之旅 调用按钮实现游戏开始功能

pygame游戏之旅 调用按钮实现游戏开始功能

本文为大家分享了pygame游戏之旅的第12篇,供大家参考,具体内容如下 实现点击功能: click = pygame.mouse.get_pressed() print(click...

Python并行分布式框架Celery详解

Python并行分布式框架Celery详解

Celery 简介 除了redis,还可以使用另外一个神器---Celery。Celery是一个异步任务的调度工具。 Celery 是 Distributed Task Queue,分...

python base64库给用户名或密码加密的流程

给明文密码加密的流程: import base64 pwd_after_encrypt = base64.b64encode(b'this is a scret!') pwd_bef...