Python生成协议脚本工具

最近项目里需要一个生成协议脚本的工具,因为有空闲时间就写了一个,协议文件是html格式,我们用requests库请求然后解析,然后生成cs代码,以下是代码:

# -*- coding: utf8 -*-
import wx
import os
import requests
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()

h2Start = "<h2>"
h2End = "</h2>"
C2SS4 = "C2SS4"
SS2C4 = "SS2C4"
C2BS4 = "C2BS4"
BS2C4 = "BS2C4"
extends = "</font> extends"
h3pre = "</h3><pre>"
NoParams = "无参数"
pre = "</pre>"

class ValueAttribute:
    def __init__(self):
        self.name = ''
        self.structType = ''
        self.normalType = ''
        self.annotations = ''
        self.stringLen = 0
        self.sturctName = ''

handlerScriptName = "Test"
config_name = 'NetHandlerConfig.txt'
bufferConfigPath = os.getcwd() + "/" + config_name
class App(wx.App):
    def OnInit(self):
        frame = wx.Frame(parent=None, title='协议代码生成工具1.0', pos=(1000, 200), size=(650, 130))
        scriptNameTitle = wx.StaticText(frame, label="脚本名称", pos=(5, 10), size=(200, 30))
        scriptNameInput = wx.TextCtrl(frame, pos=(110, 8), size=(400, 24))
        netHandlerNumbersTitle = wx.StaticText(frame, label="协议号(逗号分割)", pos=(5, 35), size=(150, 30))
        netHandlerNumbersInput = wx.TextCtrl(frame, pos=(110, 33), size=(400, 24))
        assetsPathTitle = wx.StaticText(frame, label="Assets文件目录", pos=(5, 60), size=(150, 30))
        assetsInput = wx.TextCtrl(frame, pos=(110, 58), size=(400, 24))
        file = open(bufferConfigPath, encoding='utf-8')
        fileLineArr = file.readlines()
        for line in fileLineArr:
            # print(line)
            strArr = line.replace('\n', '').split(':')
            if strArr[0] == "ScriptName":
                scriptNameInput.SetValue(strArr[1])
            elif strArr[0] == "NetHandlerNumbers":
                netHandlerNumbersInput.SetValue(strArr[1])
            elif strArr[0] == "AssetsPath":
                assetsInput.SetValue(strArr[1] + ":" + strArr[2])

        checkButton = wx.Button(frame, label="生成代码", pos=(515, 35), size=(100, 24))
        def onClickCheckButton(event):
            handlerScriptName = scriptNameInput.GetValue()
            netHandlerNumbersStr  = netHandlerNumbersInput.GetValue()
            assetsPath = assetsInput.GetValue().replace('\\', '/')
            netHandlerNumbersArr = netHandlerNumbersStr.split(',')
            netHandlerNumberList = []
            for numberStr in netHandlerNumbersArr:
                netHandlerNumberList.append(int(numberStr))

            writeStr = ""
            writeCount = 0
            for netHandlerNumber in netHandlerNumberList:
                url = "http://dungeon.admin.indra.vip:90/{0}.html".format(netHandlerNumber)

                # print(url)

                response = requests.request("GET", url, stream=True, data=None, headers=None)
                response.encoding = 'utf-8'

                # print(response.text.replace('/h3>', '/h3>\n\n\t').replace('/>', '/>\n\t').replace('br>', 'br>\n')
                #       .replace('BR>', 'BR>\n\t\t').replace('<pre>', '<pre>\n\t\t').replace('</pre>', '</pre>\n\n')
                #       .replace('\t</pre>', '</pre>'))

                titleIndex = 0
                title = ""
                cssIndex = 0
                cbsIndex = 0
                reqIndex = 0
                sscIndex = 0
                bscIndex = 0
                repIndex = 0
                reqStr = "Req"
                reqType = "NormalMessage"
                reqValueStr = ""
                reqValueList = []
                repStr = "Rep"
                repValueStr = ""
                repValueList = []
                subStructStr = ""
                subStructList = []
                subStructDict = {}
                isOne = False
                isTwo = False
                isThree = False
                isFour = False

                for c in response.text:
                    # 解析标题
                    if titleIndex < len(h2Start):
                        if c == h2Start[titleIndex]:
                            titleIndex += 1
                    if titleIndex >= len(h2Start) and titleIndex - len(h2Start) < len(h2End):
                        if c == h2End[titleIndex - len(h2Start)]:
                            titleIndex += 1
                    if titleIndex == len(h2Start):
                        title += c
                    # 解析C2SS
                    if cssIndex >= len(C2SS4) and cssIndex - len(C2SS4) < len(extends):
                        if c == extends[cssIndex - len(C2SS4)]:
                            cssIndex += 1
                    if cssIndex == len(C2SS4) and not isTwo:
                        reqType = "NormalMessage"
                        reqStr += c
                        isOne = True
                    if cssIndex < len(C2SS4):
                        if c == C2SS4[cssIndex]:
                            cssIndex += 1
                        else:
                            cssIndex = 0
                        # print(cssIndex, c, bscIndex)
                    # 解析C2BS
                    if cbsIndex >= len(C2BS4) and cbsIndex - len(C2BS4) < len(extends):
                        if c == extends[cbsIndex - len(C2BS4)]:
                            cbsIndex += 1
                    if cbsIndex == len(C2BS4) and not isOne:
                        reqType = "PvpMessage"
                        reqStr += c
                        isTwo = True
                    if cbsIndex < len(C2BS4):
                        if c == C2BS4[cbsIndex]:
                            cbsIndex += 1
                        else:
                            cbsIndex = 0
                    # 解析请求字段
                    if reqIndex >= len(
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值