Tracking User Activity with CherryPy

Tracking User Activity with CherryPy

Python: CherryPy


It's very common that an application needs to follow a user's activity, whether to provide a better experience or to check for any unusual activity. The usual mechanism is to use a session identifier that is carried during the conversation between the user and your application. By default, the session ID is passed in a cookie, so the client's browser must have cookies enabled for the application.

We will write a simple application where '<application-name>/' opens the main page which contains the forms for the two methods square and power. We will store the values provided to these functions and use '<application-name>/display' to display the last session.

Save the below code in a file named ''.

import cherrypy class application: @cherrypy.expose def index(self): return """<html> <head></head> <body> <p>Call square function:<br> <form method="get" action="square"> <input type="text" name="num" placeholder="num"/> <button type="submit">Submit</button> </form> </p> <p>Call power function:<br> <form method="get" action="power"> <input type="text" name="num" placeholder="num"/> <input type="text" name="pow" placeholder="pow"/> <button type="submit">Submit</button> </form> </p> </body> </html>""" @cherrypy.expose def square(self, num=2): result = int(num)**2 cherrypy.session['function_called'] = 'square' cherrypy.session['num'] = num if 'pow' in cherrypy.session: del(cherrypy.session['pow']) cherrypy.session['result'] = result return "The value of {} square is {}".format(num, result) @cherrypy.expose def power(self, num=2, pow=2): result = int(num)**int(pow) cherrypy.session['function_called'] = 'power' cherrypy.session['num'] = num cherrypy.session['pow'] = pow cherrypy.session['result'] = result return "The value of {} to the power {} is {}".format(num, pow, result) @cherrypy.expose def display(self): result = '' for ele in cherrypy.session.keys(): result += ele result += ': ' result += str(cherrypy.session[ele]) result += '<br>' return result if __name__ == '__main__': conf = { '/': { 'tools.sessions.on': True } } cherrypy.quickstart(application(), '/', conf)

Here we can see the methods and the corresponding return values.

We can store the session details by thinking of cherrypy.session as a dictionary.

Notice that the configurations of our application are changed to start session handling and the display method is defined to display last session details in a human-readable form.

Run the code through the Terminal/Command Line as follows:


Now, if we enter http://localhost:8080/ in the address bar of a browser, the output will be something like this

Now if we call our display method, by entering http://localhost:8080/display in the address bar after making a call to square function we will an output similar to this

Similarly, we can call the display method after calling the power method. And the output will be something like this

More Articles of Aniket Sharma:

Name Views Likes
Pyperclip: Installation and Working 990 2
Number Guessing Game using Python 683 2
Pyperclip: Not Implemented Error 1026 2
Hangman Game using Python 16785 2
Using Databases with CherryPy application 1672 2
nose: Working 506 2
pytest: Working 511 2
Open Source and Hacktoberfest 867 2
Managing Logs of CherryPy applications 1001 2
Top 20 Data Science Tools 684 2
Ajax application using CherryPy 799 2
REST application using CherryPy 663 2
On Screen Keyboard using Python 5508 2
Elastic Net Regression 815 2
US Presidential Election 2020 Prediction using Python 794 2
Sound Source Separation 1164 2
URLs with Parameters in CherryPy 1633 2
Testing CherryPy application 635 2
Handling HTML Forms with CherryPy 1448 2
Applications of Natural Language Processing in Businesses 508 2
NetworkX: Multigraphs 648 2
Tracking User Activity with CherryPy 1397 2
CherryPy: Handling Cookies 820 2
Introduction to NetworkX 633 2
TorchServe - Serving PyTorch Models 1301 2
Fake News Detection Model using Python 734 2
Keeping Home Routers secure while working remotely 483 2
Email Slicer using Python 2996 2
NetworkX: Creating a Graph 1108 2
Best Mathematics Courses for Machine Learning 551 2
Hello World in CherryPy 680 2
Building dependencies as Meson subprojects 978 2
Vehicle Detection System 1081 2
NetworkX: Examining and Removing Graph Elements 607 2
Handling URLs with CherryPy 536 2
PEP 8 - Guide to Beautiful Python Code 757 2
NetworkX: Drawing Graphs 624 2
Mad Libs Game using Python 643 2
Hosting Cherry applications 612 2
Top 5 Free Online IDEs of 2020 866 2
pytest: Introduction 534 2
Preventing Pwned and Reused Passwords 581 2
Contact Book using Python 2095 2
Introduction to CherryPy 547 2
nose: Introduction 505 2
Text-based Adventure Game using Python 3000 2
NetworkX: Adding Attributes 2278 2
NetworkX: Directed Graphs 1021 2
Dice Simulator using Python 560 2
Decorating CherryPy applications using CSS 833 2