本文整理汇总了Python中bottle.HTTPResponse.body方法的典型用法代码示例。如果您正苦于以下问题:Python HTTPResponse.body方法的具体用法?Python HTTPResponse.body怎么用?Python HTTPResponse.body使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bottle.HTTPResponse
的用法示例。
在下文中一共展示了HTTPResponse.body方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: send_response
# 需要导入模块: from bottle import HTTPResponse [as 别名]
# 或者: from bottle.HTTPResponse import body [as 别名]
def send_response(code, message=None):
r = HTTPResponse(status=code)
r.add_header('Access-Control-Allow-Origin', '*')
r.set_header('Content-Type', 'application/json')
if message is None:
r.set_header('Content-Type', 'text/plain')
elif type(message) is str:
e = {'error': message}
r.body = json.dumps(e)
elif type(message) is dict:
r.body = json.dumps(message)
else:
r.body = message
return r
示例2: apikeyNotValid
# 需要导入模块: from bottle import HTTPResponse [as 别名]
# 或者: from bottle.HTTPResponse import body [as 别名]
def apikeyNotValid():
response = HTTPResponse()
response.status = 200
response.body = json.dumps(
{
'message': 'api key not valid',
}
) + "\n"
return response
示例3: success
# 需要导入模块: from bottle import HTTPResponse [as 别名]
# 或者: from bottle.HTTPResponse import body [as 别名]
def success():
response = HTTPResponse()
response.status = 200
response.body = json.dumps(
{
'message': 'Success'
}
) + "\n"
return response
示例4: cannotTweet
# 需要导入模块: from bottle import HTTPResponse [as 别名]
# 或者: from bottle.HTTPResponse import body [as 别名]
def cannotTweet():
response = HTTPResponse()
response.status = 500
response.body = json.dumps(
{
'message': 'Failed',
'Error': 'CannotTweet'
}
) + "\n"
return response
示例5: badRequest
# 需要导入模块: from bottle import HTTPResponse [as 别名]
# 或者: from bottle.HTTPResponse import body [as 别名]
def badRequest(key):
response = HTTPResponse()
response.status = 400
response.body = json.dumps(
{
'message': 'Failed',
'BadRequest': key
}
) + "\n"
return response
示例6: api_get_info
# 需要导入模块: from bottle import HTTPResponse [as 别名]
# 或者: from bottle.HTTPResponse import body [as 别名]
def api_get_info():
response = HTTPResponse()
all_ = request.query.get('all')
issue = request.query.get('issue')
if all_ in ['1', 'True', 'true']:
rows = get_all_info()
response.body = json.dumps(
rows,
default=default_datetime_format
) + "\n"
elif issue is not None:
row = get_info(issue)
response.body = json.dumps(
row,
default=default_datetime_format
) + "\n"
else:
row = get_latest_info()
response.body = json.dumps(
row,
default=default_datetime_format
) + "\n"
return response
示例7: render_response
# 需要导入模块: from bottle import HTTPResponse [as 别名]
# 或者: from bottle.HTTPResponse import body [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
示例8: consume
# 需要导入模块: from bottle import HTTPResponse [as 别名]
# 或者: from bottle.HTTPResponse import body [as 别名]
def consume(self, event, *args, **kwargs):
# There is an error that results in responding with an empty list that will cause an internal server error
original_event_class, response_queue = self.responders.pop(event.event_id, None)
if response_queue:
accept = event.get('accept', original_event_class.content_type)
if not isinstance(event, self.CONTENT_TYPE_MAP[accept]):
self.logger.warning(
"Incoming event did did not match the clients Accept format. Converting '{current}' to '{new}'".format(
current=type(event), new=original_event_class.__name__))
event = event.convert(self.CONTENT_TYPE_MAP[accept])
local_response = HTTPResponse()
status, status_message = event.status
local_response.status = "{code} {message}".format(code=status, message=status_message)
for header, value in event.headers.iteritems():
local_response.set_header(header, value)
local_response.set_header("Content-Type", event.content_type)
if int(status) == 204:
response_data = ""
else:
response_data = self.format_response_data(event)
local_response.body = response_data
response_queue.put(local_response)
response_queue.put(StopIteration)
self.logger.info("[{status}] Service '{service}' Returned in {time:0.0f} ms".format(
service=event.service,
status=local_response.status,
time=(datetime.now()-event.created).total_seconds() * 1000), event=event)
else:
self.logger.warning("Received event response for an unknown event ID. The request might have already received a response", event=event)
示例9: api_update_info
# 需要导入模块: from bottle import HTTPResponse [as 别名]
# 或者: from bottle.HTTPResponse import body [as 别名]
def api_update_info(id_):
response = HTTPResponse()
required_key = ['apikey']
optional_key = ['type', 'service', 'begin', 'end', 'detail']
try:
params = require(required_key)
except RequireNotSatisfiedError as e:
return badRequest(e.message)
params.update(optional(optional_key))
tw_flg = request.forms.get('tweet', False)
if params['apikey'] != cfg['API_KEY']:
return apikeyNotValid()
for value in params.values():
if value is not None:
if update(id_, params) and tw_flg:
tweet(get_status(get_info(id_)))
break
row = get_info(id_)
response.body = json.dumps(
row,
default=default_datetime_format
)+"\n"
return response
示例10: jsonify
# 需要导入模块: from bottle import HTTPResponse [as 别名]
# 或者: from bottle.HTTPResponse import body [as 别名]
def jsonify(body='', status=200):
response = HTTPResponse(content_type='application/json')
response.body = json.dumps(body)
response.status = status
return response
示例11: jsonify
# 需要导入模块: from bottle import HTTPResponse [as 别名]
# 或者: from bottle.HTTPResponse import body [as 别名]
def jsonify(obj, status=200):
response = HTTPResponse(content_type='application/json')
response.body = json.dumps(obj, encoding='latin1')
response.status = status
return response