Redis sessions for Tornado
Project description
# pycket
This is a session library, written for use with Redis and Tornado web server.
## License
This software is under BSD 2-Clause License (see LICENSE file)
## Requirements
Non-Python requirements:
* Redis (tested with version 2.4.0)
Python requirements (included in setup script)
* [Tornado](http://pypi.python.org/pypi/tornado) (tested with 2.1.1, installable via "tornado" package in PyPI)
* [redis-py](http://pypi.python.org/pypi/redis/) (tested with 2.4.9, installable via "redis" package in PyPI)
### Development requirements
If you wish to contribute to the project as a developer, just install the requirements file included in the project with pip.
## Examples
You have two ways of using pycket sessions in your application.
The easier way is including the appropriate mixin(s) in the handler's inheritance list, and the "session" member will become available:
```python
from pycket.session import SessionMixin
class MyHandler(tornado.web.RequestHandler, SessionMixin):
def get(self):
self.session.set('foo', ['bar', 'baz'])
foo = self.session.get('foo') # will get back the list ['bar', 'baz']
```
The other way (harder, but less coupled) is to instantiate a SessionManager and passing the handler instance to the initializer:
```python
from pycket.session import SessionManager
class MyHandler(tornado.web.RequestHandler):
def get(self):
session = SessionManager(self)
session.set('foo', ['bar', 'baz'])
foo = session.get('foo') # will get back the list ['bar', 'baz']
```
For both examples above the session instance is a SessionManager.
SessionManager instances act as a dictionary, so they can retrieve values with a default alternative, like:
```python
session.get("this doesn't exist", "so give me this instead")
```
and they can also get and set values with square-brackets, like:
```python
session['gimme'] = 'Fire!'
print session['gimme'] # 'Fire!'
```
## Settings
pycket understands two types of settings, which must be items in the application's settings:
1. "pycket_redis": this is a dictionary containing any items that should be repassed to the redis.Redis instance to be used in the session manager (such as "host" and "port"); Notice, however, that if you want to change the dataset numbers to be used for sessions and notifications, use "db_sessions" and "db_notifications", respectively, instead of "db" (they will be converted to the "db" parameter that is passed to the Redis client for each manager afterwards);
2. "pycket_cookies": this is a dictionary containing all settings to be repassed to the RequestHandler.set_secure_cookie. If they don't contain "expires" or "expires_days" items, they will be set as None, which means that the default behaviour for the sessions is to last on browser session. (And deleted as soon as the user closes the browser.) Notice that the sessions in the database last for one day, though.
Example:
```python
application = tornado.web.Application([
(r'/', MainHandler),
], **{
'pycket_redis': {
'host': 'localhost',
'port': 6379,
'db_sessions': 10,
'db_notifications': 11,
}
'pycket_cookies': {
'expires_days': 120,
}
)
```
The default dataset numbers for sessions and notifications are, respectively, 0 and 1.
## Notifications
This feature is almost equal to the sessions, but slightly different:
* They have to be used via pycket.notification.NotificationMixin or pycket.notification.NotificationManager;
* The values persisted with them can be retrieved only once, and after this are immediately deleted from the dataset;
* The default dataset used is 1, instead of 0, to avoid conflicts with normal sessions.
## Author
This module was developed by Diogo Baeder (*/diogobaeder), who is an absolute Python lover, and is currently in love with event-driven programming and ArchLinux.
This is a session library, written for use with Redis and Tornado web server.
## License
This software is under BSD 2-Clause License (see LICENSE file)
## Requirements
Non-Python requirements:
* Redis (tested with version 2.4.0)
Python requirements (included in setup script)
* [Tornado](http://pypi.python.org/pypi/tornado) (tested with 2.1.1, installable via "tornado" package in PyPI)
* [redis-py](http://pypi.python.org/pypi/redis/) (tested with 2.4.9, installable via "redis" package in PyPI)
### Development requirements
If you wish to contribute to the project as a developer, just install the requirements file included in the project with pip.
## Examples
You have two ways of using pycket sessions in your application.
The easier way is including the appropriate mixin(s) in the handler's inheritance list, and the "session" member will become available:
```python
from pycket.session import SessionMixin
class MyHandler(tornado.web.RequestHandler, SessionMixin):
def get(self):
self.session.set('foo', ['bar', 'baz'])
foo = self.session.get('foo') # will get back the list ['bar', 'baz']
```
The other way (harder, but less coupled) is to instantiate a SessionManager and passing the handler instance to the initializer:
```python
from pycket.session import SessionManager
class MyHandler(tornado.web.RequestHandler):
def get(self):
session = SessionManager(self)
session.set('foo', ['bar', 'baz'])
foo = session.get('foo') # will get back the list ['bar', 'baz']
```
For both examples above the session instance is a SessionManager.
SessionManager instances act as a dictionary, so they can retrieve values with a default alternative, like:
```python
session.get("this doesn't exist", "so give me this instead")
```
and they can also get and set values with square-brackets, like:
```python
session['gimme'] = 'Fire!'
print session['gimme'] # 'Fire!'
```
## Settings
pycket understands two types of settings, which must be items in the application's settings:
1. "pycket_redis": this is a dictionary containing any items that should be repassed to the redis.Redis instance to be used in the session manager (such as "host" and "port"); Notice, however, that if you want to change the dataset numbers to be used for sessions and notifications, use "db_sessions" and "db_notifications", respectively, instead of "db" (they will be converted to the "db" parameter that is passed to the Redis client for each manager afterwards);
2. "pycket_cookies": this is a dictionary containing all settings to be repassed to the RequestHandler.set_secure_cookie. If they don't contain "expires" or "expires_days" items, they will be set as None, which means that the default behaviour for the sessions is to last on browser session. (And deleted as soon as the user closes the browser.) Notice that the sessions in the database last for one day, though.
Example:
```python
application = tornado.web.Application([
(r'/', MainHandler),
], **{
'pycket_redis': {
'host': 'localhost',
'port': 6379,
'db_sessions': 10,
'db_notifications': 11,
}
'pycket_cookies': {
'expires_days': 120,
}
)
```
The default dataset numbers for sessions and notifications are, respectively, 0 and 1.
## Notifications
This feature is almost equal to the sessions, but slightly different:
* They have to be used via pycket.notification.NotificationMixin or pycket.notification.NotificationManager;
* The values persisted with them can be retrieved only once, and after this are immediately deleted from the dataset;
* The default dataset used is 1, instead of 0, to avoid conflicts with normal sessions.
## Author
This module was developed by Diogo Baeder (*/diogobaeder), who is an absolute Python lover, and is currently in love with event-driven programming and ArchLinux.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pycket-0.1.1.tar.gz
(5.1 kB
view details)
Built Distribution
pycket-0.1.1-py2.7.egg
(10.6 kB
view details)
File details
Details for the file pycket-0.1.1.tar.gz
.
File metadata
- Download URL: pycket-0.1.1.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c198950663aac69d0672c7743aac160fe2f4f5d4a228cf2ace99f2c86948bd9 |
|
MD5 | 629fce1332bed7ba5260fb46267e8957 |
|
BLAKE2b-256 | ae9107ce7e2668198bb67d4d027452579982a6e748d3c26a30dfe046929d6c6a |
File details
Details for the file pycket-0.1.1-py2.7.egg
.
File metadata
- Download URL: pycket-0.1.1-py2.7.egg
- Upload date:
- Size: 10.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb2d515283b899abcb1041511305861822ffca38553aa808b57fdcbcaf9fc1e8 |
|
MD5 | 94f062c54a06e81df2aed1219ff983bf |
|
BLAKE2b-256 | ec24979c7e259f3591a45393d36adf1338f5550d847431c0b7cffd004285504a |