Python 托管静态文件和应用程序
我正在努力让CherryPy同时托管静态文件和RESTAPI。我希望API在Python 托管静态文件和应用程序,python,cherrypy,Python,Cherrypy,我正在努力让CherryPy同时托管静态文件和RESTAPI。我希望API在http://127.0.0.1:8080/api,以及http://127.0.0.1:8080/。当用户刚刚获取/apiURL时,我希望它提供一个静态HTML,并附带一个如何使用api的手册。/URL上的静态html是HTML5应用程序;RESTAPI的基本实现 我现在拥有的是: cherrypy.config.update({"tools.staticdir.on": True }) cherrypy.config
http://127.0.0.1:8080/api
,以及http://127.0.0.1:8080/
。当用户刚刚获取/api
URL时,我希望它提供一个静态HTML,并附带一个如何使用api的手册。/
URL上的静态html是HTML5应用程序;RESTAPI的基本实现
我现在拥有的是:
cherrypy.config.update({"tools.staticdir.on": True })
cherrypy.config.update({"tools.staticdir.dir": "/home/bart/html" })
#cherrypy.config.update({"tools.staticdir.index": "index.html"})
if __name__ == '__main__':
cherrypy.tree.mount(myApp(), "/api")
cherrypy.engine.start()
cherrypy.engine.block()
它几乎就在那里,除了当我http://127.0.0.1:8080/api
,其作用与上的相同。myApp()
类的索引如下所示:
@cherrypy.expose
def index(self):
# TODO: place API docs here
if cherrypy.request.method == "GET":
return "REST API v{v}".format(v=VERSION)
else:
return result(405)
我通过为一个空的应用程序创建一个单独的配置来实现它:
cherrypy.tree.mount(myApp(), "/api")
cherrypy.tree.mount(None, "/", {'/':
{"tools.staticdir.on": True ,
"tools.staticdir.dir": "/home/bart/html" ,
"tools.staticdir.index": "index.html"
} } )
cherrypy.engine.start()
cherrypy.engine.block()
看看这个,我认为您的定义需要另一个页面处理程序来处理/