Write a tornado combat with the simplest and most violent websites

Reason, Flask used to the feel or are not familiar with Djongo, first look at the Tornado (funny), remember that this is one of the most simple web page the most violent, no details.

Environmental
Win10 
Python 3.6
PyCharm
Tornado

First, you need a plan
first no matter what the programming, then no matter how we do not officially, at least, must have a plan, and my plan is relatively simple to:

**开发个人日记**
    -- 新建日记
        --保存在数据库
            --信息:
                    1.日期
                    2.天气
                    3.心情
                    4.内容
                    --扩展//未开发
                    5.信纸
                    6.类别
                    7.bgm
    -- 查询日记
        --查询
            --时间
            --心情
            --天气


So now we are down to develop the outline how to achieve it?

[Input]: If i need Python基础 and web前端基础 and 服务器编程基础?
[Output]: Yes…
[Output]: But,it is so f**king EZ.

From the start HelloWorld
Hello World, how simple operation:
open our Pycharm, create a new project, we call him HelloWorld (because of violence, the project name is not changed)
to create a new app.py

Then we need to import our tornado library, but we do not need anything else for the time being this:

from tornado import web,ioloop,httpserver


This time we are not helpless

Do nothing [yī chóu mò zhǎn]
chips: Here, trick; development: cast. Also play a little trick not, I could not think that way. Raise: planning, strategy; exhibition: display. Also play a little trick not, I could not think that way. Raise: planning, strategy; exhibition: display. Also play a little trick not, I could not think that way.
Source Source Source
Qing ⋅ Kong Shangren "Peach Blossom Fan": "Xia Guan Shi Kefa; Nikkei slightly Plains; actually do nothing." Qing Kong Shangren "Peach Blossom Fan": "Xia Guan Shi Kefa; Nikkei slightly Plains; actually do nothing." Qing ⋅ Kong Shangren "peach Blossom fan": "Xia Guan Shi Kefa; Nikkei slightly Plains; actually do nothing."
Examples Examples Examples
1. facing the complicated math problem, that he - I do not know where to start. 1. The face of this path complex math problems, he - I do not know where to start. 1. The face of this path complex math problems, he - I do not know where to start. There are confused I do not know how to study a friend Editor's Choice Python school of learning in learning qun 315 -346- 913 can learn to learn together and progress together! Share free videos

When we do not know why English is the inspiration of the time, what we write is? Web applications, on! That his English it? web Application, then why do not we try, tornado developers will not let us directly know how to use it?

web.Application

Then use our knowledge and hyperlinks Pycharm: Ctrl-click Application, Wow, the New World (I also found the time to learn Kotlin ...):

class Application(ReversibleRouter):

    """A collection of request handlers that make up a web application.

    Instances of this class are callable and can be passed directly to

    HTTPServer to serve the application::

        application = web.Application([

            (r"/", MainPageHandler),

        ])

        http_server = httpserver.HTTPServer(application)

        http_server.listen(8080)

        ioloop.IOLoop.current().start()

    The constructor for this class takes in a list of `~.routing.Rule`

    objects or tuples of values corresponding to the arguments of

    `~.routing.Rule` constructor: ``(matcher, target, [target_kwargs], [name])``,

    the values in square brackets being optional. The default matcher is

    `~.routing.PathMatches`, so ``(regexp, target)`` tuples can also be used

    instead of ``(PathMatches(regexp), target)``.

    A common routing target is a `RequestHandler` subclass, but you can also

    use lists of rules as a target, which create a nested routing configuration::

        application = web.Application([

            (HostMatches("example.com"), [

                (r"/", MainPageHandler),

                (r"/feed", FeedHandler),

            ]),

        ])

    In addition to this you can use nested `~.routing.Router` instances,

    `~.httputil.HTTPMessageDelegate` subclasses and callables as routing targets

    (see `~.routing` module docs for more information).

    When we receive requests, we iterate over the list in order and

    instantiate an instance of the first request class whose regexp

    matches the request path. The request class can be specified as

    either a class object or a (fully-qualified) name.

    A dictionary may be passed as the third element (``target_kwargs``)

    of the tuple, which will be used as keyword arguments to the handler's

    constructor and `~RequestHandler.initialize` method. This pattern

    is used for the `StaticFileHandler` in this example (note that a

    `StaticFileHandler` can be installed automatically with the

    static_path setting described below)::

        application = web.Application([

            (r"/static/(.*)", web.StaticFileHandler, {"path": "/var/www"}),

        ])

    We support virtual hosts with the `add_handlers` method, which takes in

    a host regular expression as the first argument::

        application.add_handlers(r"www\.myhost\.com", [

            (r"/article/([0-9]+)", ArticleHandler),

        ])

    If there's no match for the current request's host, then ``default_host``

    parameter value is matched against host regular expressions.

    You can serve static files by sending the ``static_path`` setting

    as a keyword argument. We will serve those files from the

    ``/static/`` URI (this is configurable with the

    ``static_url_prefix`` setting), and we will serve ``/favicon.ico``

    and ``/robots.txt`` from the same directory.  A custom subclass of

    `StaticFileHandler` can be specified with the

    ``static_handler_class`` setting.

    .. versionchanged:: 4.5

       Integration with the new `tornado.routing` module.

    """

