wxpython笔记:wxpython实现HTTP请求交互式工具

 

简介:wxpython写的HTTP请求交互式工具,现在只支持Get、Post两种方式。

主程序:

import wx
import numpy
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure
import http_resquest
import json
import login
class Example(wx.Frame):
    def __init__(self, parent, title):
        super(Example, self).__init__(parent, title=title)
        self.Init_Panel()
        self.Init_Box()
        self.Init_Left()
        self.Init_Center()
        # 将三个垂直盒子添加到垂直盒子
        self.Boxh1.Add(self.LeftPanel,proportion = 1, border = 2,flag = wx.ALL | wx.EXPAND)
        self.Boxh1.Add(self.CenterPanel, proportion=4, border=2, flag=wx.ALL | wx.EXPAND)

        #将垂直盒子和主框架关联
        self.SetSizer(self.Boxh1)
        #显示主框架
        self.Show()
        self.Http = http_resquest.Http_R()
    #创建三个面板
    def Init_Panel(self):
        self.LeftPanel = wx.Panel(self)
        self.CenterPanel = wx.Panel(self)

    #创建四个盒子
    #两个垂直盒子、两个水平盒子
    def Init_Box(self):
        #两个垂直盒子
        self.Boxv1 = wx.BoxSizer(wx.VERTICAL)
        self.Boxv2 = wx.BoxSizer(wx.VERTICAL)

        #两个水平盒子
        self.Boxh1 = wx.BoxSizer(wx.HORIZONTAL)
        self.Boxh2 = wx.BoxSizer(wx.HORIZONTAL)
        self.Boxh3 = wx.BoxSizer(wx.HORIZONTAL)
    def Init_Left(self):

        # 创建一个wx.StaticBox对象。
        # 声明一个wx.StaticBoxSizer与创建的wx.StaticBox对象作为其参数。
        nm = wx.StaticBox(self.LeftPanel, -1, 'URL')
        nmSizer = wx.StaticBoxSizer(nm, wx.VERTICAL)
        self.input = wx.TextCtrl(self.LeftPanel, -1, size=(200, 20), style=wx.ALIGN_LEFT)
        nmSizer.Add(self.input, 1, wx.EXPAND | wx.ALL, 10)
        self.Boxh3.Add(nmSizer, 5, wx.EXPAND | wx.ALL, 10)


        self.button = wx.Button(self.LeftPanel, -1, "请求")
        self.Bind(wx.EVT_BUTTON, self.OnClick, self.button)
        # self.button.SetDefault()  # 将按钮设置为默认按钮,不然会是选中状态,边框不同
        self.Boxh3.Add( self.button, 0,wx.EXPAND | wx.ALL, 10)
        self.Boxv1.Add(self.Boxh3, 0,wx.EXPAND | wx.ALL, 10)

        # 创建一个wx.StaticBox对象。
        # 声明一个wx.StaticBoxSizer与创建的wx.StaticBox对象作为其参数。
        nm1 = wx.StaticBox(self.LeftPanel, -1, '请求方法')
        nmSizer1 = wx.StaticBoxSizer(nm1, wx.VERTICAL)
        # 创建下拉框
        self.languages = ['GET', 'POST', 'HEAD','PUT','ELETE']
        self.combo1 = wx.ComboBox(self.LeftPanel, choices=self.languages, value=self.languages[0])
        # 在StaticBoxSizer盒子添加下拉框
        nmSizer1.Add(self.combo1, 1, wx.EXPAND  | wx.ALL, 1)
        self.Boxv1.Add(nmSizer1,0, wx.EXPAND | wx.ALL,10)



        # 创建一个wx.StaticBox对象
        # 声明一个wx.StaticBoxSizer与创建的wx.StaticBox对象作为其参数
        nm2 = wx.StaticBox(self.LeftPanel, -1, '请求参数')
        nmSizer2 = wx.StaticBoxSizer(nm2, wx.VERTICAL)
        # 创建文本域
        self.multiText1 = wx.TextCtrl(self.LeftPanel, -1, style=wx.TE_MULTILINE)  # 创建一个文本控件
        self.multiText1.SetInsertionPoint(0)  # 设置插入点
        nmSizer2.Add(self.multiText1, 1, wx.EXPAND | wx.ALL, 10)
        #  在垂直盒子里添加StaticBoxSizer盒子
        self.Boxv1.Add(nmSizer2, 1, wx.EXPAND | wx.ALL, 10)

        # 创建一个wx.StaticBox对象
        # 声明一个wx.StaticBoxSizer与创建的wx.StaticBox对象作为其参数
        nm3 = wx.StaticBox(self.LeftPanel, -1, '请求头')
        nmSizer3 = wx.StaticBoxSizer(nm3, wx.VERTICAL)
        # 创建文本域
        self.multiText2 = wx.TextCtrl(self.LeftPanel, -1, style=wx.TE_MULTILINE)  # 创建一个文本控件
        self.multiText2.SetInsertionPoint(0)  # 设置插入点
        nmSizer3.Add(self.multiText2, 1, wx.EXPAND | wx.ALL, 10)

        #  在垂直盒子里添加StaticBoxSizer盒子
        self.Boxv1.Add(nmSizer3, 1, wx.EXPAND | wx.ALL, 10)

        #把垂直盒子与LeftPanel关联起来
        self.LeftPanel.SetSizer(self.Boxv1)

    def OnClick(self,event):
        url = self.input.GetValue()
        if url != "":
            self.multiText3.Clear()
            self.multiText4.Clear()
            data = self.multiText1.GetValue()
            headers = self.multiText2.GetValue()
            if self.combo1.GetValue() == "GET":
                self.get_data(headers, data, url, self.Http.Get)
            elif self.combo1.GetValue() == "POST":
                self.get_data(headers, data, url, self.Http.Post)
            else:
                print("功能占位")
    def get_data(self,headers,data,url,Callback):
        if data == "" and headers == "":
            headers_data, json_data = Callback(url)
        elif data != "" and headers == "":
            data = data.split("\n")
            data_list = [i.split(":") for i in data]
            headers_data, json_data = Callback(url, data=dict(data_list))
        elif data == "" and headers != "":
            headers = headers.split("\n")
            headers_list = [i.split(":") for i in headers]
            headers_data, json_data = Callback(url, header=dict(headers_list))
        else:
            data = data.split("\n")
            data_list = [i.split(":") for i in data]
            headers = headers.split("\n")
            headers_list = [i.split(":") for i in headers]
            headers_data, json_data = Callback(url, dict(data_list), dict(headers_list))
        for key, value in headers_data.items():
            self.multiText3.write(str(key) + ":" + str(value) + "\n")
        for key, value in json_data.items():
            self.multiText4.write(str(key) + ":" + str(value) + "\n")

    def Init_Center(self):
        # 创建一个wx.StaticBox对象
        # 声明一个wx.StaticBoxSizer与创建的wx.StaticBox对象作为其参数
        nm1 = wx.StaticBox(self.CenterPanel, -1, '返回头')
        nmSizer1 = wx.StaticBoxSizer(nm1, wx.VERTICAL)
        # 创建文本域
        self.multiText3 = wx.TextCtrl(self.CenterPanel, -1, style=wx.TE_MULTILINE)  # 创建一个文本控件
        self.multiText3.SetInsertionPoint(0)  # 设置插入点
        nmSizer1.Add(self.multiText3, 1, wx.EXPAND | wx.ALL, 10)
        #  在水平盒子里添加StaticBoxSizer盒子
        self.Boxh2.Add(nmSizer1, 1, wx.EXPAND | wx.ALL, 10)

        # 创建一个wx.StaticBox对象
        # 声明一个wx.StaticBoxSizer与创建的wx.StaticBox对象作为其参数
        nm2 = wx.StaticBox(self.CenterPanel, -1, '返回内容')
        nmSizer2 = wx.StaticBoxSizer(nm2, wx.VERTICAL)
        # 创建文本域
        self.multiText4 = wx.TextCtrl(self.CenterPanel, -1, style=wx.TE_MULTILINE)  # 创建一个文本控件
        self.multiText4.SetInsertionPoint(0)  # 设置插入点
        nmSizer2.Add(self.multiText4, 1, wx.EXPAND | wx.ALL, 10)

        #  在水平盒子里添加StaticBoxSizer盒子
        self.Boxh2.Add(nmSizer2, 1, wx.EXPAND | wx.ALL, 10)

        #  在垂直盒子里添加水平盒子
        self.Boxv2.Add(self.Boxh2, 1, wx.EXPAND | wx.ALL, 10)


        #创建notebook
        nb = wx.Notebook(self.CenterPanel, style=wx.NB_FIXEDWIDTH)
        page1 = PageOne(nb)
        page2 = PageTwo(nb)
        page3 = PageThree(nb)
        #在notebook中添加面板,第一个参数是面板,第二个参数是标签
        nb.AddPage(page1, "页面内容")
        nb.AddPage(page2, "内容查找")
        nb.AddPage(page3, "日志")
        self.Boxv2.Add(nb, 1, wx.EXPAND | wx.ALL, 10)
        self.CenterPanel.SetSizer(self.Boxv2)

