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.

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?

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 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.

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 Amazon, Google, Microsoft, and more?
Attempt our Online Mock Test Series on Coding Ninjas Studio now!
Happy Coding!