URLs with Parameters in CherryPy

URLs with Parameters in CherryPy

Python: CherryPy


Suppose, we have an application which performs mathematical operations. So, we need some way to provide operands to those operations and one way to do this is to give parameters in the URL and CherryPy will handle those parameters as arguments for the method being called.

We will write a simple application where '<application-name>/' opens the main page in which the description of mathematical operations in our application is provided,  '<application-name>/square' calls the square method and gives square of num and '<application-name>/power' calls the power method and gives num raised to the pow as answer.

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

import cherrypy

class application:
def index(self):
return """Method 'square' will take one parameter 'num' and give square of num.<br><br>
Method 'power' will take two parameters 'num' and 'pow' and give num raised to the power pow as answer."""

def square(self, num=2):
return "The value of {} square is {}".format(num, int(num)**2)

def power(self, num=2, pow=2):
return "The value of {} to the power {} is {}".format(num, pow, int(num)**int(pow))

if __name__ == '__main__':

Here we can see the methods and the corresponding return values. All parameters are also given a default value in case parameters are provided explicitly.

Notice that here we are first converting the parameters to integers using the int() function. This is because the parameter values sent from the client to our server are strings.

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

python3 url_param.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/square without any parameter in the address bar the output will be

And if we enter http://localhost:8080/square?num=11 with one parameter in the address bar the output will be

Similarly, we can also call our power method with no parameters, one parameter or two parameters. To call the power method with two parameters enter http://localhost:8080/power?num=11&pow=4 in address bar we get output as

In a URL the section after ? is called a query-string. Traditionally, the query-string is used to contextualize the URL by passing a set of (key, value) pairs. The format for those pairs is key=value. Each pair being separated by a & character.

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 510 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 798 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 1396 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 623 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 546 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