return u'1分钟前' if delta < 3600: return u'%s分钟前' % (delta // 60) if delta < 86400: return u'%s小时前' % (delta // 3600) if delta < 604800: return u'%s天前' % (delta // 86400) dt = datetime.fromtimestamp(t) return u'%s年%s月%s日' % (dt.year, dt.month, dt.day) db.create_engine(**configs.db) wsgi = WSGIApplication(os.path.dirname(os.path.abspath(__file__))) template_engine = Jinja2TemplateEngine( os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')) wsgi.template_engine = template_engine # 把filter添加到jinjia2,filter名称为datetime,filter本身是一个函数对象: template_engine.add_filter('datetime', datetime_filter) import urls wsgi.add_interceptor(urls.user_interceptor) wsgi.add_interceptor(urls.manage_interceptor) wsgi.add_module(urls) if __name__ == '__main__': wsgi.run(10088, host='0.0.0.0') else: application = wsgi.get_wsgi_application()
#!/usr/bin/env python # -*- coding: utf-8 -*- __author__ = 'Michael Liao' ''' A WSGI application entry. ''' import urls from transwarp.web import WSGIApplication # init wsgi app: wsgi = WSGIApplication() wsgi.add_module(urls) if __name__ == '__main__': wsgi.run(8080) else: application = wsgi.get_wsgi_application()
if delta < 3600: return u'%s分钟前' % (delta // 60) if delta < 86400: return u'%s小时前' % (delta // 3600) if delta < 604800: return u'%s天前' % (delta // 86400) dt = datetime.fromtimestamp(t) return u'%s年%s月%s日' % (dt.year, dt.month, dt.day) # init db: db.create_engine(**configs.db) # init wsgi app: wsgi = WSGIApplication(os.path.dirname(os.path.abspath(__file__))) template_engine = Jinja2TemplateEngine( os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')) template_engine.add_filter('datetime', datetime_filter) wsgi.template_engine = template_engine import urls wsgi.add_interceptor(urls.user_interceptor) wsgi.add_interceptor(urls.manage_interceptor) wsgi.add_module(urls) if __name__ == '__main__': wsgi.run(9000, '127.0.0.1') else: logging.info("?????????????????????????????") application = wsgi.get_wsgi_application()
return u"%s分钟前" % (delta // 60) if delta < 86400: return u"%s小时前" % (delta // 3600) if delta < 604800: return u"%s天前" % (delta // 86400) dt = datetime.fromtimestamp(t) return u"%s年%s月%s日" % (dt.year, dt.month, dt.day) # 初始化数据库: db.create_engine(**configs.db) # 创建一个WSGIApplication: wsgi = WSGIApplication(os.path.dirname(os.path.abspath(__file__))) # 初始化jinja2模板引擎: template_engine = Jinja2TemplateEngine(os.path.join(os.path.dirname(os.path.abspath(__file__)), "templates")) template_engine.add_filter("datetime", datetime_filter) wsgi.template_engine = template_engine # 加载带有@get/@post的URL处理函数: import urls wsgi.add_interceptor(urls.user_interceptor) wsgi.add_interceptor(urls.manage_interceptor) wsgi.add_module(urls) # 在9000端口上启动本地测试服务器: if __name__ == "__main__": wsgi.run(9000, host="0.0.0.0")
def __call__(self, path, model): return "<!-- override this method to render template -->" class Jinja2TemplateEngine(TemplateEngine): def __init__(self, templ_dir, **kw): from jinja2 import Environment, FileSystemLoader if not "autoescape" in kw: kw["autoescape"] = True self._env = Environment(loader=FileSystemLoader(templ_dir), **kw) def add_filter(self, name, fn_filter): self._env.filters[name] = fn_filter def __call__(self, path, model): return self._env.get_template(path).render(**model).encode("utf-8") # init wsgi app: wsgi = WSGIApplication(os.path.dirname(os.path.abspath(__file__))) template_engine = Jinja2TemplateEngine(os.path.join(os.path.dirname(os.path.abspath(__file__)), "templates")) wsgi.template_engine = template_engine wsgi.add_module(urls) # 把url中的路径-函数,打包成映射关系,路径:route,route是从原python函数 if __name__ == "__main__": wsgi.run(9000, host="0.0.0.0") # 自己运行,使用内置的web服务器 else: application = wsgi.get_wsgi_application() # 传递给其他模块
A WSGI application entry. ''' import logging; logging.basicConfig(level=logging.INFO) import os from transwarp import db from transwarp.web import WSGIApplication, Jinja2TemplateEngine from config import configs # init db: db.create_engine(**configs.db) # init wsgi app: wsgi = WSGIApplication(os.path.dirname(os.path.abspath(__file__))) template_engine = Jinja2TemplateEngine(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')) wsgi.template_engine = template_engine import urls wsgi.add_interceptor(urls.user_interceptor) wsgi.add_interceptor(urls.manage_interceptor) wsgi.add_module(urls) if __name__ == '__main__': wsgi.run(8000)
if delta < 86400: return u'%s小时前' % (delta // 3600) if delta < 604800: return u'%s天前' % (delta // 86400) dt = datetime.fromtimestamp(t) return u'%s年%s月%s日' % (dt.year, dt.month, dt.day) # 初始化数据库 db.create_engine(**configs.db) # 创建一个WSGIApplication: wsgi = WSGIApplication(os.path.abspath(__file__)) # 初始化jinja2模板引擎 template_engine = Jinja2TemplateEngine( os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')) template_engine.add_filter('datetime', datetime_filter) wsgi.template_engine = template_engine # 加载带有@get/@post 的URL处理函数 import urls wsgi.add_interceptor(urls.user_interceptor) wsgi.add_interceptor(urls.manage_interceptor) wsgi.add_module(urls) # 在9000端口上启动本地测试服务器 if __name__ == '__main__': wsgi.run(9000) else: application = wsgi.get_wsgi_application()
if delta < 3600: return u'%s分钟前' % (delta // 60) if delta < 86400: return u'%s小时前' % (delta // 3600) if delta < 604800: return u'%s天前' % (delta // 86400) dt = datetime.fromtimestamp(t) return u'%s年%s月%s日' % (dt.year, dt.month, dt.day) # init db: db.create_engine(**configs.db) # init wsgi app: wsgi = WSGIApplication(os.path.dirname(os.path.abspath(__file__))) template_engine = Jinja2TemplateEngine( os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')) template_engine.add_filter('datetime', datetime_filter) wsgi.template_engine = template_engine import urls wsgi.add_interceptor(urls.user_interceptor) wsgi.add_interceptor(urls.manage_interceptor) wsgi.add_module(urls) if __name__ == '__main__': wsgi.run(9002, host='0.0.0.0')
if delta < 3600: return u'%s分钟前' % (delta // 60) if delta < 86400: return u'%s小时前' % (delta // 3600) if delta < 604800: return u'%s天前' % (delta // 86400) dt = datetime.fromtimestamp(t) return u'%s年%s月%s日' % (dt.year, dt.month, dt.day) # init db print configs.db db.create_engine(**configs.db) # init wsgi app wsgi = WSGIApplication(os.path.dirname(os.path.abspath(__file__))) template_engine = Jinja2TemplateEngine( os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')) template_engine.add_filter('datetime', datetime_filter) wsgi.template_engine = template_engine import urls wsgi.add_interceptor(urls.user_interceptor) wsgi.add_interceptor(urls.manage_interceptor) wsgi.add_module(urls) if __name__ == '__main__': wsgi.run(9000, host='127.0.0.1') else: application = wsgi.get_wsgi_application()
#!/usr/bin/env python # coding: utf-8 # wsgiapp.py import logging logging.basicConfig(level=logging.INFO) import os from transwarp import db from transwarp.web import WSGIApplication, Jinja2TemplateEngine from config import configs # 初始化数据库: db.create_engine(**configs.db) # 创建一个WSGIApplication: wsgi = WSGIApplication(os.path.dirname(os.path.abspath(__file__))) # 初始化jinja2模板引擎: template_engine = Jinja2TemplateEngine( os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')) wsgi.template_engine = template_engine # 加载带有@get/@post的URL处理函数: import urls wsgi.add_module(urls) # 在9000端口上启动本地测试服务器: if __name__ == '__main__': wsgi.run(9000, '0.0.0.0')
return u'%s小时前' % (delta // 3600) if delta < 604800: return u'%s天前' % (delta // 86400) dt = datetime.fromtimestamp(t) return u'%s年%s月%s日' % (dt.year, dt.month, dt.day) # 初始化数据库: db.createEngine(**configs.db) # 初始化WEB框架: wsgi = WSGIApplication(os.path.dirname(os.path.abspath(__file__))) template_engine = Jinja2TemplateEngine( os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')) template_engine.add_filter('datetime', datetime_filter) wsgi.template_engine = template_engine # 加载urls import urls wsgi.add_interceptor(urls.user_interceptor) wsgi.add_interceptor(urls.manage_interceptor) wsgi.add_module(urls) if __name__ == '__main__': # 启动Web服务 wsgi.run(8081, '10.104.128.190') else: application = wsgi.get_wsgi_application()
class TemplateEngine(object): def __call__(self, path, model): return '<!-- override this method to render template -->' class Jinja2TemplateEngine(TemplateEngine): def __init__(self, templ_dir, **kw): from jinja2 import Environment, FileSystemLoader if not 'autoescape' in kw: kw['autoescape'] = True self._env = Environment(loader=FileSystemLoader(templ_dir), **kw) def add_filter(self, name, fn_filter): self._env.filters[name] = fn_filter def __call__(self, path, model): return self._env.get_template(path).render(**model).encode('utf-8') # init wsgi app: wsgi = WSGIApplication(os.path.dirname(os.path.abspath(__file__))) template_engine = Jinja2TemplateEngine( os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')) wsgi.template_engine = template_engine wsgi.add_module(urls) #把url中的路径-函数,打包成映射关系,路径:route,route是从原python函数 if __name__ == '__main__': wsgi.run(9000, host='0.0.0.0') #自己运行,使用内置的web服务器 else: application = wsgi.get_wsgi_application() #传递给其他模块
if delta < 3600: return u'%s分钟前' % (delta // 60) if delta < 86400: return u'%s小时前' % (delta // 3600) if delta < 604800: return u'%s天前' % (delta // 86400) dt = datetime.fromtimestamp(t) return u'%s年%s月%s日' % (dt.year, dt.month, dt.day) # init db: db.create_engine(**configs.db) # init wsgi app: wsgi = WSGIApplication(os.path.dirname(os.path.abspath(__file__))) template_engine = Jinja2TemplateEngine(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')) template_engine.add_filter('datetime', datetime_filter) wsgi.template_engine = template_engine import urls wsgi.add_interceptor(urls.user_interceptor) wsgi.add_interceptor(urls.manage_interceptor) wsgi.add_module(urls) if __name__ == '__main__': wsgi.run(80, host='0.0.0.0') else: application = wsgi.get_wsgi_application()
if delta < 86400: return u"%s小时前" % (delta // 3600) if delta < 604800: return u"%s天前" % (delta // 86400) dt = datetime.fromtimestamp(t) return u"%s年%s月%s日" % (dt.year, dt.month, dt.day) #初始化数据库 db.create_engine(**configs.db) #创建一个WSGIApplication wsgi = WSGIApplication(os.path.dirname(os.path.abspath(__file__))) #初始化jinja2模板引擎 template_engine = Jinja2TemplateEngine(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')) # 把filter添加到jinja2, filteri名称为DateTime,filter本身是一个函数对象 template_engine.add_filter('datetime', datetime_filter) wsgi.template_engine = template_engine #加载带有@get/@post的URL处理函数 import urls wsgi.add_interceptor(urls.user_interceptor) wsgi.add_interceptor(urls.manage_interceptor) wsgi.add_module(urls) # 在9000窗口上启动本地测试服务器 if __name__ == '__main__': wsgi.run(5000, host='0.0.0.0')
if delta < 3600: return u'%s分钟前' % (delta // 60) if delta < 86400: return u'%s小时前' % (delta // 3600) if delta < 604800: return u'%s天前' % (delta // 86400) dt = datetime.fromtimestamp(t) return u'%s年%s月%s日' % (dt.year, dt.month, dt.day) # init db: db.create_engine(**configs.db) # init wsgi app: wsgi = WSGIApplication(os.path.dirname(os.path.abspath(__file__))) template_engine = Jinja2TemplateEngine(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')) template_engine.add_filter('datetime', datetime_filter) wsgi.template_engine = template_engine import urls wsgi.add_interceptor(urls.user_interceptor) wsgi.add_interceptor(urls.manage_interceptor) wsgi.add_module(urls) if __name__ == '__main__': wsgi.run(9000, host='0.0.0.0') else: application = wsgi.get_wsgi_application()
if delta<60: return u'1 minute ago' if delta<3600: return u'%s minutes ago' % (delta/60) if delta<84600: return u'%s hours ago' % (delta/3600) if delta<604800: return u'%s days ago' % (delta/85600) dt = datetime.fromtimestamp(t) return u'%s year %s month %s day' % (dt.year, dt.month, dt.day) # initialize the DB db.create_engine(**configs['db']) # create the WSGI application wsgi = WSGIApplication(os.path.dirname(os.path.abspath(__file__))) template_engine = Jinja2TemplateEngine(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')) template_engine.add_filter('datetime',datetime_filter) wsgi.template_engine = template_engine # load the process func with @get and @post import urls wsgi.add_module(urls) if __name__ == '__main__': wsgi.run(7000, host='0.0.0.0') else: application = wsgi.get_wsgi_application()
from config import configs from filters import datetime_filter, rssdate_filter, markdown_filter # 初始化数据库 db.create_engine(**configs['db']) # 创建一个WSGIApplication wsgi = WSGIApplication(os.path.dirname(os.path.abspath(__file__))) # 初始化Jinja2模版引擎 template_engine = Jinja2TemplateEngine(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')) # 把filter添加到jinja2,filter本身是一个函数对象 template_engine.add_filter('datetime', datetime_filter) template_engine.add_filter('rssdate', rssdate_filter) template_engine.add_filter('html', markdown_filter) wsgi.template_engine = template_engine # 加载url中的函数 import urls wsgi.add_interceptor(urls.user_interceptor) wsgi.add_interceptor(urls.manage_interceptor) wsgi.add_model(urls) # 在9000端口启动wsgi if __name__ == '__main__': wsgi.run(9000) else: application = wsgi.get_wsgi_application()
wsgi.template_engine = template_engine # load module import urls wsgi.add_module(urls) # jinja2 filter import time, datetime def datetime_filter(t): delta = int(time.time() - t) if delta < 60: return u"1分钟前" if delta < 3600: return u"%s分钟前" % (delta // 60) if delta < 86400: return u"%s小时前" % (delta // 3600) if delta < 604800: return u"%s天前" % (delta // 86400) dt = datetime.fromtimestamp(t) return u"%s年%s月%s日" % (dt.year, dt.month, dt.day) template_engine.add_filter("datetime", datetime_filter) if __name__ == "__main__": wsgi.run(port=9000, host="0.0.0.0")
if delta < 86400: return u'%s小时前' % (delta // 3600) if delta < 604800: return u'%s天前' % (delta // 86400) dt = datetime.fromtimestamp(t) return u'%s年%s月%s日' % (dt.year, dt.month, dt.day) # init db: db.create_engine(**configs.db) # init wsgi app: wsgi = WSGIApplication(os.path.dirname(os.path.abspath(__file__))) template_engine = Jinja2TemplateEngine( os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')) template_engine.add_filter('datetime', datetime_filter) wsgi.template_engine = template_engine import urls wsgi.add_interceptor(urls.user_interceptor) wsgi.add_interceptor(urls.manage_interceptor) wsgi.add_module(urls) if __name__ == '__main__': wsgi.run(9001, host='127.0.0.1') else: application = wsgi.get_wsgi_application()
if m: return lambda p: p.endswith(m.group(1)) raise ValueError('Invalid pattern definition in interceptor.') "" @interceptor('/manage/') def manage_interceptor(next): user = ctx.request.user if user and user.admin: return next() raise seeother('/signin') """ # 这里的装饰器里的__interceptor__和_build_pattern_fn不是很懂。 # _build_pattern_fn中的返回了lambda,是返回了一个函数? wsgi.add_module(urls) """ def add_module(self, mod): self._check_not_running() m = mod if type(mod)==types.ModuleType else _load_module(mod) logging.info('Add module: %s' % m.__name__) for name in dir(m): fn = getattr(m, name) if callable(fn) and hasattr(fn, '__web_route__') and hasattr(fn, '__web_method__'): self.add_url(fn) """ # 检查输入参数是否是模块,对于模块内的所有函数,检查其是否包含__web_route__属性与__web_method__属性,如果包含,执行add_url函数 # # 在9000端口上启动本地测试服务器: if __name__ == '__main__': wsgi.run(9001, host='0.0.0.0')
if delta < 86400: return u'%s小时前' % (delta // 3600) if delta < 604800: return u'%s天前' % (delta // 86400) dt = datetime.fromtimestamp(t) return u'%s年%s月%s日' % (dt.year, dt.month, dt.day) # init db: db.create_engine(**configs.db) # init wsgi app: wsgi = WSGIApplication(os.path.dirname(os.path.abspath(__file__))) template_engine = Jinja2TemplateEngine( os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')) template_engine.add_filter('datetime', datetime_filter) wsgi.template_engine = template_engine import urls wsgi.add_interceptor(urls.user_interceptor) wsgi.add_interceptor(urls.manage_interceptor) wsgi.add_module(urls) if __name__ == '__main__': wsgi.run(9000, host='0.0.0.0') else: application = wsgi.get_wsgi_application()
return u'1分钟前' if delta < 3600: return u'%s分钟前' % (delta // 60) if delta < 86400: return u'%s小时前' % (delta // 3600) if delta < 604800: return u'%s天前' % (delta // 86400) dt = datetime.fromtimestamp(t) return u'%s年%s月%s日' % (dt.year, dt.month, dt.day) # init db: db.create_engine(**configs.db) # init wsgi app: wsgi = WSGIApplication(os.path.dirname(os.path.abspath(__file__))) template_engine = Jinja2TemplateEngine(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')) template_engine.add_filter('datetime', datetime_filter) wsgi.template_engine = template_engine import urls wsgi.add_interceptor(urls.user_interceptor) wsgi.add_interceptor(urls.manage_interceptor) wsgi.add_module(urls) if __name__ == '__main__': wsgi.run(9000)#, host='0,0,0,0') else: application = wsgi.get_wsgi_application()