- 论坛徽章:
- 0
|
- #!/usr/bin/python
- #-*- coding: utf-8 -*-
- #FileName: subnet-calculator.py
- '''这个小程序的功能是给定ip地址及子网掩码,
- 计算出相关的网络号以及广播地址'''
- import re
- import wx
- class MyFrame(wx.Frame):
- def __init__(self):
- wx.Frame.__init__(self, None, -1, u'子网掩码计算器', size=(500, 200))
- panel = wx.Panel(self, -1)
- wx.StaticText(panel, -1, u'IP地址:', pos=(10, 12))
- self.ipCtrl = wx.TextCtrl(panel, -1, '', pos=(70, 10), size=(300, -1))
- wx.StaticText(panel, -1, u'子网掩码:', pos=(10, 42))
- self.maskCtrl = wx.TextCtrl(panel, -1, '', pos=(70, 40), size=(300, -1))
- wx.StaticText(panel, -1, u'计算结果:', pos=(10, 72))
- self.resCtrl = wx.TextCtrl(panel, -1, '', pos=(70, 70), size=(400, -1))
- button = wx.Button(panel, label=u'计算', pos=(70, 100))
- self.Bind(wx.EVT_BUTTON, self.OnClick, button)
- def emu_bit_or(self, bit1, bit2):
- #'''这个小函数用于模拟或运算'''
- if bit1 == '0' and bit2 == '0':
- return '0'
- else:
- return '1'
- def list_or(self, list1, list2):
- #'''这个小程序里,list都是8位的,模拟按位或运算'''
- result = []
- for i in range(0,8):
- result.append(self.emu_bit_or(list1[i], list2[i]))
- return result
- def bin2dec(self, num):
- return str(int(num, 2))
- def ipnum_dec2bin(self, ipnum):
- #'''将ip地址转换成8位2进制数,不足8位补齐'''
- result = ''
- num = int(ipnum)
- while num > 0:
- mod = num % 2
- num /= 2
- result = str(mod) + result
- if len(result) != 8:
- result = '0'*(8-len(result)) + result
- return result
- def str2list(self, mystr):
- return [i for i in mystr]
- def list2str(self, mylist):
- result = ''
- for i in mylist:
- result += i
- return result
- def emu_invert(self, mybit):
- #'''模拟取反操作'''
- if mybit == '1':
- return '0'
- else:
- return '1'
- def emu_list_invert(self, mylist):
- return [self.emu_invert(i) for i in mylist]
- def str_or_str(self, ipnum1, ipnum2):
- #'''模拟ip地址的按位或操作,每次操作ip地址中的一个数字返回值为一个8位二进制字符串'''
- str1 = self.ipnum_dec2bin(ipnum1)
- str2 = self.emu_list_invert(self.ipnum_dec2bin(ipnum2))
- str1 = self.str2list(str1)
- str2 = self.str2list(str2)
- return self.list2str(self.list_or(str1, str2))
- def check_ip(self, ip):
- return re.match('(([01]?\d\d?|2[0-4]\d|25[0-5])\.){3}([01]?\d\d?|2[0-4]\d|25[0-5])$',ip)
- def check_mask(self, mask):
- if mask == '0.0.0.0':
- return False
- if self.check_ip(mask):
- mask = mask.split(".")
- masknum = [int(num) for num in mask]
- maskbin = ''
- for num in masknum:
- maskbin += self.ipnum_dec2bin(num)
- if '01' in maskbin:
- return False
- else:
- return True
- def OnClick(self, event):
- ipaddr = self.ipCtrl.GetValue()
- mask = self.maskCtrl.GetValue()
- if not self.check_ip(ipaddr):
- self.resCtrl.SetValue(u"请输入正确的ip地址")
- elif not self.check_mask(mask):
- self.resCtrl.SetValue(u"请输入正确的子网掩码")
- else:
- ipaddr = ipaddr.split(".")
- mask = mask.split(".")
- ipnum = [int(num) for num in ipaddr]
- masknum = [int(num) for num in mask]
- subnet = []
- broadcast = []
- for i in range(0, 4):
- subnet.append(ipnum[i] & masknum[i])
- broadcast.append(self.str_or_str(ipnum[i], masknum[i]))
- subnet = [str(num) for num in subnet]
- subnet = ".".join(subnet)
- broadcast = [self.bin2dec(num) for num in broadcast]
- broadcast = ".".join(broadcast)
- self.resCtrl.SetValue(u"网络号是:%s 广播地址是:%s" % (subnet, broadcast))
- # result = self.posCtrl.GetValue()
- # self.resCtrl.SetValue('%s' % result)
- if __name__ == '__main__':
- app = wx.PySimpleApp()
- frame = MyFrame()
- frame.Show(True)
- app.MainLoop()
复制代码
|
|