#本质:创建了三个面板
###########################使用说明1######################################
class PageOne(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        colour = [(160, 255, 204), (153, 204, 255), (151, 253, 225), ]
        self.SetBackgroundColour(colour[0])
        self.tx1 = wx.StaticText(self, -1, "页面内容", (355, 45),
                                    (100, -1), wx.ALIGN_CENTER)
        font = wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD)
        self.tx1.SetFont(font)


###########################使用说明2######################################
class PageTwo(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        colour = [(160, 255, 204), (153, 204, 255), (151, 253, 225), ]
        self.SetBackgroundColour(colour[1])
        self.tx2 = wx.StaticText(self, -1, "选择检索", (355, 45),
                                    (100, -1), wx.ALIGN_CENTER)
        font = wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD)
        self.tx2.SetFont(font)


###########################使用说明3######################################
class PageThree(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        colour = [(160, 255, 204), (153, 204, 255), (151, 253, 225), ]
        self.SetBackgroundColour(colour[2])
        self.tx3 = wx.StaticText(self, -1, "日志", (355, 45),
                                    (100, -1), wx.ALIGN_CENTER)
        font = wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD)
        self.tx3.SetFont(font)

app = wx.App()
Example(None, title='HTTP Tool')
app.MainLoop()
http_resquest.py:
import requests

class Http_R():
    def __init__(self):
       pass
    def Get(self,url,data = None,header = None):
        try:
           res = requests.get(url, params=data,headers = header,timeout=10)
           return res.headers, res.json()
        except:
            print("请求错误")

    def Post(self,url,data = None,header = None):
        try:
            res = requests.post(url, params=data,headers = header,timeout=10)
            return res.headers, res.json()
        except:
            print("请求错误")

演示:

 

posted @ 2020-02-10 16:06  -零  阅读(737)  评论(0编辑  收藏  举报