Do you think IIT Guwahati certified course can help you in your career?
No
Introduction💁
Python is a multi-purpose programming language. Its code is mostly emphasized on readability with the usage of a significant amount of indentation.
The cherrypy is a python framework that is used for web application development.
CherryPy
Now that we know what cherryPy is, let us look at a few of its advantages and disadvantages.
Advantages
CherryPy has various built-in plugins.
CherryPy supports various HTTP servers simultaneously.
CherryPy provides built-in tools for profiling and testing.
CherryPy is HTTP/1.1 compliant and supports thread pooled WSGI web server
Disadvantages
CherryPy is a fairly outdated framework. Thus it is very hard to find documentation for it. Users might have to look for hours to find proper documentation for a particular feature.
Tailored Dispatchers
Dispatchers are used to locate the proper page handler for a specific request. The cherrypy framework comes with various dispatchers. These are called tailored dispatchers in cherrypy. Let us look at an example of the tailored dispatchers in cherrypy.
Example✨
# importing all the necessary packages
import random
import string
import cherrypy
from cherrypy._cpdispatch import Dispatcher
# creating a class to generate random hexadecimal
class GenerateHex(object):
@cherrypy.expose
def generate(self, length=10):
# Generating random hexadecimal of length 10
# Joining the hexadecimal to our pre-defined message
return 'A random hexdigit of length 10: '.join(random.sample(string.hexdigits, int(length)))
# creating the dispatcher class to dispatch requests to the live server
class ForceLowerDispatcher(Dispatcher):
def __call__(self, path_info):
return Dispatcher.__call__(self, path_info.lower())
# creating the main function
if __name__ == '__main__':
# Creating the route configurations for the root path.
conf = {
'': {
'request.dispatch': ForceLowerDispatcher(),
}
}
# Starting the live server using the cherryPy framework
cherrypy.quickstart(GenerateHex())
You can also try this code with Online Python Compiler
There often arises the need for the best page handler for the requested resource, which Tools cannot provide. Thus Dispatchers come more in Handy when the page handler is needed to be provided. The Dispatchers are only required when a very specific use case needs to be located. Otherwise, the default page handler, such as Tools, will suffice.
Now let's discuss some frequently asked questions associated with Tailored Dispatchers in cherrypy.
Frequently Asked Questions
Why should one choose cherrypy?
The cherrypy is a very pythonic framework, providing features such as functions, classes, and modularity. Cherrypy also has a very simple learning curve when compared to other Python frameworks such as Django.
What are tools in cherrypy?
Tools are simply objects present in the cherrypy framework that are attached to a hook point. Tools can also be assigned and created manually.
Is the cherrypy framework multithreaded?
Yes, the cherrypy framework is developed in the concepts of multithreading. Every time a user or developer sets or gets a value in cherrypy, it is done using a multithreading environment.
What is the default port in cherrypy?
The default port in cherrypy is localhost:8080, but the users can change it according to their convenience.
Conclusion
This Blog covered all the necessary points about the Tailored Dispatchers in cherrypy. We further discussed an example of Tailored Dispatchers in cherrypy.
Hey Ninjas! Don’t stop here. Check out Coding Ninjas for Python, more unique courses, and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and excellent Machine Learning and Python problems.