-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.py
54 lines (40 loc) · 1.33 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
from datetime import datetime
import tornado.httpserver
import tornado.ioloop
import tornado.web
from tornado.web import asynchronous
import memnado
m = memnado.Memnado('127.0.0.1', 11211)
class MainHandler(tornado.web.RequestHandler):
key = 'data'
@asynchronous
def post(self):
data = self.get_argument('data', '')
def set_it(response):
self.write('Saved %s. <a href="/">View it.</a>' % data)
self.finish()
now = datetime.today().isoformat()
to_store = "%s: %s" % (now, str(data))
m.set(self.key, to_store, self.async_callback(set_it))
@asynchronous
def get(self):
def do_it(data):
self.write("<pre>%s</pre>" % str(data))
self.finish()
self.write("""
<form action="/" method="post">
<label>Set some data: </label>
<input name="data" value="" />
<input type="submit" />
</form>
<br />
""")
self.write("Current data: ")
m.get(self.key, self.async_callback(do_it))
application = tornado.web.Application([
(r"/", MainHandler)
])
if __name__ == "__main__":
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8888)
tornado.ioloop.IOLoop.instance().start()