Shit yeah so long, but we will find a template, the template we like to do?

application = web.Application([
            (r"/", MainPageHandler),
        ])
        http_server = httpserver.HTTPServer(application)
        http_server.listen(8080)
        ioloop.IOLoop.current().start()


How to use it?
We need to define a class (MainPageHandler)

class MainPageHandler(web.RequestHandler):
    def get(self, *args, **kwargs):



Why you wrote it? We look at the comments to understand RequestHandler

"""Base class for HTTP request handlers.
    Subclasses must define at least one of the methods defined in the
    "Entry points" section below.
"""
SUPPORTED_METHODS = ("GET", "HEAD", "POST", "DELETE", "PATCH", "PUT",
                         "OPTIONS")



However, we need to get that data, then we should get method. (Web basics)


Then we need to give him to write a Hello, World let him show up, how to write it? Certainly not print, we only need to:

class MainPageHandler(web.RequestHandler):
    def post(self, *args, **kwargs):
    


 

[Input]: 接下来呢?这就完了?
[Input]: 给人写信,怎么可能没有地址,没有邮局呢(前提是距离长的像互联网一般)
[Input]: 所以我们要干啥呢?
[Output]: 找人帮忙:

Processing, routing, reception
First we define the class (MainPageHandler): it is one of our assistants, aides a, is responsible for the content get

class MainPageHandler(web.RequestHandler):
    def get(self, *args, **kwargs):
    


Next we need to address, b by the assistant to run errands, told Address:

application = web.Application([
            (r"/", MainPageHandler),
        ])


 

[Input]: 那我们还需要什么呢?
[Output]: 前台,没有助手前台,怎么发送信(这个前台包括邮筒)

Reception Little Helper c:

def main():
    http_server = httpserver.HTTPServer(application)
    http_server.listen(8080)
    ioloop.IOLoop.current().start()
[Input]: 看起来是完了,但是我们写在哪里呢,信纸呢 ?
[Output]: 当然是我们的前端页面

Violence: In the current directory to create a new HTML page: We called him index.html, write the following html code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>Hello,World</h1>
</body>
</html>

Then please send us something little helper a, to take away the letter:

class MainPageHandler(web.RequestHandler):
    def get(self, *args, **kwargs):
        self.render("index.html")


Next is the assistant stage b, where should tell

application = web.Application([
            (r"/", MainPageHandler),
        ])


Now for our reception, and

def main():
    http_server = httpserver.HTTPServer(application)
    http_server.listen(8080)
    ioloop.IOLoop.current().start()


Come and see our letter:

if __name__ == '__main__':
    main()


Open our server is running app.py
open the browser, go visit [127.0.0.1:8080](http://127.0.0.1:8080/)

 

The above is a simple process of the whole site, if you want to learn more, welcome into the group discussion and small series

Guess you like

Origin blog.csdn.net/weixin_44995023/article/details/91870854