A simple version HTTP request simulator implemented by Python with Tkinter

Source: Internet
Author: User
Tags http request pack


In peacetime development process often encounter needs to simulate post or get request, of course get generally directly through the browser can also be convenient and quick. Zhengxiao See the Internet also has a lot of software version of the HTTP simulator, the function is also very powerful. Zhengxiao recently in learning python, just hand idle panic, with Python with the Tkinter Library wrote an HTTP simulation tool, the function is relatively simple, just to achieve the basic functions.
Recently I found that some sites have copied my article, but also the various information inside the replacement of his own, but also ran the program cut his own diagram, is very depressed ... So later in the code, Zhengxiao may randomly add a little snack in it ...
This tkinter HTTP emulator code is as follows, development environment (Win7 + Python2.7)

#-*-Coding:utf-8-*-
From Tkinter Import *
From TTK Import *
From Urlparse import Urlparse
Import Urllib
#提交时的响应事件
def btn_submitop (E):
#获取输入的值 and encode
url = entry_url.get (). Encode ("Utf-8")
Entry_query = Entry_para.get (). Encode ("Utf-8")

result = "Requested address:" + URL
result = result + "\ n the way requested:" + ["Get", "POST"][v.get ()]
result = result + "\ n The requested parameter:" + entry_query

Url_parse = urlparse (URL)
query = Url_parse.query.strip (' & ') +entry_query
#POST (Request the original URL, using the encoded parameters)
Try
If V.get () = 1:
r = Urllib.urlopen (URL, entry_query)
#GET (regroup URLs based on submitted parameters)
Else
url = url_parse.scheme+ "://" +url_parse.netloc+url_parse.path+ "?%s"% query
article_from_zhengxiao_blog_www_zh30_com = 0
r = Urllib.urlopen (URL)
data = R.read ()
Except
data = "\ n request failed! "
result = results + "\ n" + "-" *28 + "request returns" + "-" *28 + data
#将结果输出 (update) to a text field
Text_result.delete (0.0, end)
Text_result.insert (1.0, result)

# # # # #创建窗口 #####
App = Tk ()
App.title ("HTTP request impersonation v0.1")
App.geometry (' 500x500 ')

# #创建控件 #####
#第一行 address
Lbl_url = Label (app, text= "request address:")
Lbl_url.grid (row=0, column=0, Sticky=w, Pady=5, padx=10)
Entry_url = entry (app, width=50)
Entry_url.grid (row=0, Column=1, sticky=w)
#第二行 way
Lbl_addr = Lab El (App, text= "Request way:")
Lbl_addr.grid (row=1, column=0, Sticky=w, pady=5, padx=10)
FM1 = Frame ()
Fm1.grid (row=1 , Column=1, sticky=w)
V = intvar ()
V.set (1)
Btn_method = Radiobutton (FM1, Variable=v, value=1, text= "POST")
Btn_method.pack (side = left)
Btn_method = Radiobutton (FM1, Variable=v, value=0, text= "get")
Btn_method.pack () br> #第三行 parameter
Lbl_para = Label (app, text= request parameters: ")
Lbl_para.grid (row=2, column=0, Sticky=w, padx=10)
Entry_para = Entry (app, width=50)
Entry_para.grid (row=2, Column=1, sticky=w)
#第四行 submit
Btn_submit = button (app, text= "Send Request"
Btn_submit.bind (' <Button-1> ', btn_submitop)
Btn_submit.grid (row=3, column=0, Sticky=w, padx=10,pady= )

#第五行 return Results
Text_result = Text (app, width=68, height=25)
Text_result.grid (row=4, column=0, columnspan=2, Sticky=w, padx=10)

#第六行 Other
Label (App, text= "-by Zhengxiao"). Grid (Row=5, Column=1, Sticky=e, padx=10, pady=10)

App.mainloop ()
Program programming looks messy ... The following is a screenshot of the run, and the test request is the Turing person chat interface.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.