Handling URLs with CherryPy














































Handling URLs with CherryPy



Python: CherryPy

HANDLING URLS WITH CHERRYPY

We can handle multiple URLs in our application by defining methods in our class. Suppose we want an application where '<application-name>/' opens the main page '<application-name>/second_page' opens another page. To do this in CherryPy we will define two methods in our class, one index(self) to handle the first page and another second_page(self) to handle the second page.

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

import cherrypy


class application:
@cherrypy.expose
def index(self):
return "This is the main page of the application."

@cherrypy.expose
def second_page(self):
return "This is the second page of the application."


if __name__ == '__main__':
cherrypy.quickstart(application())

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

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

python3 multi_url.py

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


And if we enter http://localhost:8080/second_page in the address bar the output will be


The URL contains various parts:

  • http:// which roughly indicates it's a URL using the HTTP protocol.

  • localhost:8080 is the server's address. It's made of a hostname and a port number.

  • /second_page is the path segment of the URL. This is what CherryPy uses to locate an exposed method to respond.

CherryPy uses the index() method to handle / by default.


More Articles of Aniket Sharma:

Name Views Likes
Pyperclip: Installation and Working 1050 2
Number Guessing Game using Python 720 2
Pyperclip: Not Implemented Error 1316 2
Hangman Game using Python 18430 2
Using Databases with CherryPy application 2059 2
nose: Working 557 2
pytest: Working 551 2
Open Source and Hacktoberfest 924 2
Managing Logs of CherryPy applications 1081 2
Top 20 Data Science Tools 740 2
Ajax application using CherryPy 882 2
REST application using CherryPy 722 2
On Screen Keyboard using Python 7000 2
Elastic Net Regression 914 2
US Presidential Election 2020 Prediction using Python 843 2
Sound Source Separation 1280 2
URLs with Parameters in CherryPy 1975 2
Testing CherryPy application 719 2
Handling HTML Forms with CherryPy 1700 2
Applications of Natural Language Processing in Businesses 563 2
NetworkX: Multigraphs 740 2
Tracking User Activity with CherryPy 1560 2
CherryPy: Handling Cookies 1063 2
Introduction to NetworkX 670 2
TorchServe - Serving PyTorch Models 1433 2
Fake News Detection Model using Python 795 2
Keeping Home Routers secure while working remotely 535 2
Email Slicer using Python 3095 2
NetworkX: Creating a Graph 1163 2
Best Mathematics Courses for Machine Learning 592 2
Hello World in CherryPy 836 2
Building dependencies as Meson subprojects 1147 2
Vehicle Detection System 1171 2
NetworkX: Examining and Removing Graph Elements 667 2
Handling URLs with CherryPy 592 2
PEP 8 - Guide to Beautiful Python Code 811 2
NetworkX: Drawing Graphs 688 2
Mad Libs Game using Python 715 2
Hosting Cherry applications 667 2
Top 5 Free Online IDEs of 2020 945 2
pytest: Introduction 570 2
Preventing Pwned and Reused Passwords 617 2
Contact Book using Python 2197 2
Introduction to CherryPy 595 2
nose: Introduction 542 2
Text-based Adventure Game using Python 3264 2
NetworkX: Adding Attributes 2761 2
NetworkX: Directed Graphs 1104 2
Dice Simulator using Python 621 2
Decorating CherryPy applications using CSS 955 2

Comments