如何将cherrypy用作静态文件的Web服务器? [英] How to use cherrypy as a web server for static files?

查看:46
本文介绍了如何将cherrypy用作静态文件的Web服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将CherryPy用作将在某些文件夹中显示 .html 文件的Web服务器吗?所有CherryPy介绍性文档都声明内容是动态生成的:

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())

使用 index.html 代替HelloWorld.index()方法是否容易?

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

推荐答案

此简单代码将为当前目录中的文件提供服务.

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()

这篇关于如何将cherrypy用作静态文件的Web服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