REST describes a form of interaction between a client and a server on a network. REST itself is not useful, but how to design RESTful apis (RESTful Web interfaces).
Now that we’re going to design RESTful apis, let’s take a look at what non-restful apis look like. Tornado
import os
import tornado.web
import tornado.ioloop
class AddHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello , ADD\n")
class DeleteHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello , DELETE\n")
class UpdateHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello , UPDATE\n")
class SelectHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello , SELECT\n")
if __name__ == "__main__":
settings = {
'debug' : True,
'static_path' : os.path.join(os.path.dirname(__file__) , "static") ,
'template_path' : os.path.join(os.path.dirname(__file__) , "template") ,
}
application = tornado.web.Application([
(r"/add" , AddHandler),
(r"/delete" , DeleteHandler),
(r"/update" , UpdateHandler),
(r"/select" , SelectHandler),
] , **settings)
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
Copy the code
As above, it is not RESTful to define 4 routes to implement the function of add, delete, modify and search. The RESTful API looks something like this:
import os
import tornado.web
import tornado.ioloop
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello , SELECT\n")
def post(self):
self.write("hello , ADD\n")
def put(self):
self.write("hello , UPDATE\n")
def delete(self):
self.write("hello , DELETE\n")
if __name__ == "__main__":
settings = {
'debug' : True,
'static_path' : os.path.join(os.path.dirname(__file__) , "static") ,
'template_path' : os.path.join(os.path.dirname(__file__) , "template") ,
}
application = tornado.web.Application([
(r"/" , MainHandler),
] , **settings)
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
Copy the code
Curl curl curl curl curl curl curl curl curl
It just defines one API to do different things with different requests, so it’s RESTful