首页 > 代码库 > cherrypy入门
cherrypy入门
主要是一个简单的cherrypy hello例子
import cherrypyfrom jinja2 import Environment, FileSystemLoaderimport 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入门
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。