Table of contents
1.
Introduction
2.
Global Config
3.
Application Config
4.
Request Config
5.
Frequently Asked Questions
5.1.
What Exactly is CherryPy?
5.2.
What accomplishes CherryPy expose?
5.3.
Is CherryPy multithreaded?
5.4.
Is CherryPy an MVC framework?
5.5.
Is CherryPy open source?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Architecture of Config In CherryPy

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

Introduction

The first thing you should be aware of while configuring CherryPy is that it separates global configuration from the application configuration. You must be careful to separate the configurations if you're deploying multiple applications at the same site, which more and more people are doing as Python web apps tend to decentralize. There is always just one "global config," but each app you deploy has its own "app config."

The configuration information may be applicable to any of the three scopes since CherryPy Requests are a component of an Application, which executes in a global context. Let's examine each of those spheres individually.

Architecture of Config

Global Config

Global configuration settings are kept in cherrypy.config and are applicable everywhere. Only global configuration information, or "site-wide" configuration settings that apply to all mounted applications, is stored in this flat dict.

You update the global configuration by calling cherrypy.config.update because it is kept in the cherrypy.config dict. A filename, an open file, or a dictionary of configuration entries can all be used as the conf argument. A dict argument example is provided below:

cherrypy.config.update({'server.socket_host': '64.72.221.48',
                        'server.socket_port': 80,
                       })

 

The server.socket_host option determines on which network interface CherryPy will listen. The server.socket_port option determines the TCP port on which to listen.

Application Config

Application entries are kept on each Application object as an app.config and are exclusive to a single mounted application. This is a two-level dict where each value is a dict containing configuration items and each top-level key is a path, or "relative URL". The URLs are positioned in relation to the application's script name. The call to the tree.mount(root(), script name='/path/to', config=conf) often contains all of this information, but you can alternatively use app.merge. A filename, an open file, or a dictionary of configuration entries can all be used as the conf argument.

Configuration file example:

[/]
tools.trailing_slash.on = False
request.dispatch: cherrypy.dispatch.MethodDispatcher()

 

or, in python code:

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

 

Only parts that begin with "/" are used by CherryPy (except [global], see below). With a section name that does not begin with "/," you can add your own configuration entries to a CherryPy configuration file. You could, for instance, use the following database entries:

[global]
server.socket_host: "0.0.0.0"

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

[/path]
response.timeout: 6000

 

Then, using cherrypy.request.app.config['Databases'] in your application code, you may access these settings during request time. You must send a reference to your Application around for code that is not part of the request process.

Request Config

There is only one request.config dict per Request object. This dict is initially filled by combining global configuration, application configuration, and any configuration found while looking up the page handler. Only those configuration entries that relate to the provided request are included in this dict.

Learning from this article

Frequently Asked Questions

What Exactly is CherryPy?

CherryPy is a very famous Python framework. Web applications can be constructed or built faster and more reliably with CherryPy. It's also known as a web application library. Because it is based on object-oriented Python programming, it is used for its simplicity, resulting in minor source code in less time.

What accomplishes CherryPy expose?

It is your responsibility as a developer to offer the tools necessary to implement the logic of your application after CherryPy has been discovered and is called an exposed method. CherryPy believes that you, the developer, are the expert.

Is CherryPy multithreaded?

The multithreading idea served as the foundation for CherryPy's design. The multi-threaded environment is used each time a developer obtains or sets a value in the CherryPy namespace.

Is CherryPy an MVC framework?

Full stack applications offer the ability to launch a new application by using a command or file. Consider the MVC framework when creating projects or apps in Python, such as the web2py framework.

Is CherryPy open source?

Since CherryPy is an open-source project, contributions are welcome. If this interests you, you may fork CherryPy here on GitHub and send a pull request with your changes.

Conclusion

In this article, we have learned the architecture of config in cherrypy. I hope you would have gained a better understanding of this topic now!

Refer to our guided paths on Coding Ninjas Studio to learn about Data Structure and Algorithms, Competitive Programming, JavaScript, etc. Enroll in our courses and refer to our mock test available. Have a look at the interview experiences and interview bundle for placement preparations.

Happy Coding!

Thank You
Live masterclass