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:

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.






