bottle 是python的轻量级web框架,单一文件, 内置模板引擎, 支持标准wsgi
beaker 是session提供支持, 貌似是最好的选择
twisted 是python网络框架, reactor模式, 效率很高
集成在一起,做一个记录
- from bottle import Bottle, route, run, template, request
-
-
- from twisted.web import server,resource
- from twisted.web.wsgi import WSGIResource
- from twisted.python.threadpool import ThreadPool
- from twisted.internet import reactor
- from twisted.application import service, strports
-
- from beaker.middleware import SessionMiddleware
- '''
- bottle+beaker+twisted
- '''
- def login_required(func):
-
- def check_login(*args,**dict):
-
- currentSession = request.environ.get("beaker.session")
-
- if currentSession != None and "is_login" in currentSession and currentSession["is_login"]==True:
-
- return func(*args,**dict)
-
- return "not login"
-
- return check_login
-
- class TestWebRoot(Bottle):
-
- def __init__(self):
- Bottle.__init__(self)
- self.init_route()
-
- def init_route(self):
- self.route('/',callback=self.doRoot)
-
- def doRoot(self, name='all'):
- currentSession=request.environ["beaker.session"]
- currentSession["is_login"]=True
-
- return 'Hello %s' % name
-
-
-
- class TestWebNode(Bottle):
-
- def __init__(self):
- super(TestWebNode,self).__init__()
- self.init_route()
-
- def init_route(self):
- self.route('/',callback=self.doNode)
-
- @login_required
- def doNode(self, *args,**kwargs):
-
- return 'Hello node path' + str(args) + str(kwargs)
-
- if __name__ == '__main__':
-
- port = 7999
-
- # Create and start a thread pool,
- wsgiThreadPool = ThreadPool()
- wsgiThreadPool.start()
- b = TestWebRoot()
-
- node = TestWebNode()
- b.mount('/node/',node)
-
- session_opts={
- 'session.type':'file',
- 'session.cookie_expires':True,
- 'session.data_dir':'./data',
- 'session.auto':True
- }
- app = SessionMiddleware(b, session_opts)
-
-
- wsgiAppAsResource = WSGIResource(reactor, wsgiThreadPool, app)
- site = server.Site(wsgiAppAsResource)
- reactor.listenTCP(port, site)
- reactor.run()
阅读(768) | 评论(0) | 转发(0) |