Chinaunix首页 | 论坛 | 博客
  • 博客访问: 54582
  • 博文数量: 11
  • 博客积分: 185
  • 博客等级: 入伍新兵
  • 技术积分: 135
  • 用 户 组: 普通用户
  • 注册时间: 2013-01-09 11:07
文章分类

全部博文(11)

文章存档

2020年(1)

2013年(10)

我的朋友

分类: Python/Ruby

2013-01-09 11:10:03

bottle 是python的轻量级web框架,单一文件, 内置模板引擎, 支持标准wsgi

beaker 是session提供支持, 貌似是最好的选择

twisted 是python网络框架, reactor模式, 效率很高

集成在一起,做一个记录


 

点击(此处)折叠或打开

  1. from bottle import Bottle, route, run, template, request
  2.    
  3.    
  4. from twisted.web import server,resource
  5. from twisted.web.wsgi import WSGIResource
  6. from twisted.python.threadpool import ThreadPool
  7. from twisted.internet import reactor
  8. from twisted.application import service, strports
  9.    
  10. from beaker.middleware import SessionMiddleware
  11. '''
  12. bottle+beaker+twisted
  13. '''
  14. def login_required(func):
  15.    
  16.     def check_login(*args,**dict):
  17.            
  18.         currentSession = request.environ.get("beaker.session")
  19.            
  20.         if currentSession != None and "is_login" in currentSession and currentSession["is_login"]==True:
  21.    
  22.             return func(*args,**dict)
  23.    
  24.         return "not login"
  25.    
  26.     return check_login
  27.        
  28. class TestWebRoot(Bottle):
  29.        
  30.     def __init__(self):
  31.         Bottle.__init__(self)
  32.         self.init_route()
  33.            
  34.     def init_route(self):
  35.         self.route('/',callback=self.doRoot)
  36.            
  37.     def doRoot(self, name='all'):
  38.         currentSession=request.environ["beaker.session"]
  39.         currentSession["is_login"]=True
  40.            
  41.         return 'Hello %s' % name
  42.    
  43.            
  44.    
  45. class TestWebNode(Bottle):
  46.        
  47.     def __init__(self):
  48.         super(TestWebNode,self).__init__()
  49.         self.init_route()
  50.            
  51.     def init_route(self):
  52.         self.route('/',callback=self.doNode)
  53.            
  54.     @login_required
  55.     def doNode(self, *args,**kwargs):
  56.            
  57.         return 'Hello node path' + str(args) + str(kwargs)
  58.    
  59. if __name__ == '__main__':
  60.        
  61.     port = 7999
  62.        
  63.     # Create and start a thread pool,
  64.     wsgiThreadPool = ThreadPool()
  65.     wsgiThreadPool.start()
  66.     b = TestWebRoot()
  67.        
  68.     node = TestWebNode()
  69.     b.mount('/node/',node)
  70.        
  71.     session_opts={
  72.         'session.type':'file',
  73.         'session.cookie_expires':True,
  74.         'session.data_dir':'./data',
  75.         'session.auto':True
  76.     }
  77.     app = SessionMiddleware(b, session_opts)
  78.        
  79.    
  80.     wsgiAppAsResource = WSGIResource(reactor, wsgiThreadPool, app)
  81.     site = server.Site(wsgiAppAsResource)
  82.     reactor.listenTCP(port, site)
  83.     reactor.run()


 

阅读(768) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:java的内存收缩问题(部分翻译)

给主人留下些什么吧!~~