免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: chinaunix874
打印 上一主题 下一主题

贡献一个小软件:子网计算器 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2010-06-18 15:42 |只看该作者
本帖最后由 chinaunix874 于 2010-06-18 15:43 编辑

按照luffy.deng的提醒,修改了一下代码,主要是修改了计算广播地址的函数,现在看上去简单多了。多谢多谢。

  1. #!/usr/bin/python
  2. #FileName: e-wxsubnet.py
  3. import re
  4. import wx
  5. import ctypes

  6. def ipnum_dec2bin(ipnum):
  7.     result = ''
  8.     num = int(ipnum)
  9.     while num > 0:
  10.         mod = num % 2
  11.         num /= 2
  12.         result = str(mod) + result
  13.     if len(result) != 8:
  14.         result = '0'*(8-len(result)) + result
  15.     return result

  16. def check_ip(ip):
  17.     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)

  18. def check_mask(mask):
  19.     if mask == '0.0.0.0':
  20.         return False
  21.     if check_ip(mask):
  22.         mask = mask.split(".")
  23.         masknum = [int(num) for num in mask]
  24.         maskbin = ''
  25.         for num in masknum:
  26.             maskbin += ipnum_dec2bin(num)
  27.         if '01' in maskbin:
  28.             return False
  29.         else:
  30.             return True

  31. def get_subnet(ip, mask):
  32.     ip = ip.split('.')
  33.     mask = mask.split('.')
  34.     return [str(int(i) & int(m)) for i, m in map(None, ip, mask)]
  35. def get_broadcast(ip, mask):
  36.     ip = ip.split('.')
  37.     mask = mask.split('.')
  38.     mask = [ctypes.c_ubyte(~int(i)).value for i in mask]
  39.     return [str(int(i) | int(m)) for i, m in map(None, ip, mask)]

  40. class MyFrame(wx.Frame):
  41.     def __init__(self):
  42.         wx.Frame.__init__(self, None, -1, 'Subnet Calculator', size=(500, 200))
  43.         panel = wx.Panel(self, -1)
  44.         wx.StaticText(panel, -1, 'IP:', pos=(10, 12))
  45.         self.ipCtrl = wx.TextCtrl(panel, -1, '', pos=(70, 10), size=(300, -1))
  46.         wx.StaticText(panel, -1, 'Netmask:', pos=(10, 42))
  47.         self.maskCtrl = wx.TextCtrl(panel, -1, '', pos=(70, 40), size=(300, -1))
  48.         wx.StaticText(panel, -1, 'Result:', pos=(10, 72))
  49.         self.resCtrl = wx.TextCtrl(panel, -1, '', pos=(70, 70), size=(400, -1))
  50.         button = wx.Button(panel, label='Calculator', pos=(70, 100))
  51.         self.Bind(wx.EVT_BUTTON, self.OnClick, button)
  52.     def OnClick(self, event):
  53.         ipaddr = self.ipCtrl.GetValue()
  54.         mask = self.maskCtrl.GetValue()
  55.         if not check_ip(ipaddr):
  56.             self.resCtrl.SetValue("Please input correct IP address!")
  57.         elif not check_mask(mask):
  58.             self.resCtrl.SetValue("Please input correct Netmask")
  59.         else:
  60.             subnet = '.'.join(get_subnet(ipaddr, mask))
  61.             broadcast = '.'.join(get_broadcast(ipaddr,mask))
  62.             self.resCtrl.SetValue("Subnet is:%s    Broadcast is:%s" % (subnet, broadcast))

  63. if __name__ == '__main__':
  64.     app = wx.PySimpleApp()
  65.     frame = MyFrame()
  66.     frame.Show(True)
  67.     app.MainLoop()
复制代码

论坛徽章:
0
12 [报告]
发表于 2010-06-19 03:51 |只看该作者
本帖最后由 Kabie 于 2010-06-19 04:12 编辑

ipnum_dec2bin 可以改成
  1. '%08d' % int(bin(ipnum)[2:])
复制代码
或者
  1. '{0:08b}'.format(ipnum)
复制代码
check_mask也可以写得更简练一些……

比如……
  1. def check_mask(mask):
  2.         mask = map(int,mask.split('.'))
  3.         tmp = reduce(lambda x,y:(x<<8)+y,mask)
  4.         if tmp==0 or '01' in '{0:032b}'.format(tmp):
  5.                 return False
  6.         return True
复制代码

论坛徽章:
0
13 [报告]
发表于 2010-06-19 07:55 |只看该作者
本帖最后由 luffy.deng 于 2010-06-19 07:57 编辑

回复 12# Kabie
bin   format  是2.6才有的,很多人用的是2.5。转换为二进制大体的写法是这样。
  1. def dec2bin(num):
  2.     result = ''
  3.     num = int(num)
  4.     while num > 0:
  5.         if num &1:
  6.             result ='1'+result
  7.         else:
  8.             result ='0'+result
  9.         num>>=1
  10.     return result.rjust(8,'0')
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP