Table of contents
1.
Introduction🌼
2.
Configure🤖
3.
Architecture🏗️
4.
Global Config🌐
5.
Application Config🖥️
6.
Per Application Configuration🖥️
7.
Additional application settings🖥️
8.
Frequently Asked Questions
8.1.
What exactly does CherryPy expose?
8.2.
Is CherryPy free and open source?
8.3.
Is CherryPy a web server?
8.4.
What exactly is the CherryPy framework?
8.5.
What exactly is Falcon API?
9.
Conclusion
Last Updated: Mar 27, 2024
Medium

Configuring and application settings in cherryPy

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

Introduction🌼

CherryPy is a Python web framework that provides a user-friendly interface to the HTTP protocol for Python developers. It's also known as a web application library.

introductory image

CherryPy models and binds the HTTP protocol into an API using Python's strengths as a dynamic language. It is one of the oldest Python web frameworks, with a clean interface and a dependable platform.

In late June 2002, Remi Delon released the first version of CherryPy. This was the foundation for a successful Python web library. Remi is a French hacker who believes Python is one of the best options for web application development.

In this article, we will cover everything you need to know about Configuring and application settings in cherryPy

Configure🤖

While Dictionaries are used to implement a configuration in CherryPy. Keys are strings that identify the mapped value; values can be any type.

configuration image

Configuration (files or dictionaries) is used in CherryPy 3 to set attributes directly on the engine, server, request, response, and log objects. As a result, the best way to learn about everything in the config file is to simply import those objects and see what help(obj) tells you.

Architecture🏗️

The first thing you should know about CherryPy 3 configuration is that it separates global and application configurations. 

architecture image

If you are deploying multiple applications at the same site (which is becoming more common as Python web apps decentralize), you should also separate the configurations. There is only one "global configuration," but each app has its own "app configuration."

CherryPy Requests are part of an Application that runs in a global context, and configuration data can be applied to any of those three scopes. Let's take a look at each of those scopes individually.

Global Config🌐

For Configuring and application settings in cherryPy, Global configuration entries are stored in cherrypy.config and apply everywhere. 

global config image

This flat dict only stores global configuration data, i.e. "site-wide" configuration entries that affect all mounted applications.

The cherrypy.config dict stores global configuration, which you can update by calling cherrypy.config.update (conf). The conf argument can be a filename, an open file, or a dictionary of configuration entries. Here's an example of how to pass a dict argument:

cherrypy.config.update({'server.socket_host': '73.61.124.52', 'server.socket_port': 60, })


In this example of Configuring and application settings in cherryPy, the server.socket host option specifies which network interface CherryPy will listen on. The server.socket port option specifies the TCP port to use for listening.

Application Config🖥️

For Configuring and application settings in cherryPy, Application entries apply to a single mounted application and are stored as app.config on each Application object. 

application image

This is a two-level dict, with each top-level key being a path, or "relative URL" (for example, "/" or "/my/page"), and each value being a dict of configuration entries. The URLs are relative to the Application's script name (mount point). All of this information is generally provided to you in the call to “tree.mount(root(), script name='/path/to', config=conf)”, but you can also use app.merge (conf). The conf argument can be a filename, an open file, or a dictionary of configuration entries.

Example of a configuration file in Configuring and application settings in cherryPy:

tools.trailing_slash.on = False 

request.dispatch: cherrypy.dispatch.MethodDispatcher()

Alternatively, in Python code:

config = {'/':
    {
        'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
        'tools.trailing_slash.on': False,
    }
}

cherrypy.tree.mount(Root(), config=config)


CherryPy only employs sections that begin with "/". (except [global], see below). That is, you can add your own configuration entries to a CherryPy config file by giving them a section name that does not begin with "/." For example, you could include the following database entries while Configuring and application settings in cherryPy:


[global]
server.socket_host: "1.1.1.1"

[Databases]
driver: "postgres"
host: "localhost"
port: 5454

[/path]
response.timeout: 5000


Then, in your application code, use cherrypy.request.app.config['Databases'] to read these values during request time. You will be needed to pass a reference to your Application around for code that isn't part of the request process.

Per Application Configuration🖥️

When you connect your application to the server, pass in a dictionary or a file to configure it.

cherrypy.quickstart (myapp, '/', {'/': {'tools.gzip.on': True}})

 

or via a file (for example, app.conf):

[/]
tools.gzip.on: True
cherrypy.quickstart(myapp, '/', "app.conf")

 

Although most of your configuration can be defined globally, it is sometimes more convenient to define them where they are used in the code.

class Root(object):
    @cherrypy.expose
    @cherrypy.tools.gzip()
    def index(self):
        return "Hey, Ninja!"

 

A variation on the preceding:

class Root(object):
@cherrypy.expose
def index(self):
return "hello world!"
index._cp_config = {'tools.gzip.on': True}

 

Additional application settings🖥️

You can add non-request URL-specific settings and retrieve them from your page handler as follows:

[/]
tools.gzip.on: True

[googleapi]
key = "..."
appid = "..."

 

class Root(object):
	@cherrypy.expose
	def index(self):
		google_appid = cherrypy.request.app.config['googleapi']['appid']
		return "Hey, Ninja!"


cherrypy.quickstart(Root(), '/', "app.conf")

 

Frequently Asked Questions

What exactly does CherryPy expose?

In a nutshell, once CherryPy has been discovered and is called an exposed method, then it is up to you, the person who writes it, the developer, to provide the tools needed to implement the logic of your application. CherryPy believes that you, the developer, are the most knowledgeable.

Is CherryPy free and open source?

CherryPy is an open-source project, so contributions are welcome. If you're interested, you can fork CherryPy on GitHub and submit a pull request with your changes.

Is CherryPy a web server?

In Brief: CherryPy WSGI Web Server. It is a modular component that can serve any Python WSGI web application.

What exactly is the CherryPy framework?

CherryPy is an object-oriented web application framework that is written in Python. It is intended for the rapid development of web applications by wrapping the HTTP protocol, but it remains at a low level and does not provide much more than RFC 7231 defines.

What exactly is Falcon API?

Falcon is a lightweight Python web API framework for creating robust app backends and microservices. 

Conclusion

This article covers everything you need to know about Configuring and application settings in cherryPy. Here are more articles for rescue.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Happy Learning!

closure image

Live masterclass