位置: IT常识 - 正文
推荐整理分享Python如何使用tkinter编写GUI程序(python怎么用),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:python怎么用,python的tk,python的tk,python的tk,python tr,python tle,python tle,python tr,内容如对您有帮助,希望把文章链接给更多的朋友!
@(Python3中tkinter写的HTTP测试工具代码支持正则表达式和XPATH)
程序非常简单,暂时只支持GET方法,使用内置库tkinter编写GUI窗口,在Mac下运行效果图如下,windows运行需要更改控件宽度高度等控制参数才能很好的显示。
效果图(使用XPATH匹配房天下网站)
代码如下,需要安装requests和lxml第三方库
相关推荐:《Python视频教程》
importtkinterastkfromtkinterimportscrolledtextfromtkinterimportmessageboximportreimportrequestsfromlxmlimportetreeclassApplication(tk.Frame):def__init__(self,master=None):super().__init__(master)self.master=masterself.pack()self.create_widgets()defcreate_widgets(self):#barself.canvas=tk.Canvas(self,width=980,height=65,bg='#ebf4ec')self.image_file=tk.PhotoImage(file='network.png')self.imag=self.canvas.create_image(50,0,anchor='n',image=self.image_file)self.canvas.create_text(480,35,text='HTTP测试工具',font=('Arial',35),)self.canvas.grid(row=0,columnspan=3)#methodHTTP方法多选框self.var_method=tk.StringVar(self)self.var_method.set('GET')self.optionmenu=tk.OptionMenu(self,self.var_method,'GET','POST','PUT','PATCH','DELETE','HEAD','OPTIONS').grid(row=1,column=0,padx=2,pady=2,ipadx=10,ipady=5)#urlself.var_url=tk.StringVar(self)self.var_url.set('http://')self.url_entry=tk.Entry(self,font=('Arial',14),width=82,textvariable=self.var_url).grid(row=1,column=1,padx=1,pady=2,ipadx=10,ipady=5)#requestbuttonself.request_button=tk.Button(self,text='发送请求',fg='#436EEE',command=self.request).grid(row=1,column=2,padx=2,pady=2,ipadx=10,ipady=5)#requestheaderself.header_label=tk.Label(self,text='请求头:').grid(row=3,column=0,padx=2,pady=2,ipadx=10,ipady=5)self.var_header=tk.StringVar(self)self.var_header.set('Mozilla/5.0(Macintosh;IntelMacOSX10.12;rv:64.0)Gecko/20100101Firefox/64.0')self.header_entry=tk.Entry(self,font=('Arial',14),width=82,textvariable=self.var_header).grid(row=3,column=1,padx=2,pady=2,ipadx=10,ipady=5)#regularexpressionself.re_header=tk.Label(self,text='正则表达式:').grid(row=4,column=0,padx=2,pady=2,ipadx=10,ipady=5)self.var_re=tk.StringVar(self)self.re_entry=tk.Entry(self,font=('Arial',14),width=82,textvariable=self.var_re).grid(row=4,column=1,padx=2,pady=2,ipadx=10,ipady=5)self.re_button=tk.Button(self,text='匹配全部',command=self.re_request).grid(row=4,column=2,padx=2,pady=2,ipadx=10,ipady=5)#xpathself.xpath_label=tk.Label(self,text='Xpath表达式:').grid(row=5,column=0,padx=2,pady=2,ipadx=10,ipady=5)self.var_xpath=tk.StringVar(self)self.xpath_entry=tk.Entry(self,font=('Arial',14),width=82,textvariable=self.var_xpath).grid(row=5,column=1,padx=2,pady=2,ipadx=10,ipady=5)self.xpath_button=tk.Button(self,text='匹配全部',command=self.xpath_request).grid(row=5,column=2,padx=2,pady=2,ipadx=10,ipady=5)#informationself.var_status=tk.StringVar()self.var_status.set('当前状态:未请求')self.information=tk.Label(self,textvariable=self.var_status,bg='#48D1CC',font=('Arial',14)).grid(row=6,column=1,padx=2,pady=2,ipadx=10,ipady=5)#resultsself.textarea=tk.scrolledtext.ScrolledText(self,width=117,height=25,bg='black',fg='#00EE00',font=('Arial',14))self.textarea.grid(row=7,columnspan=3,pady=10)#xpath匹配defxpath_request(self):xpath=self.var_xpath.get()ifxpath=='':messagebox.showinfo('警告',message='请输入XPATH表达式!')else:url=self.var_url.get()header=self.var_header.get()headers={'User-Agent':header}data=requests.get(url=url,headers=headers).textresults=etree.HTML(data).xpath(xpath)self.textarea.delete('1.0','end')foriinresults:self.textarea.insert('end',i.text+'\n')#正则表达式匹配defre_request(self):exp=self.var_re.get()ifexp=='':messagebox.showinfo('警告',message='请输入正则表达式!')else:url=self.var_url.get()header=self.var_header.get()headers={'User-Agent':header}data=requests.get(url=url,headers=headers).textresults=re.compile(exp).findall(data)self.textarea.delete('1.0','end')self.textarea.insert('end',results)defrequest(self):method=self.var_method.get()ifmethod=='GET':self.get_request()elifmethod=='POST':self.post_request()defpost_request(self):pass#还没写#发送HTTPGET请求defget_request(self):self.textarea.delete('1.0','end')ifself.var_header.get()=='':headers={'User-Agent':''}else:headers={'User-Agent':self.var_header.get()}url=self.var_url.get()data=requests.get(url=url,headers=headers)if(data.status_code==200):self.var_status.set('当前状态:'+'GET'+url+''+str(data.status_code)+'OK')elif(data.status_code==404):self.var_status.set('当前状态:'+str(data.status_code)+'Notfound')else:self.var_status.set('当前状态:'+str(data.status_code))content=data.textself.textarea.insert('end',content)if__name__=='__main__':root=tk.Tk()root.title('KKHTTP测试工具')root.geometry('980x700')app=Application(master=root)app.mainloop()上一篇:DedeCMS栏目二级域名设置教程(二级栏目怎么做)
下一篇:dedecms后台功能之自动摘要和分页介绍(dedecms建站操作)
友情链接: 武汉网站建设