python_way day22
1.tonardo
一、tonardo:
#!/usr/bin/env python3# Created by han on 2016/10/23import os, syssys.path.append(os.path.dirname(os.path.abspath(__file__)))import tornado.ioloopimport tornado.webUSER_INFO = [] class MainHadler(tornado.web.RequestHandler): def get(self): self.write("hello,word") #相当于django的HTTPResponse class CmdbHander(tornado.web.RequestHandler): #tornado内部使用反射来找到类中对应的方法 def get(self): # self.write("cmdb") self.render("index.html") def post(self): #以post方式请求, user = self.get_argument("user") #获取post请求的信息 pwd = self.get_argument("password") USER_INFO.append({"u": user, "p": pwd}) self.redirect(‘/home‘) #跳转 class HomeHadler(tornado.web.RequestHandler): def get(self): self.render("home.html", user_info_list = USER_INFO) #模板渲染 #源码中的反射:# obj = CmdbHander()# func = getattr(obj, "get")# func() #配置模板路径settings = { "template_path":"template", #自定义页面放置位置 "static_path": "static", #自定义静态文件放置位置,定义好以后在html页面中写上link标签引入静态文件才能生效} application = tornado.web.Application([ (r"/main", MainHadler),], **settings) #只需要在application这里增加setttings这个配置 application.add_handlers("cmdb.old.com",[ (r"/home", HomeHadler),]) application.add_handlers("cmdb.old.com",[ (r"/main", CmdbHander),]) if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.instance().start()
1、从请求头中获取请求方式:
2、html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body> <h1> hello HOME </h1> <ul> <!--模板语言--> {% for item in user_info_list %} <li> {{item[‘u‘]}} </li> <li> {{ item.get("p","default")}} </li> {% end %} <!--注意end结尾,不再是endfor了--> </ul> </body></html>
时间: 2024-10-29 19:07:53