(三)Web框架-龙卷风Tornado之快速上手

版权声明:本文为博主原创文章,转载文章须附上文章来源与作者。 https://blog.csdn.net/ChangerJJLee/article/details/77435783

示例

HelloTornado.py

# _*_coding:utf-8_*_

import tornado.ioloop
import tornado.web


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, Tornado")

class Handler_index(tornado.web.RequestHandler):
    def get(self):
        self.render("index.html")

settings = {
    "template_path": "template",
    "static_path": "static"
}

application = tornado.web.Application([
    (r"/hello", MainHandler),
    (r"/index", Handler_index),
], **settings)

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start() 

第一步:执行脚本,监听 8888 端口

第二步:浏览器客户端访问 /index –> http://127.0.0.1:8888/index或者访问 /hello –> http://127.0.0.1:8888/hello

第三步:服务器接受请求,并交由对应的类处理该请求

第四步:类接受到请求之后,根据请求方式(post / get / delete …)的不同调用并执行相应的方法

第五步:方法返回值的字符串内容发送浏览器


index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="static/common.css"/>
</head>
<body>
<h1>Hello template/index.html</h1>
</body>
</html>

common.css

h1 {
    background-color: grey;
}

文件目录
这里写图片描述


源码详解

 # _*_coding:utf-8_*_

import tornado.ioloop
import tornado.web


class MainHandler(tornado.web.RequestHandler):
    # 定义get方法,用以匹配前端的GET访问方式
    def get(self):
        #返回以字符串组成的响应内容
        self.write("Hello, Tornado")

class Handler_index(tornado.web.RequestHandler):
    def get(self):
        #返回模板文件,默认在当前目录下找,也可以放置自定义的template目录下,
        #但是需要模板路径全局配置,那么此时他就会去template目录下去找
        self.render("index.html")

#全局静态文件目录配置
settings = {
    "template_path": "template",#模板路径的配置
    "static_path": "static",#静态文件目录路径配置
}



#如下是一个url的对应关系,就是所谓的路由映射
#url后面的的路径匹配,匹配到了就执行这个路径对应的类
#如下:index路径对应的的雷士MainHandler
#如果请求路径不匹配返回404
application = tornado.web.Application([
    (r"/hello", MainHandler),
    (r"/index", Handler_index),
], **settings)


if __name__ == "__main__":
    # 创建socket对象
    application.listen(8888)
    #ioloop里用了IO多路复用
    tornado.ioloop.IOLoop.instance().start()
展开阅读全文

Tornado! 龙卷风

12-25

DescriptionnnIs this crazy weather the result of mankind's continuous interference in the environment? Or is it simply the normal cycle of climate changes through the ages? No one seems to know for sure, but the fact is that natural phenomena such as tornadoes and hurricanes have been hitting our country with more force and frequence than in past decades. nnOne tornado has just hit Silverado Farm, a cattle and milk producer, and made havoc. The barn roof was torn, several trees were uprooted, the farm truck was overturned... But the worst thing is that the tornado destroyed several sections of the fence that surrounded the property. The fence was very well built, with concrete posts every two meters, and barbed wire enclosing the whole farm perimeter (the perimeter, in meters, is an even number, making the fence perfectly regular). nnNow several posts are broken or missing, and there are gaps in the fence. To prevent the cattle from getting out of the property, the fence must be restored as quickly as possible. Reconstructing the fence to its original form, with concrete posts, will take a long time. In the meantime, the farm owners decided to close the gaps with a temporary fence, made with wooden posts. Wooden posts will be placed in exactly the same spots where missing/broken concrete posts were/are. However, in order to make the temporary reconstruction faster and less expensive, the owners decided to use fewer posts: a wooden post will be used to replace a missing/broken concrete post only if the length of the barbed wired needed to close the distance to the next post (wooden or concrete) exceeds four meters. nnnnnGiven the description of which posts are missing/broken, you must write a program to determine the smallest number of wooden posts needed to close all the gaps in the fence, according to the owners' decision. nInputnnThe input contains several test cases. The first line of a test case contains one integer N indicating the number of original concrete posts in the fence (5 <= N <= 5000). The second line of a test case contains N integers Xi indicating the state of each concrete post after the tornado (0 <= Xi <= 1 for 1 <= i <= N). If Xi = 1 post i is in good condition, if Xi = 0 post i is broken or missing. Note that post N is next to post 1. The end of input is indicated by N = 0.nOutputnnFor each test case in the input your program must produce one line of output, containing an integer indicating the smallest number of wooden posts that are needed to restore the fence, according to the owners' decision.nSample Inputnn10n1 0 0 1 0 0 1 0 1 1n11n1 0 0 1 0 0 0 1 1 0 1n12n0 0 0 0 0 1 1 0 0 0 1 1n0nSample Outputnn2n2n3 问答

没有更多推荐了,返回首页