-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
43 lines (33 loc) · 832 Bytes
/
server.py
File metadata and controls
43 lines (33 loc) · 832 Bytes
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
import os
from tornado import gen
import tornado.ioloop
import tornado.web
DEFAULT_PAUSE = 5
class MainHandler(tornado.web.RequestHandler):
@gen.coroutine
def get(self, t=None):
t = self._parse(t)
yield gen.sleep(t)
self.finish({"waited": t})
@gen.coroutine
def post(self, t=None):
t = self._parse(t)
yield gen.sleep(t)
self.finish(self.request.body)
@staticmethod
def _parse(t):
try:
t = float(t)
except:
t = DEFAULT_PAUSE
return t
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
(r"/(\S+)", MainHandler),
])
if __name__ == "__main__":
port = os.getenv('PORT', 80)
app = make_app()
app.listen(port)
tornado.ioloop.IOLoop.current().start()