Table of contents
1.
Introduction
2.
Different URLs lead to different functions in cherrypy
3.
URLs with parameters
4.
Frequently Asked Questions
4.1.
How is CherryPy useful in project management?
4.2.
What does CherryPy expose do?
4.3.
What is CherryPy used for?
5.
Conclusion
Last Updated: Aug 13, 2025

Tutorial 2: Different URLs lead to different functions 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 URLs that lead to different methods in CherryPy, along with the details of dynamically deploying the URLs through a program.

Without further ado, let's get started.

Different URLs lead to different functions in cherrypy

Naturally, several URLs will be handled by your applications. Let's look at the code snippet which produces a random string:

Code:

import random
import string
 
import cherrypy
 
 
class StringGenerator(object):
    @cherrypy.expose
    def index(self):
        return "Hey Ninja!!"
 
    @cherrypy.expose
    def generate(self):
        return ''.join(random.sample(string.hexdigits, 20))
 
 
if __name__ == '__main__':
    cherrypy.quickstart(StringGenerator())

 

Enter http://localhost:8080/generate into your browser now, and a random string will appear.

Output:

Output

Explanation:

Let's break down what's going on for a moment. You entered the following URL into your browser: http://localhost:8080/generate

There are several sections in this URL:

  • The URL begins with http://, which generally denotes that it is an HTTP protocol URL.
     
  • The address of the server is localhost:8080. It consists of a port and a hostname.
     
  • The path portion of the URL is /generate. In order to find an exposed function or method to react, CherryPy uses this.
     
  • Here, CherryPy handles / with the index() method and /generate with the generate() function.
     

Let's deploy the above program dynamically.

URLs with parameters

We learned how to build a program that could produce random strings. Let's say that you want to dynamically indicate the length of that string.

Code:

import random
import string
import cherrypy

class StringGenerator(object):
    @cherrypy.expose
    def index(self):
        return "Hello world!"

    @cherrypy.expose
    def generate(self, length=18):
        return ''.join(random.sample(string.hexdigits, int(length)))

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

 

Visit http://localhost:8080/generate?length=20 right away, and your browser will show you a created string that is 20 characters long. Take note of how we continue to support URLs like http://localhost:8080/generate by taking advantage of Python's default argument values.

Output:

Output

Explanation:

The URL portion that comes after the question mark (?) is known as the query string. Traditionally, a set of (key, value) pairs are passed through the query string to contextualise the URL. These pairings have the key=value format. There is a & character between each pair. Take note of the fact that we must convert the provided length value to an integer. In fact, values are delivered as strings from the client to our server.
The query-keys strings are mapped to the exposed function arguments in a manner similar to how CherryPy binds URL path segments to exposed functions.

Frequently Asked Questions

How is CherryPy useful in project management?

CherryPy offers CRUD (Create, Retrieve, Update, and Delete) functions for apps and aids in project management utilising the user's browser from any location.

What does CherryPy expose do?

As a developer, it is up to you to offer the resources necessary to implement the logic of your application at the time the expose method is invoked. According to CherryPy, you, the developer, are the expert.

What is CherryPy used for?

CherryPy wraps the HTTP protocol and is intended to speed up the creation of web applications.

Conclusion

In this article, we have extensively discussed the details of URLs that lead to different methods in CherryPy, along with the details of deploying the URLs dynamically through a program.

We hope that this blog has helped you enhance your knowledge regarding different URLs that lead to different functions 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