使用HTTP方法调度程序启动CherryPy应用程序

时间:2015-03-29 20:37:24

标签: python rest url-routing cherrypy

我是新手,我正在尝试使用方法调度程序启动一个简单的应用程序。我一直在尝试使用这个网站了解cherrypy配置:https://cherrypy.readthedocs.org/en/3.2.6/concepts/config.html,但我仍然不明白我做错了什么。当我启动应用程序,并转到127.0.0.1:8080时,我收到错误消息:路径' /'没找到。这是我用来启动应用程序的python文件:

import cherrypy
import re
import json
import requests

class root(object):

    def GET(self):
        return "<html> <p> Hello </p> </html>"



if __name__ == '__main__':

    conf = {'server.socket_host': '127.0.0.1', 
            'server.socket_port': 8080}
    cherrypy.config.update(conf)

    cherrypy.tree.mount(root(), '/', {
        '/': {
            'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
            'tools.trailing_slash.on': False,
        }
    })
    cherrypy.engine.start()
    cherrypy.engine.block()

我正在尝试设置此根应用程序,以便我可以使用_cp_dispatch函数根据给定的路径调度应用程序。这是最好的方式吗?

1 个答案:

答案 0 :(得分:1)

您必须公开定义属性的对象&#34;公开&#34;:

import cherrypy
import re
import json
import requests

class root(object):
    exposed = True

    def GET(self):
        return "<html> <p> Hello </p> </html>"



if __name__ == '__main__':

    conf = {'server.socket_host': '127.0.0.1',
            'server.socket_port': 8080}
    cherrypy.config.update(conf)

    cherrypy.tree.mount(root(), '/', {
        '/': {
            'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
            'tools.trailing_slash.on': False,
        }
    })
    cherrypy.engine.start()
    cherrypy.engine.block()
相关问题