-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.py
61 lines (56 loc) · 2.76 KB
/
core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import tornado.web
import tornado.httpserver
import tornado.ioloop
from tornado.log import enable_pretty_logging
import logging
import os.path
import pycurl
from io import BytesIO
import requests
import json
class RootHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html")
def post(self):
request_url = self.get_argument("url")
request_json_string = self.get_argument("json")
request_method = self.get_argument("method-select")
if request_method == "GET":
# Do get request to url and print response
try:
r = requests.get(request_url)
try:
response_body = r.json()
self.render("response.html", response = response_body, original_url = request_url, original_json = request_json_string, errors = None)
except ValueError as e:
self.render("response.html", response = r, original_url = request_url, original_json = request_json_string, errors = "Response body was not in JSON")
except Exception as e:
self.render("response.html", response = "", original_url = request_url, original_json = request_json_string, errors = "URL not formatted properly")
elif request_method == "POST":
# Do post request and print response
try:
request_json = json.loads(request_json_string)
try:
r = requests.post(request_url, json = request_json)
try:
response_body = r.json()
self.render("response.html", response = response_body, original_url = request_url, original_json = request_json_string, errors = None)
except ValueError as e:
self.render("response.html", response = r, original_url = request_url, original_json = request_json_string, errors = "Response body was not in JSON")
except Exception as e:
self.render("response.html", response = "", original_url = request_url, original_json = request_json_string, errors = "URL not formatted properly")
except Exception as e:
self.render("response.html", response = r, original_url = request_url, original_json = request_json_string, errors = "JSON was not correctly fomatted")
app = tornado.web.Application(
[(r"/", RootHandler),],
template_path = os.path.join(os.path.dirname(__file__), "templates"),
static_path = os.path.join(os.path.dirname(__file__), "static"),
cookie_secret = "secret",
debug = True,
)
def main():
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(8080)
tornado.ioloop.IOLoop.instance().start()
if __name__ == '__main__':
main()