本文整理汇总了Python中bottle.HTTPResponse.content_type方法的典型用法代码示例。如果您正苦于以下问题:Python HTTPResponse.content_type方法的具体用法?Python HTTPResponse.content_type怎么用?Python HTTPResponse.content_type使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bottle.HTTPResponse
的用法示例。
在下文中一共展示了HTTPResponse.content_type方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: patch
# 需要导入模块: from bottle import HTTPResponse [as 别名]
# 或者: from bottle.HTTPResponse import content_type [as 别名]
def patch(self, id=None):
if id:
m = self.query().get(id)
if m:
for key, value in self.model.from_dict(request.json, self.db).items():
# if value being replaced is a model or list of models,
# delete it/them from the database first
if isinstance(value, list):
old = getattr(m, key)
newitems = [i for i in value if i not in old]
olditems = [i for i in old if i not in value]
for item in olditems:
old.remove(item)
if issubclass(type(item), Association):
self.db.delete(item)
for item in newitems:
old.append(item)
else:
setattr(m, key, value)
self.db.commit()
self.db.refresh(m)
response = HTTPResponse(status=200, body=json.dumps(m.to_dict()))
response.content_type = "application/json"
return response
abort(404)
示例2: default_error_handler
# 需要导入模块: from bottle import HTTPResponse [as 别名]
# 或者: from bottle.HTTPResponse import content_type [as 别名]
def default_error_handler(error):
if isinstance(error, HTTPError):
r = HTTPResponse()
error.apply(r)
r.content_type = error.content_type
return r
return error
示例3: get
# 需要导入模块: from bottle import HTTPResponse [as 别名]
# 或者: from bottle.HTTPResponse import content_type [as 别名]
def get(self, id=None):
if id:
match = self.query().get(id).to_dict()
if match:
return json.dumps(match)
abort(404)
else:
response = HTTPResponse(status=200, body=json.dumps([m.to_dict() for m in self.query().all()]))
response.content_type = "application/json"
return response
示例4: error404
# 需要导入模块: from bottle import HTTPResponse [as 别名]
# 或者: from bottle.HTTPResponse import content_type [as 别名]
def error404(error):
mheader = request.get_header("host")
output = call_lua("./src/static.lua", mheader, request.path)
resp = HTTPResponse(body=output, status=200)
mtype = "application/octet-stream"
lowered = request.path.lower()
if lowered.endswith("jpg") or lowered.endswith("jpeg"):
mtype = "image/jpeg"
elif lowered.endswith("gif"):
mtype = "image/gif"
elif lowered.endswith("png"):
mtype = "image/png"
elif lowered.endswith("webm"):
mtype = "video/webm"
resp.content_type = mtype
return resp
示例5: render_response
# 需要导入模块: from bottle import HTTPResponse [as 别名]
# 或者: from bottle.HTTPResponse import content_type [as 别名]
def render_response(self, resp):
# always ensure we have a http response instance
if not isinstance(resp, HTTPResponse):
resp = HTTPResponse(resp)
# create new response type
nresp = HTTPResponse()
resp.apply(nresp)
if not request.nctx.renderer: return nresp
# apply rendering
nresp.body = request.nctx.renderer.render(nresp.body)
nresp.content_type = request.nctx.response_content_type
# XXX: manually append charset due to bug
# https://github.com/bottlepy/bottle/issues/1048
if request.nctx.renderer.charset:
to_append = '; charset={}'.format(request.nctx.renderer.charset.upper())
nresp.content_type += to_append
return nresp
示例6: root_post
# 需要导入模块: from bottle import HTTPResponse [as 别名]
# 或者: from bottle.HTTPResponse import content_type [as 别名]
def root_post():
mheader = request.get_header("host")
if request.POST:
json_val = {k:v for k,v in request.forms.items()}
image = request.files.get("image")
if image:
json_val["image"] = TMPFILE_LOC + image.filename
image.save(TMPFILE_LOC, overwrite=True)
music = request.files.get("music")
if music:
json_val["music"] = TMPFILE_LOC + music.filename
music.save(TMPFILE_LOC, overwrite=True)
return call_lua("./src/root.lua", mheader, json.dumps(json_val))
output = call_lua("./src/root.lua", mheader)
resp = HTTPResponse(body=output, status=200)
if mheader.startswith('api.'):
resp.content_type = 'application/json'
return resp
示例7: post
# 需要导入模块: from bottle import HTTPResponse [as 别名]
# 或者: from bottle.HTTPResponse import content_type [as 别名]
def post(self):
m = self.insert(request.json)
response = HTTPResponse(status=201, body=json.dumps(m.to_dict()))
response.content_type = "application/json"
response.add_header("Location", "{}/{}".format(self.base, m.id))
return response