CherryPy: Handling Cookies














































CherryPy: Handling Cookies



Python: CherryPy

CHERRYPY: HANDLING COOKIES

Cookies are small text files stored in a user's web browser directory. Websites and applications use cookies to validate a user's identity. Without cookies users will have to enter their login credentials on every page.

CherryPy uses the Cookie.SimpleCookie object type of Python's Cookie module to handle cookies. Below is how to perform basic cookie operations in CherryPy:

  • To send a cookie to a browser, set cherrypy.response.cookie[key] = value.

  • To retrieve a cookie sent by a browser, use cherrypy.request.cookie[key].

  • To delete a cookie, send the cookie with its expiration time set to 0:

Eg.:

cherrypy.response.cookie[key] = value cherrypy.response.cookie[key]['expires'] = 0

We will now design an application which will set user cookies and display them to the user.

Save the below code in a file named 'cookie.py'.

import cherrypy class application: @cherrypy.expose def set(self): cookie = cherrypy.response.cookie cookie['cookie_name'] = 'cookie_val' cookie['cookie_name']['path'] = '/' cookie['cookie_name']['max-age'] = 3600 cookie['cookie_name']['version'] = 1 return "<html><body>Cookies are now stored in your web browser.</body></html>" @cherrypy.expose def read(self): cookie = cherrypy.request.cookie res = """<html><body>There are {} cookies.<br /> Refer to the below list for more information:<br /><br />""".format(len(cookie)) for name in cookie.keys(): res += "name: {}, value: {}<br>".format(name, cookie[name].value) return res + "</body></html>" if __name__ == '__main__': cherrypy.quickstart(application(), '/cookie')


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

python3 cookie.py

Now, if we enter http://localhost:8080/cookie/set in the address bar of a browser a cookie is now stored and we get the output which tells us about the same.


We can see the cookies stored by entering http://localhost:8080/cookie/read in the address bar. The output shows the cookies we set previously.



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 1027 2
Hangman Game using Python 16785 2
Using Databases with CherryPy application 1673 2
nose: Working 507 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 664 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 509 2
NetworkX: Multigraphs 649 2
Tracking User Activity with CherryPy 1397 2
CherryPy: Handling Cookies 821 2
Introduction to NetworkX 633 2
TorchServe - Serving PyTorch Models 1302 2
Fake News Detection Model using Python 734 2
Keeping Home Routers secure while working remotely 484 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 608 2
Handling URLs with CherryPy 537 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 613 2
Top 5 Free Online IDEs of 2020 867 2
pytest: Introduction 534 2
Preventing Pwned and Reused Passwords 582 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 2279 2
NetworkX: Directed Graphs 1021 2
Dice Simulator using Python 560 2
Decorating CherryPy applications using CSS 833 2

Comments