Table of contents
1.
Introduction🤓
2.
Features of CherryPy📱
2.1.
Simplicity😊
2.2.
Open-sourced framework🖼️
2.3.
Power⚡
2.4.
Community Help👥
2.5.
Deployment🧑‍💻
3.
Static content serving🧑‍🔧
3.1.
Serving a single file using CherryPy📑
3.2.
Serving a whole directory using CherryPy🗃️
3.3.
Specifying an index file using CherryPy🗂️
3.4.
Allow files downloading⬇️
4.
Getting Started🤩✨
5.
Frequently Asked Questions
5.1.
Is CherryPy paid service?
5.2.
What is the CherryPy configuration?
5.3.
What are CherryPy Web services?
5.4.
Does CherryPy have its own web server?
5.5.
Is CherryPy an MVC framework?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Static content serving in CherryPy

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

Introduction🤓

Hey Ninjas! Excited to learn something new today? Then let's get started with today’s topic, CherryPy🍒

CherryPy is an object-oriented web application framework built using the Python programming language. It permits developers to build web applications the same way they would be made using any other object-oriented Python program. This leads to smaller source code developed in less time.

CherryPy uses Python's main strength as a dynamic language to model, build and bind HTTP protocol into an API. It is one of the oldest Python web frameworks, which provides a clean interface and reliable platform.

cherryPy

CherryPy is more than a decade old, and it has consistently proven to be fast and reliable. It is currently being used in production by many sites, from the simplest to the most demanding. It is designed to rapidly develop web applications by wrapping the HTTP protocol but stays at a low level.

CherryPy can act as a web server or be launched via any WSGI (Web Server Gateway Interface) environment. The framework can be extended with filters, which are called at defined points in the request/response processing. CherryPy does not deal with tasks like backend access or templating for output rendering.

Features of CherryPy📱

Here are a few features of CherryPy that display its strength💪

Simplicity😊

Developing a project using CherryPy is a super simple task with a few lines of code written as per the accord and indentations of the Python language.

CherryPy is very modular in that the primary components are well managed with correct logic concepts. Parent classes can be expanded to child classes.

Open-sourced framework🖼️

CherryPy is licensed under the open-source BSD license, which means this framework can be used commercially at ZERO cost.

Power⚡

CherryPy supports all the features of Python. It also provides plugins and tools which are powerful extension points needed to develop world-class applications.

Community Help👥

It has a passionate community that provides all the necessary support to questions. The community gives complete assistance to developers from the beginner level to the master.

Deployment🧑‍💻

CherryPy provides cost-effective ways of deploying applications. It includes a ready-for-production HTTP server to host the applications. CherryPy can also be deployed on a WSGI-compliant gateway.

Static content serving🧑‍🔧

Static content can be any file that is stored on a server and is it is delivered to users as the same file every time. Images and HTML files are examples of this kind of content.

CherryPy can serve static content such as images, javascript and CSS resources, etc.

CherryPy uses the mime-types module to find the best content type that is required to serve a resource. If the choice is not valid, the user can set a few more media types as shown below:

import mimetypes   #importing mimetypes
mimetypes.types_map['.csv'] = 'textfile/csv'   #types_map attribute is used for dictionary mapping filename extensions to MIME types.
You can also try this code with Online Python Compiler
Run Code

Serving a single file using CherryPy📑

You can serve a single file as given below:

[/style.css]
tools.staticfile.on = True
tools.staticfile.filename = "/home/site/style.css"
You can also try this code with Online Python Compiler
Run Code


CherryPy automatically responds to URLs such as this one http://hostname/styles.css.

Serving a whole directory using CherryPy🗃️

Serving directory is very similar to serving a single file:

[/static]
tools.staticdir.on = True
tools.staticdir.dir = "/home/site/static"
You can also try this code with Online Python Compiler
Run Code


Considering you have a file at static/js/my.js, CherryPy will automatically respond to URLs such as http://hostname/static/js/my.js.

CherryPy requires the absolute path of the files or directories it serves. Suppose there are several static sections to be configured but are located in the same root directory. In that case, the following shortcut can be used:

[/]
tools.staticdir.root = "/home/site"

[/static]
tools.staticdir.on = True
tools.staticdir.dir = "static"
You can also try this code with Online Python Compiler
Run Code

Specifying an index file using CherryPy🗂️

By default, CherryPy responds to the root of a static directory with a 404 error denoting the path '/' was not found. To fix an index file, you can use the following:

[/static]
tools.staticdir.on = True
tools.staticdir.dir = "/home/site/static"
tools.staticdir.index = "index.html"
You can also try this code with Online Python Compiler
Run Code


Considering you have a file at static/index.html, CherryPy automatically responds to URLs such as http://hostname/static/ by returning its contents.

Allow files downloading⬇️

Using "application/x-download" response content type, you can tell a browser that a resource should be downloaded onto the user's machine rather than displayed.

You could, for example, write a page handler as follows:

from  cherrypy.lib.static import serve_file

@cherrypy.expose
def download(self, filepath):
    return serve_file(filepath, "application/x-download", "attachment")
You can also try this code with Online Python Compiler
Run Code


Assuming the file path is a valid path on the system, the response would be viewed as downloadable content by the browser.

Getting Started🤩✨

All new to CherryPy? Don't worry! Here are a few of our resources that will aid you in getting started.

What is CherryPy🔎

Why CherryPy🤔❓

Environmental setup for CherryPy⚒️

Uploading file and reading it in CherryPy python.⬆️📈

Authentication in CherryPy🔐

Frequently Asked Questions

Is CherryPy paid service?

CherryPy is a completely free object-oriented web framework that can be installed and run on your system without any charges. 

What is the CherryPy configuration?

CherryPy framework has its own configuration system allowing the users to parameterize the HTTP server. The configuration settings are either saved as a Python dictionary or just as a textual context record. 

What are CherryPy Web services?

CherryPy web services are a set of web-based additives that aid in changing data between systems or applications. 

Does CherryPy have its own web server?

Yes! CherryPy comes with its own HTTP web server, which makes it self-contained and enables users to run its utility within minutes of installation. 

Is CherryPy an MVC framework?

Yes! CherryPy offers the ability to launch a new application by using just a command or file as it supports Model-View-Controller architectural pattern.

Conclusion

This article provides a brief overview of static content serving using CherryPy. Now that you’re familiar with CherryPy, learn how to configure application settingsArchitecture of Config In CherryPyNamespaces in CherryPy, and perform arithmetic in CherryPy python, or Start with the basics of Python.

Happy learning!

thank_you
Live masterclass