2.7版本的一个简单的框架
from wsgiref.simple_server import make_server def new():
return 'new'
def index():
return 'index' urls={'/new':new,
'/index':index
} def Runserver(environ,start_response):
start_response('200 OK',[('Content-Type','text/html')])
url=environ['PATH_INFO']
if url in urls.keys():
ret=urls[url]()
else:
ret=''
return ret if __name__=="__main__":
httpd=make_server('',8000,Runserver)
httpd.serve_forever()
一个简单的tornado脚本
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import tornado.ioloop
import tornado.web is_login=False class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.render('index.html') class LoginHandler(tornado.web.RequestHandler):
def get(self, *args, **kwargs):
self.render('login.html',status='') def post(self, *args, **kwargs):
username = self.get_argument('username',None)
password = self.get_argument('password',None)
if username == 'dalong' and password == "":
self.set_cookie('auth', '')
self.redirect('/manager')
else:
self.render('login.html',status='登陆失败')
class ManagerHandler(tornado.web.RequestHandler):
def get(self,*args, **kwargs):
co=self.get_cookie('auth')
print(co)
if co=='':
self.render('manager.html')
else:
self.redirect('/login') class LogoutHandler(tornado.web.RequestHandler):
def get(self,*args, **kwargs):
self.set_cookie('auth','')
self.redirect('/login') settings = {
'template_path': 'views', # 模板路径的配置
} # 路由映射,路由系统
application = tornado.web.Application([
# (r"/index", MainHandler),
(r"/index", IndexHandler),
(r"/login", LoginHandler),
(r"/manager", ManagerHandler),
(r"/logout", LogoutHandler),
], **settings) if __name__ == "__main__":
# socket运行起来
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()