码迷,mamicode.com
首页 > 其他好文 > 详细

cherrypy入门

时间:2014-12-29 13:29:33      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

主要是一个简单的cherrypy hello例子

 

import cherrypy
from jinja2 import Environment, FileSystemLoader
import os
 
 
def create_server():
    euterpectl = EuterpeRoot()
 
    route = cherrypy.dispatch.RoutesDispatcher()
    route.mapper.explicit = False
 
    route.connect(index, /, controller=euterpectl, action=index)
    route.connect(say, /say/:word, controller=euterpectl, action=say)
    return euterpectl, route
 
 
class EuterpeRoot:
 
    def __init__(self):
        self.env = Environment(loader=FileSystemLoader(template))
 
    def render(self, template_name, *args, **kwargs):
        template = self.env.get_template(template_name)
        return template.render(*args, **kwargs)
 
    def index(self):
        return self.render("index.html", salutation=Hello, target=World)
 
    def say(self, word):
        return "say %s" % (word)
 
 
if __name__ == __main__:
    server, route = create_server()
    static_dir = os.path.join(os.path.dirname(__file__), "static")
    conf = {
        # ‘server.socket_port‘: 8088,
        engine.autoreload.on: True,
        log.screen: True,
        checker.on: True,
        # ‘restart_enable‘: True,
 
        # add css, image and js static files
        /style: {tools.staticdir.on: True,
                   tools.staticdir.dir: os.path.join(static_dir, style)
        },
 
        /js: {tools.staticdir.on: True,
                tools.staticdir.dir: os.path.join(static_dir, js)
        },
 
        /img: {tools.staticdir.on: True,
                tools.staticdir.dir: os.path.join(static_dir, img)
        },
    }
    config = {/: {request.dispatch: route},
              global: conf
    }
    cherrypy.config.update(config)
    cherrypy.tree.mount(server, /, config=config)
    cherrypy.engine.start()
    cherrypy.engine.block()

 

cherrypy入门

标签:

原文地址:http://www.cnblogs.com/nagi/p/4191447.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!