Home >Backend Development >Python Tutorial >RequestHandler for Python development of Tornado website: access point function
Functions that require subclasses to inherit and define specific behaviors are called entry point functions in RequestHandler. The get() function in the Hello World instance above is a typical access point function.
This method is overridden by subclasses and implements the initialization process of RequestHandler subclass implementation.
You can pass parameters to this function (the parameters come from the definition of configuring URL mapping).
from tornado.web import RequestHandler,Application import tornado.ioloop import tornado.web class ProfileHandler(RequestHandler): def initialize(self,database): self.database=database def get(self): return self.write(self.database) def post(self): pass def make_app(): return Application([ (r"/test",ProfileHandler,dict(database="test.db",)) ]) def main(): app=make_app() app.listen(8888) tornado.ioloop.IOLoop.current().start() if __name__=="__main__": main()
Enter on the browser: http://localhost:8888/test
The page displays:
test.db
prepare() method is used for initialization processing before calling the request processing (get, post, etc.) method, and is usually used for resource initialization operations.
The on_finish() method is used for some cleanup work after the request processing is completed. It is usually used to clean up the memory occupied by the object or close the database connection.
Each HTTP Action is processed separately with a separate function in RequestHandler:
RequestHandler.get( *args,**kwargs)
RequestHandler.post(*args,**kwargs)
RequestHandler.head(*args,* *kwargs)
RequestHandler.delete(*args,**kwargs)
The above is the detailed content of RequestHandler for Python development of Tornado website: access point function. For more information, please follow other related articles on the PHP Chinese website!