-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.py
128 lines (93 loc) · 3.37 KB
/
example.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os
import sys
import pprint
import pytz
import time
import datetime
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.gen
import tornado.options
from tornado.options import options
from tornado.options import define
define('debug', default=False, help='Debug', type=bool, group='Application')
class BaseHandler(tornado.web.RequestHandler):
def initialize(self, **kwargs):
super(BaseHandler, self).initialize(**kwargs)
self.kwargs = kwargs
self.start = datetime.datetime.now(pytz.UTC)
@tornado.gen.coroutine
def prepare(self):
if self.request.headers.get('X-Post-Action'):
self.set_status(202)
self.finish()
## Do something coroutine specific here.. in this case we will just pprint some data
pprint.pprint(
{
'start': self.start,
'end': datetime.datetime.now(pytz.UTC),
'duration': (datetime.datetime.now(pytz.UTC) - self.start).total_seconds(),
'request': {
'protocol': self.request.protocol,
'uri': self.request.uri,
'body': self.request.body,
'full_url': self.request.full_url(),
'args': self.path_args,
'kwargs': self.path_kwargs,
'headers': dict((key,value) for key, value in self.request.headers.iteritems() if not key.startswith('X-Response')),
},
'response': {
'headers': dict((key.replace('X-Response-',''), value) for key, value in self.request.headers.iteritems() if key.startswith('X-Response')),
},
'kwargs': self.kwargs,
}
)
if self.request.headers.get('X-Throughput-Junkie'):
self.set_status(200)
self.finish()
class StubHandler(BaseHandler):
def check_xsrf_cookie(self, *args, **kwargs):
pass
@tornado.web.asynchronous
@tornado.gen.engine
def get(self, *args, **kwargs):
self.write(dict(self.request.headers))
self.set_header('Random-Junk', 'gobbldegook')
yield tornado.gen.Task(tornado.ioloop.IOLoop.instance().add_timeout, time.time() + 1)
self.finish()
def head(self, *args, **kwargs):
self.write('')
def post(self, *args, **kwargs):
self.write(self.request.body)
def patch(self, *args, **kwargs):
self.write('')
def delete(self, *args, **kwargs):
self.write('')
def options(self, *args, **kwargs):
self.write('')
def main():
tornado.options.parse_command_line()
handlers = [
tornado.web.url(r'/', StubHandler, name='index'),
]
settings = dict(
#...
**options.as_dict()
)
application = tornado.web.Application(handlers=handlers, **settings)
http_server = tornado.httpserver.HTTPServer(application, xheaders=True)
http_server.listen(8081)
loop = tornado.ioloop.IOLoop.instance()
try:
loop_status = loop.start()
except KeyboardInterrupt:
loop_status = loop.stop()
return loop_status
if __name__ == '__main__':
try:
sys.exit(main())
except KeyboardInterrupt:
pass