for u in session.query(User).all(): u = dict(u.__dict__) u.pop('_sa_instance_state', None)
@app.route('/set_cookie') def set_cookie(): response=make_response('Hello World'); response.set_cookie('Name','Joo') return response
@app.route('/get_cookie') def get_cookie(): name=request.cookies.get('Name') return name
@app.route('/del_cookie') def del_cookie(): response=make_response('delete cookie') response.set_cookie('Name','',expires=0) return response
@app.route('/del_cookie') def del_cookie(): response=make_response('delete cookie') response.delete_cookie('Name') return response
微信獲取access_token時有效期是7200s,並且微信限制了天天的調用頻率(2000次/天),因此簡單使用lru_cache在內存中維護了一個token緩衝,示例代碼以下:css
@lru_cache(None) def getAccessToken(): url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}'.format(app_id, app_secret) r = requests.get(url) access_token = r.json().get('access_token') time_now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") print '[{0}] getAccessToken Result:\t{1}'.format(time_now, r.text) return access_token, datetime.now() def getToken(): token, t = getAccessToken() if (datetime.now() - t).seconds > 3600: getAccessToken.cache_clear() token, t = getAccessToken() return token else: return token
參考:html
bootstrap = lwrap( WebCDN('//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/%s/' % BOOTSTRAP_VERSION), local) jquery = lwrap( WebCDN('//cdnjs.cloudflare.com/ajax/libs/jquery/%s/' % JQUERY_VERSION), local) html5shiv = lwrap( WebCDN('//cdnjs.cloudflare.com/ajax/libs/html5shiv/%s/' % HTML5SHIV_VERSION)) respondjs = lwrap( WebCDN('//cdnjs.cloudflare.com/ajax/libs/respond.js/%s/' % RESPONDJS_VERSION))
bootstrap = lwrap( WebCDN('//cdn.bootcss.com/twitter-bootstrap/%s/' % BOOTSTRAP_VERSION), local) jquery = lwrap( WebCDN('//cdn.bootcss.com/jquery/%s/' % JQUERY_VERSION), local) html5shiv = lwrap( WebCDN('//cdn.bootcss.com/html5shiv/%s/' % HTML5SHIV_VERSION)) respondjs = lwrap( WebCDN('//cdn.bootcss.com/respond.js/%s/' % RESPONDJS_VERSION))
# coding:utf-8 from tornado.wsgi import WSGIContainer from tornado.httpserver import HTTPServer from tornado.ioloop import IOLoop from run import app http_server = HTTPServer(WSGIContainer(app)) # address爲實際訪問URL,port爲端口號 http_server.listen(port=5000, address="127.0.0.1") IOLoop.instance().start()