Skip to content

Instantly share code, notes, and snippets.

@chibisov
Last active December 22, 2015 19:49
Show Gist options
  • Save chibisov/6522525 to your computer and use it in GitHub Desktop.
Save chibisov/6522525 to your computer and use it in GitHub Desktop.
Show request info in chrome dev tools manner. Tornado used
#!/usr/bin/env python
"""
$ curl -d 'hello=world' http://localhost:8888/path/?someparam=1
Request URL: http://localhost:8888/path/?someparam=1
Request Method: POST
Request Headers:
Host: localhost:8888
Content-Type: application/x-www-form-urlencoded
Content-Length: 11
Accept: */*
User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
Request Body:
hello=world
Query String Parameters:
{"someparam": ["1"], "hello": ["world"]}
"""
import json
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.set_header('Content-Type', 'text/plain')
self.set_header('Access-Control-Allow-Origin', '*')
self.set_header('Access-Control-Allow-Headers', 'accept, origin, x-requested-with, x-csrftoken, content-type')
response = self._get_response()
print response
self.write(response)
head = get
post = get
put = get
patch = get
delete = get
options = get
def _get_response(self):
headers_text = '\n'.join(
['%s: %s' % (header_name, value)
for header_name, value
in self.request.headers.items()]
)
return """
Request URL: {request_url}
Request Method: {request_method}
Request Headers:
{headers}
Request Body:
{body}
Query String Parameters:
{query_string_parameters}
""".format(
request_url=self.request.full_url(),
request_method=self.request.method,
headers=headers_text,
body=self.request.body,
query_string_parameters=json.dumps(self.request.arguments)
).lstrip()
application = tornado.web.Application([
(r".*", MainHandler),
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment