Introduction 📝 🔖
Have you heard the word cherryPy? No, it's not any fruit name, CherryPy is a Python web framework that provides a user-friendly interface to the HTTP protocol for Python developers. It's also known as a web application library. In this article, we will discuss the Cookies module and sessions in cherryPy. We will also discuss the Filesystem backend and Memcached backend.

Cookies 🎫
To manage cookies, CherryPy employs Python's Cookie module, specifically the Cookie.SimpleCookie object type.
-
Set cherrypy.response.cookie[key] = value to send a cookie to a browser.
-
Cherrypy.request.cookie[key] is used to retrieve a cookie sent by a browser.
-
To delete a cookie (on the client side), send it with the expiration time set to 0:
cherrypy.response.cookie[key] = value
cherrypy.response.cookie[key]['expires'] = 0
It is critical to recognize that request cookies are not automatically copied to response cookies. Clients will send the same cookies with each request, so cherrypy.request.cookie should be filled in each time. However, because the server does not have to send the same cookies with each response, cherrypy.response.cookie is usually empty. To "delete" (expire) a cookie, you must first set cherrypy.response.cookie[key] = value and then set its expires attribute to 0.
Example:
import cherrypy
class MyCookieApp(object):
@cherrypy.expose
def set(self):
cookie = cherrypy.response.cookie
cookie['cookieName'] = 'cookieValue'
cookie['cookieName']['path'] = '/'
cookie['cookieName']['max-age'] = 3600
cookie['cookieName']['version'] = 1
return "<html><body>Hello,you can see your cookies</body></html>"
@cherrypy.expose
def read(self):
cookie = cherrypy.request.cookie
res = """<html><body>Hello, Welcome %s cookies.<br />
You can see the cookies list/values:<br />""" % len(cookie)
for name in cookie.keys():
res += "name: %s, value: %s<br>" % (name, cookie[name].value)
return res + "</body></html>"
if __name__ == '__main__':
cherrypy.quickstart(MyCookieApp(), '/cookie')
Using sessions 🌐
Sessions are a popular mechanism for developers to identify users and synchronize their activity. CherryPy does not activate sessions by default because it is not a required feature; to enable it, simply add the following settings to your configuration:
[/]
tools.sessions.on: True
cherrypy.quickstart(home, '/', "home.conf")
Sessions are stored in RAM by default, so if you restart your server, all of your current sessions will be lost. You can instead store them in Memcached or on the filesystem.
The following is how you use sessions in your applications:
import cherrypy
@cherrypy.expose
def index(self):
if "count" not in cherrypy.session:
cherrypy.session["count"] = 0
cherrypy.session["count"] = 1
Every time the index page handler is called in this snippet, the current user's session's 'count' key is increased by one.
CherryPy determines which session to use by inspecting the cookie that is sent with the request. This cookie contains the session identifier that CherryPy uses to retrieve the user's session from storage.





