Home > Backend Development > Python Tutorial > RequestHandler for Python development of Tornado website: access point function

RequestHandler for Python development of Tornado website: access point function

不言
Release: 2018-10-19 17:12:35
forward
2230 people have browsed it
The content of this article is about the RequestHandler: access point function of developing Tornado website in Python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

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.

1. RequestHandler.initialize()

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

Example:
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()
Copy after login

Enter on the browser: http://localhost:8888/test

The page displays:

test.db
Copy after login

2, RequestHandler. prepare(), RequestHandler.on_finish()

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.

3. HTTP Action processing function

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)

  • ##RequestHandler.patch(*args,**kwargs)

  • RequestHandler.put(*args,**kwargs)

  • ##RequestHandler.options(*args,**kwargs)
  • Each processing function is named after the lowercase name of the HTTP Action.

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template