Cherrypy Restful interface

Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Cherrypy Restful interface

Rayon
This post has NOT been accepted by the mailing list yet.
This post was updated on .
I am trying to create a cherrypy restful application with Backbone.js.

i can send GET request to the server and load the index page but PUT,  DELETE doesn't work.  

Please see my code below:

root method:

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()

-----------------------------------------------------------------------------------------------------------



SUB APP list:

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




Reply | Threaded
Open this post in threaded view
|

Re: Cherrypy Restful interface

Rayon
This post has NOT been accepted by the mailing list yet.
Hi Guys when i posted this it did not go out to the mailing list so i am adding this reply.