Advertisement
Nicc234

Python back end code for a Tornado webserver

Jun 26th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. import tornado.web
  2. import tornado.ioloop
  3.  
  4. class basicRequestHandler(tornado.web.RequestHandler):
  5.     def get(self):
  6.         self.write("Ya boi Nick made this.")
  7.  
  8. class resourceRequestHandler(tornado.web.RequestHandler):
  9.     def get(self, id):
  10.         self.write("Querying tweet with id " + id)
  11.  
  12. class queryStringRequestHandler(tornado.web.RequestHandler):
  13.     def get(self):
  14.         n = int(self.get_argument("n"))
  15.         r = "odd" if n % 2 else "even"
  16.        
  17.         self.write("the number " + str(n) + " is " + r)
  18.  
  19. class staticRequestHandler(tornado.web.RequestHandler):
  20.     def get(self):
  21.         self.render("index.html")
  22.  
  23. if __name__ == "__main__":
  24.     app = tornado.web.Application([
  25.         (r"/", basicRequestHandler),
  26.         (r"/blog", staticRequestHandler),
  27.         (r"/isEven", queryStringRequestHandler),
  28.         (r"/tweet/([0-9]+)", resourceRequestHandler)
  29.     ])
  30.  
  31.     app.listen(8881)
  32.     print("I'm listening on port 8881")
  33.     tornado.ioloop.IOLoop.current().start()
  34.  
  35.  
  36. #localhost:/
  37. #localhost:/8881/blog
  38. #localhostL:8881isEven?n= number
  39. #localhost:8881/tweet/ id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement