2
我不熟悉正確的瓶的語法,因爲這樣我離開了這個錯誤:Bottle自定義插件將JSON的日期時間返回?
TypeError: call() takes exactly 3 arguments (1 given)
這裏是我的嘗試:
from bottle import Bottle, JSONPlugin, app, route, run, static_file
from json import JSONEncoder, dumps as jsonify
from datetime import datetime
# http://bottlepy.org/docs/dev/recipes.html#ignore-trailing-slashes
class StripPathMiddleware(object):
def __init__(self, app):
self.app = app
def __call__(self, e, h):
e['PATH_INFO'] = e['PATH_INFO'].rstrip('/')
return self.app(e, h)
# https://github.com/defnull/bottle/issues/287
class MyJsonEncoder(JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return str(obj.strftime("%Y-%m-%d %H:%M:%S"))
return JSONEncoder.default(self, obj)
@route('/api')
def latest_api_version():
return {'api_version': 0.1, 'latest_as_of': datetime.utcnow()}
if __name__ == '__main__':
myApp = Bottle(autojson=False)
myApp.install(JSONPlugin(json_dumps=lambda s: jsonify(s, cls=MyJsonEncoder)))
run(app=StripPathMiddleware(myApp()), debug=True)
如何獲得瓶的JSON解析器返回日期時間時間戳數據沒有錯誤?
請張貼完整回溯。給我們十幾行代碼,告訴我們在某個地方有一個錯誤不會讓你走得很遠。這種錯誤實際上是回溯中最不有用的部分。 – kindall