Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use cherrypy as a web server for static files?

Is it any easy way to use CherryPy as an web server that will display .html files in some folder? All CherryPy introductory documentation states that content is dynamically generated:

import cherrypy
class HelloWorld(object):
    def index(self):
        return "Hello World!"
    index.exposed = True
cherrypy.quickstart(HelloWorld())

Is it any easy way to use index.html instead of HelloWorld.index() method?

like image 421
grigoryvp Avatar asked Apr 17 '09 08:04

grigoryvp


People also ask

Is CherryPy a Web server?

It is designed for rapid development of web applications by wrapping the HTTP protocol but stays at a low level and does not offer much more than what is defined in RFC 7231. CherryPy can be a web server itself or one can launch it via any WSGI compatible environment.

How does CherryPy work?

CherryPy is a web framework of Python which provides a friendly interface to the HTTP protocol for Python developers. It is also called a web application library. CherryPy uses Python's strengths as a dynamic language to model and bind HTTP protocol into an API.

What is static file in web server?

Static content is any content that can be delivered to an end user without having to be generated, modified, or processed. The server delivers the same file to each user, making static content one of the simplest and most efficient content types to transmit over the Internet.

Is CherryPy a MVC framework?

CherryPy is designed on the concept of multithreading. This gives it the benefit of handling multiple tasks at the same time. It also takes a modular approach, following the Model View Controller (MVC) pattern to build web services; therefore, it's fast and developer-friendly.


1 Answers

This simple code will serve files on current directory.

import os
import cherrypy

PATH = os.path.abspath(os.path.dirname(__file__))
class Root(object): pass

cherrypy.tree.mount(Root(), '/', config={
        '/': {
                'tools.staticdir.on': True,
                'tools.staticdir.dir': PATH,
                'tools.staticdir.index': 'index.html',
            },
    })

cherrypy.quickstart()
like image 91
nosklo Avatar answered Sep 21 '22 15:09

nosklo