用python实现IEEE 802.3以太网帧封,python802.3,在Debian的环境下,


在Debian的环境下,用python简单的实现IEEE 802.3以太网帧封,界面用wxpython

由于 python 标准库里没有 wxpython, 所以写界面前需要安装 wxpython, 基于 debian 的 Linux 系统: apt-getinstall python-wxgtk2.8 python-wxtools wx2.8-i18n

需要 5 个静态文本框、 5 个可编辑文本框和两个按钮。对两个按钮添加时间处理程序。点击“执行”按钮可计算 FCS和输出封装好的帧,点击“清空”可清空界面输入和输出的内容。

#!/usr/bin/env python# -*- coding:utf8 -*-import wximport reimport sys#将MAC地址转化为二进制字符串形式def replace(mac):    m = ''.join(re.split('[:-]',mac)) #正则表达式去掉多余字符    mac_str = ''    for i in range(len(m)):        temp = bin(int(m[i],16))[2:]        if len(temp)<4:            temp = (4-len(temp))*'0' + temp          mac_str += temp    return mac_str#将数据部分变成二进制字符串形式def str2bin(data_be):    data = ''    for i in range(len(data_be)):        temp = bin(ord(data_be[i]))[2:]        if len(temp)<8:            temp = (8-len(temp))*'0' + temp              data += temp    return data#长度变为二进制字符串形式def len2bin(data):    temp = bin(len(data))[2:]    if len(data)>1500:        print sys.stderr,"数据部分超过1500个字节"        return False    if len(temp)<16:        temp = (16-len(temp)) * '0' + temp    return temp#FCS算法def fcs(mac1,mac2,data):    a = replace(mac1) + replace(mac2) + len2bin(data) + str2bin(data) + 32*'0'    temp = int(a[:9],2)    i = 9    while i<=len(a):        if temp<256:num=0        else:num=263        temp = int(bin(temp^num)[2:]+a[i:i+1],2)        i += 1    return bin(temp)[2:]#封装成帧def calculation(mac1,mac2,data):    before = '10'*31 + '11'    data_after = ''    if len(data)<46:        data_after = data + (46 - len(data))*'0'    else:data_after=data    Fcs = fcs(mac1,mac2,data)    Fcs = (32 - len(Fcs))*'0'+Fcs    strbin = before + replace(mac2) + replace(mac1) + len2bin(data) + str2bin(data_after) + Fcs    strlist = re.findall('....',strbin)    strhex = ''    for i in range(len(strlist)):        strhex += hex(int(strlist[i],2))[2:]    return strhex#图形界面的设计,继承wx.Frame类class MyFrame(wx.Frame):    def __init__(self):        wx.Frame.__init__(self,None,-1,'     IEEE以太网封装',size=(600,500))        self.panel = wx.Panel(self,-1)        self.mac_from = wx.StaticText(self.panel,-1,"     源MAC地址:")        self.mac_text = wx.TextCtrl(self.panel,-1,size=(400,-1))        self.mac_text.SetInsertionPoint(0)        self.mac_text.SetValue('20:7c:8f:1f:3e:e1')        self.mac_to = wx.StaticText(self.panel,-1,"     目的MAC地址:")        self.mac_to_text = wx.TextCtrl(self.panel,-1,size=(400,-1))        self.mac_to_text.SetInsertionPoint(0)        self.data = wx.StaticText(self.panel,-1,"     数据部分:")        self.data_text =wx.TextCtrl(self.panel,-1,size=(400,100),style = wx.TE_MULTILINE)        self.data_text.SetInsertionPoint(0)        self.crc = wx.StaticText(self.panel,-1,"     CRC检测结果:")        self.crc_text = wx.TextCtrl(self.panel,-1,size=(400,-1))        self.crc_text.SetInsertionPoint(0)        self.end = wx.StaticText(self.panel,-1,"    封装结果:")        self.end_text = wx.TextCtrl(self.panel,-1,style = wx.TE_MULTILINE,size=(400,200))        self.end_text.SetInsertionPoint(0)        #按钮事件        exButton = wx.Button(self.panel,label='执行',pos=(100,450),size=(80,25))        exButton.Bind(wx.EVT_BUTTON,self.ex)        clearButton = wx.Button(self.panel,label='清空',pos=(400,450),size=(80,25))        clearButton.Bind(wx.EVT_BUTTON,self.cle)        sizer = wx.FlexGridSizer(cols=2,hgap=6,vgap=6)        sizer.AddMany([self.mac_from,self.mac_text,self.mac_to,self.mac_to_text,self.data,self.data_text,self.crc,self.crc_text,self.end,self.end_text,exButton,clearButton])        self.panel.SetSizer(sizer)    #执行事件的函数        def ex(self,event):        mac1 = self.mac_text.GetValue()        mac2 = self.mac_to_text.GetValue()        if mac1=='' or mac2=='':            print sys.stderr,'输入的MAC地址不能为空' #标准错误输出            return        da = self.data_text.GetValue()        fcs_crc = fcs(mac1,mac2,da)        self.crc_text.SetValue(fcs_crc)        end = calculation(mac1,mac2,da)        self.end_text.SetValue(end)        event.Skip()    #清空事件函数    def cle(self,event):        self.mac_text.SetValue('20:7c:8f:1f:3e:e1')        self.mac_to_text.Clear()        self.data_text.Clear()        self.crc_text.Clear()        self.end_text.Clear()        event.Skip()class MyApp(wx.App):    def OnInit(self):        frame = MyFrame()        frame.Show()        return Truedef main():    MyApp(True).MainLoop()if __name__ == '__main__':    main()#该片段来自于http://byrx.net

评论关闭