Skip to content
This repository was archived by the owner on Jul 29, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions demos/cyclone-redis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Cyclone Redis Demo

See [github/firoix/cyclone] for the latest source.

## About

This demonstrates integrating [pycket] with [cyclone], using a redis backend (but easily switched to using memcached).

[Cyclone] is a clone of facebook's [Tornado], on top of [Twisted].

Although cyclone and tornado are very similar, cyclone leverages all
enterprise class features of Twisted, and more. It is extremely stable, and
ready for production.


[cyclone]: http://cyclone.io
[github/firoix/cyclone]: https://github.com/fiorix/cyclone
[Tornado]: http://tornadoweb.org
[Twisted]: http://twistedmatrix.com
[pycket]: https://github.com/diogobaeder/pycket
95 changes: 95 additions & 0 deletions demos/cyclone-redis/pycketdemo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""
Cyclone Redis Demo

This demonstrates integrating pycket with cyclone, using a redis
backend (but easily switched to using memcached).

"""

import sys

import cyclone.auth
import cyclone.escape
import cyclone.web

from twisted.python import log
from twisted.internet import reactor
from pycket.session import SessionMixin


class Application(cyclone.web.Application):
def __init__(self):
handlers = [
(r"/", MainHandler),
(r"/auth/login", AuthHandler),
(r"/auth/logout", LogoutHandler),
]
settings = dict(
cookie_secret="32oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
debug=True,
login_url="/auth/login",
logout_url="/auth/logout",
)

settings['pycket'] = {
'engine': 'redis',
'storage': {
'host': 'localhost',
'port': 6379,
'db_sessions': 10,
'db_notifications': 11
}
}

cyclone.web.Application.__init__(self, handlers, **settings)


class BaseHandler(cyclone.web.RequestHandler, SessionMixin):
def get_current_user(self):
user = self.session.get('user')
if not user:
return None
return user


class MainHandler(BaseHandler):
@cyclone.web.authenticated
def get(self):
name = cyclone.escape.xhtml_escape(self.current_user)
self.write("Hello, " + name)
self.write("<br><br><a href=\"/auth/logout\">Log out</a>")


class AuthHandler(BaseHandler, SessionMixin):

def get(self):
self.write('<form method="post">'
'Enter your username: <input name="username" type="text">'
'<button type="submit" class="btn">Login</button></form>')

def post(self):
username = self.get_argument('username')
if not username:
self.write('<form method="post">Enter your username: '
'<input name="username" type="text">'
'<button type="submit" class="btn">Login</button>'
'</form>')
else:
self.session.set('user', username)
self.redirect('/')


class LogoutHandler(BaseHandler, SessionMixin):
def get(self):
self.session.delete('user')
self.redirect("/")


def main():
log.startLogging(sys.stdout)
reactor.listenTCP(8888, Application(), interface="127.0.0.1")
reactor.run()


if __name__ == "__main__":
main()