Table of contents
1.
Introduction
2.
Session Tracking
2.1.
Cookies
2.2.
Hidden form fields
2.3.
URL rewriting
3.
Tracking end-user’s activity
4.
Frequently Asked Questions
4.1.
How is a session initiated?
4.2.
Can cookies track your activity?
4.3.
Which method is used in CherryPy for session tracking?
5.
Conclusion
Last Updated: Aug 13, 2025

Tutorial 4: Track my end-user’s activity in cherrypy

Author Nagendra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

CherryPy is an object-oriented web application framework that uses the computer language Python. Although it covers the HTTP protocol and enables rapid development of web applications, it is still low level and only offers the features listed in RFC 7231. With its Create, Retrieve, Update, and Delete functionalities, this framework is primarily for programmers who want to use Python to build portable database-driven web applications.
This blog explains the details of session tracking and the details of tracking the end-user’s activity in CherryPy.

Without further ado, let's get started. 

Session Tracking

User requests are tracked, and their state is kept up to date using session tracking. It is a system for keeping data on certain users and identifying their requests when connecting to the web server. Each request made to the server using the stateless HTTP protocol is handled as a fresh request. 

Cookies, URL rewriting, and hidden form fields are just a few methods for tracking user sessions.

Cookies

A cookie is a special ID given to customers after their initial visit to a website. This client will deliver the cookie that the server previously provided whenever it reconnects to the server. When that happens, the server will realise it is the same person.

Hidden form fields

When submitting a request to the server, hidden form fields are utilised to add information, such as a special ID. Although the user cannot see these fields, the server can identify the client because every time a request is made, the server also includes the client's unique ID.

URL rewriting

When a URL is rewritten, a special session ID is added to the URL before it is sent to the server. Since the session ID is a component of the URL, users may see it right away.

Tracking end-user’s activity

A program may occasionally need to monitor the user's activity for a period. The typical method is to employ a session identifier, which is carried throughout the user and program communication.

Code:

import random
import string
 
import cherrypy
 
class StringGenerator(object):
    @cherrypy.expose
    def index(self):
        return """<html>
          <head></head>
          <body>
            <form method="get" action="generate">
              <label>Enter the length of String <input type="text" value="15" name="length" /><label>
              <button type="submit">Generate String</button>
            </form>
          </body>
        </html>"""
 
    @cherrypy.expose
    def generate(self, length=15):
        some_string = ''.join(random.sample(string.hexdigits, int(length)))
        cherrypy.session['mystring'] = some_string
        return some_string
 
    @cherrypy.expose
    def display(self):
        return cherrypy.session['mystring']
 
if __name__ == '__main__':
    conf = {
        '/': {
            'tools.sessions.on': True
        }
    }
    cherrypy.quickstart(StringGenerator(), '/', conf)
You can also try this code with Online Python Compiler
Run Code

 

We create the string and store it in the current session as well. You can see the string you just made by going to http://localhost:8080/, creating a random string, and then going to http://localhost:8080/display.

Output: 

The following output is displayed at http://localhost:8080/ 

Output

The following output is displayed at http://localhost:8080/display 

Output

You can see how to activate session support in your CherryPy application. CherryPy stores sessions by default in the process's memory. Additionally, it supports more persistent backends.

Frequently Asked Questions

How is a session initiated?

When a user establishes their identity by a password or another authentication procedure, a session is initiated.

Can cookies track your activity?

The most common tool for tracking sessions is cookies. Cookies are key-value pairs of data that the server sends to the browser. The browser needs to save this to its location on the client PC. The cookie is sent along with every request the browser makes to that server.

Which method is used in CherryPy for session tracking?

The cherrypy.session() is used for session tracking.

Conclusion

In this article, we have extensively discussed the details of session tracking along with the details of tracking the end-user’s activity in CherryPy.

We hope that this blog has helped you enhance your knowledge regarding tracking the end-user’s activity in CherryPy, and if you would like to learn more, check out our articles on CherryPy and Basics of Python. You can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews. Do upvote our blog to help other ninjas grow. Happy Coding!!

Thank You Image
Live masterclass