Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

create example, fixing a bunch of bugs in the process

  • Loading branch information...
commit 34bc77143a27ec74a646a724243d7d264bbf0963 1 parent ecaf27c
Carlo Cabanilla authored
Showing with 67 additions and 11 deletions.
  1. +54 −0 example.py
  2. +13 −11 memnado.py
54 example.py
View
@@ -0,0 +1,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()
24 memnado.py
View
@@ -1,5 +1,6 @@
import functools
import socket
+from base64 import b64encode, b64decode
from tornado.ioloop import IOLoop
from tornado.iostream import IOStream
@@ -13,7 +14,8 @@ def __init__(self, host, port):
s.connect((self.host, self.port))
self.stream = IOStream(s)
- def set(self, key, value, expiry=0, callback=lambda d: d):
+ def set(self, key, value, callback, expiry=0):
+ value = b64encode(value)
content_length = len(value)
self.stream.write("set %s 1 %s %s\r\n%s\r\n" % (key, expiry,
content_length, value))
@@ -21,19 +23,19 @@ def set(self, key, value, expiry=0, callback=lambda d: d):
def get(self, key, callback):
def process_get(stream, cb, data):
- status, k, flags, content_length = data.strip().split(' ')
- stream.read_bytes(int(content_length), functools.partial(cb, stream))
+ if data[0:3] == 'END': # key is empty
+ cb(None)
+ else:
+ status, k, flags, content_length = data.strip().split(' ')
+
+ def wrapped_cb(f):
+ return lambda data: f(b64decode(data))
+
+ stream.read_bytes(int(content_length), wrapped_cb(cb))
+ stream.read_until("\r\nEND\r\n", lambda d: d)
self.stream.write("get %s\r\n" % key)
self.stream.read_until("\r\n", functools.partial(process_get, self.stream, callback))
-if __name__ == '__main__':
- def do_stuff(stream, data):
- print "I made it! %s" % data
-
- m = Memnado("127.0.0.1", 11211)
- m.get("hey", do_stuff)
-
- IOLoop.instance().start()
Please sign in to comment.
Something went wrong with that request. Please try again.