Table of contents
1.
Introduction 
2.
Multiple HTTP Servers Support
2.1.
Example 1
3.
WSGI support
3.1.
Make Your CherryPy application a WSGI application
3.2.
Host a foreign WSGI application in CherryPy 
3.2.1.
Example 2
3.3.
No need for the WSGI interface?
3.3.1.
Example 3
4.
WebSocket support
5.
Database support
6.
HTML Templating support
7.
Frequently Asked Questions
7.1.
What is CherryPy?
7.2.
Why do we need a database for a web application?
7.3.
What is WSGI?
7.4.
What is a WebSocket?
7.5.
How does CherryPy provide database support?
8.
Conclusion
Last Updated: Mar 27, 2024
Hard

Various Types of Supports in CherryPy

Author Teesha Goyal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

CherryPy is a Python-based, object-oriented web application framework developed in 2002. CherryPy helps in speeding up the process of web application development by wrapping the HTTP protocols. 

This article will discuss the different supports available in CherryPy. We will discuss Multiple HTTP server support, WSGI support, WebSocket support, Database support, and HTML Templating support.

CherryPy

Multiple HTTP Servers Support

Whenever you run an application developed by CherryPy, an HTTP server is started automatically. You can use this server to visit/view the developed application. CherryPy also allows you to create multiple HTTP servers for your application. 
To provide Multiple HTTP Servers support, you just need to import the Server module from cherrypy._cpserver, create a server object and specify server ports. At last, subscribe to your server instance.  

Example 1

from cherrypy._cpserver import Server
server = Server()
server.socket_port = 8090
server.subscribe()
You can also try this code with Online Python Compiler
Run Code

 

Once you have subscribed, you can create any number of server instances for your application. Each instance will follow the CherryPy engine’s life cycle. 

WSGI support

The WSGI (Web Server Gateway Interface) is a calling convention for web servers to forward requests to Python-based web applications or frameworks. 

WSGI dupport

CherryPy supports WSGI in two ways:

🚀 You can host your CherryPy application using another WSGI web server. 

🚀 A foreign WSGI application can be hosted by the CherryPy server. 

Make Your CherryPy application a WSGI application

If you have already developed your web application with CherryPy and wish to host it on the WSGI server, then all you need to do is write two lines of code as shown below:

import cherrypy
WSGI_app = cherrypy.Application(StringGenerator(), '/', config=myconf)
You can also try this code with Online Python Compiler
Run Code

 

After this, you can use your web application via the WSGI_app instance on any WSGI-aware web server. 

Host a foreign WSGI application in CherryPy 

Now imagine the opposite of what we have done above. This time we have an already developed WSGI-aware application and wish to host it in CherryPy. This can be done quickly using the cherrypy.tree.graft facility provided by CherryPy. 

Example 2

def raw_wsgi_app(environ, start_response):
    status = '200 OK'
    response_headers = [('Content-type','text/plain')]
    start_response(status, response_headers)
    return ['Hello Ninja!']

cherrypy.tree.graft(raw_wsgi_app, '/')
You can also try this code with Online Python Compiler
Run Code


To learn more about this, visit How to Deploy Python WSGI Applications Using a CherryPy.

No need for the WSGI interface?

No need for the WSGI interface

If your web application is purely CherryPy, you can bypass the WSGI layer using an HTTP server. Hence, you do not always need a WSGI interface to run your CherryPy web application. Following is an example showing how: 

Example 3

import cherrypy

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

if __name__ == '__main__':
    from cherrypy._cpnative_server import CPHTTPServer
    cherrypy.server.httpserver = CPHTTPServer(cherrypy.server)
    
    cherrypy.quickstart(Root(), '/')
You can also try this code with Online Python Compiler
Run Code

WebSocket support

The WebSocket is a cutting-edge technology that allows a two-way interactive communication session to be established between a user's browser and a server. You can use this to send messages to a server and receive event-driven responses without polling the server for a response.

It is simply a client-server connection that stays open indefinitely. 

A WebSocket is a socket that is initiated by an HTTP upgrade request. After the upgrade, the underlying socket remains open but is no longer used in an HTTP context. Instead, the socket can be used by both connected endpoints to send data to the other end.

WebSockets Support

Websockets support in CherryPy is provided using a library called ws4py. You can use this library and the benefits of WebSockets in your CherryPy application.

Database support

Data is a very important element of an application. Especially if you are dealing with a dynamic website, it may generate a lot of data in a brief period, and managing such a large amount requires a database. A database is required to store the data and to manipulate and retrieve the data as and when required.  

Database support

CherryPy supports standard database interfaces to be integrated into your web application, such as DB-API. Moreover, you can also connect many ORM (object-relational mapping) tools in CherryPy. For example, SQLAlchemy or SQLObject.

HTML Templating support

CherryPy does not include an HTML template, but its architecture makes it simple to add one. Mako and Jinja2 are two famous examples. 
To integrate HTML templating support in your application, you can use plugins and tools. Plugins are entities that interact with the bus by publishing or subscribing to channels, generally both at the same time.
To learn more about plugins, visit Plugins in CherryPy

Frequently Asked Questions

What is CherryPy?

CherryPy is a python-based, object-oriented web application framework developed in 2002. CherryPy helps in speeding up the process of web application development by wrapping the HTTP protocols. 

Why do we need a database for a web application?

Data is the most crucial element of any application. If you are dealing with a dynamic website, it may generate a lot of data in a very short period of time and to manage such a large amount requires a database. 

What is WSGI?

The WSGI (Web Server Gateway Interface) is a calling convention for web servers to forward requests to Python-based web applications or frameworks. We can host our CherryPy application to the WSGI server and also host a foreign WSGI application in CherryPy. 

What is a WebSocket?

The WebSocket is a cutting-edge technology that allows a two-way interactive communication session to be established between a user's browser and a server. You can use this to send messages to a server and receive event-driven responses without polling the server for a response.

How does CherryPy provide database support?

CherryPy supports different standard database interfaces to be integrated into your web application, such as DB-API. Moreover, you can also connect many ORM (object-relational mapping) tools in CherryPy. For example, SQLAlchemy or SQLObject.

Conclusion

This article discussed the different supports available in CherryPy. We discussed Multiple HTTP server support, WSGI support, WebSocket support, Database support, and HTML Templating support.

I hope you would have gained a better understanding of these topics now!

Are you planning to ace the interviews of reputed product-based companies like AmazonGoogleMicrosoft, and more? 

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Happy Coding!

Live masterclass