0

嗨,我正在尝试创建一个可以与 Backbone.js 一起使用的cherrypy脚本

我希望能够从主应用程序加载索引页面,然后从子应用程序运行 restful 操作。当我请求 GET 从数据库中提取列表时,我可以加载索引页面并获得响应,但 PUT 和 DELETE 方法出现 404 错误。

请在下面查看我的代码:

主要应用:

  import cherrypy
  import os.path 
  import json 
  import mysql.connector
  import list



class Root: 

    @cherrypy.expose
    def index(self):
        index  = open(r"html/test.html","r")
        return index


current_dir = os.path.dirname(os.path.abspath(__file__))


    cherrypy.config.update({'server.socket_host':'0.0.0.0','server.socket_port':8084,'tools.sessions.on': True,'tools.sessions.storage_type':'ram','tools.sessions.timeout':120,
'log.error_file':'site_error.log','log.access_file':'site.log','log.screen': True,'response.timeout':100
                  })

d = cherrypy.dispatch.MethodDispatcher() 
conf = {'/': {'request.dispatch': d}} 

main_confg = {'/css':{'tools.staticdir.on': True,'tools.staticdir.dir': os.path.join(current_dir, 'css')},
    '/html':{'tools.staticdir.on': True,'tools.staticdir.dir': os.path.join(current_dir, 'html')},
    '/images':{'tools.staticdir.on': True,'tools.staticdir.dir': os.path.join(current_dir, 'image')},    
    '/script':{'tools.staticdir.on': True,'tools.staticdir.dir': os.path.join(current_dir, 'script')},
    }

if __name__ == '__main__': 

    cherrypy.tree.mount(list.List(),'/list',conf)
    cherrypy.tree.mount(Root(),'/',main_confg)
    cherrypy.engine.start()
    cherrypy.engine.block()

正在映射到主应用程序的 Rest 应用程序:

import cherrypy
import os.path
import json 
import mysql.connector


class List:
    exposed = True

    def dispatch(self, vpath):
        if vpath:
            id = vpath.pop(0)
            return MyData(id)
        return False    

    def GET(self, id=None):
        list = []
        con =  mysql.connector.connect(host="localhost",user='',password="",database='test',buffered=True)
        cur =  con.cursor()
        if id == None: 
            cur.execute('select idtask,create_date,title,completed from task')
        else: 
            cur.execute('select idtask,create_date,title,completed from task where idtask = %s',(id,))
        for x in cur: 
            record = {'idtask':x[0],'create_date':str(x[1]),'title':x[2],'completed':x[3]}
            list.append(record)
        return json.dumps(list)


    def POST(self,**Kwargs):
        return str(Kwargs)


    def PUT(self,**Kwargs):
        return str(Kwargs)

    def DELETE(self,**Kwargs):
        return Kwargs
4

1 回答 1

1

撇开dispatch方法(可能您的意思是_cp_dispatch)不谈,您的代码看起来不错,这是您尝试归档的内容以及一些 curl 命令以测试此应用程序的示例,我认为您的主干调用有问题:

import cherrypy as cp

class ConventionalApp(object):

    @cp.expose
    def index(self):
        return ("Hi I'm completely conventional and I give"
                 " a **** about REST")

class LeObject(object):
    exposed = True

    def GET(self, id=None):
        if id is not None:
            return "Here is your object with id %s" % id
        else:
            return "Imagine the list with all the objects"

    def DELETE(self, id, **kwargs):
        return "%s DELETED" % id

    def PUT(self, **kwargs):
        return "PUTTED"


def main():
    cp.tree.mount(ConventionalApp(), '')
    config = {'/': {'request.dispatch': cp.dispatch.MethodDispatcher()}}
    cp.tree.mount(LeObject(), '/list', config=config)
    cp.engine.start()
    cp.engine.block()

if __name__ == '__main__':
    main()

卷曲命令:

curl localhost:8080
curl localhost:8080/list
curl localhost:8080/list/1
curl  -X DELETE localhost:8080/list/12
curl  -X PUT -H "Content-Length: 0" localhost:8080/list/
于 2013-09-13T23:31:08.220 回答