本文整理汇总了Python中bottle.HTTPResponse类的典型用法代码示例。如果您正苦于以下问题:Python HTTPResponse类的具体用法?Python HTTPResponse怎么用?Python HTTPResponse使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了HTTPResponse类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: default_error_handler
def default_error_handler(error):
if isinstance(error, HTTPError):
r = HTTPResponse()
error.apply(r)
r.content_type = error.content_type
return r
return error
示例2: patch
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)
示例3: get_response_from_exception
def get_response_from_exception(excep, exception_mapping):
status = exception_mapping.get(excep.__class__, None)
if status is None:
status = 500
ret = HTTPResponse(status=status, body=str(excep))
ret.add_header("Content-Type", "text/plain")
return ret
示例4: kml_index
def kml_index(self):
session = self.beaker
print 'kml_index'
err, suc = [], []
kml_layer_name = ''
with self.session() as s:
try:
if request.method == 'POST':
csrf = request.params.get('csrf', '')
if session.get('csrf', '') != csrf:
print 'csrf token mismatch'
r = HTTPResponse(status=302)
r.set_header('Location: /kml/')
raise r
print 'method = POST'
name = request.params.get('name', '')
name = name.decode('utf-8')
upload = request.files.get('kml_file', '')
if not name:
err.append(u'KMLレイヤーの名前を入力してください')
else:
kml_layer_name = name
if not upload:
err.append(u'ファイルがアップロードされていません')
if not err:
tmp_fname = '/tmp/minarepo_kml_%s' % random_str(40)
upload.save(tmp_fname)
try:
with open(tmp_fname, 'rb') as fh:
fdata = fh.read()
geo_layer = GeoLayer(
name=name,
content=fdata,
file_size=len(fdata)
)
s.add(geo_layer)
finally:
os.remove(tmp_fname)
suc.append(u'KMLレイヤーを作成しました: %s' % name)
kml_layer_name = ''
layers = s.query(GeoLayer).order_by(GeoLayer.created.desc())
layers = [ l.to_api_dict() for l in layers ]
except:
s.rollback()
raise
else:
s.commit()
print 'commited'
session['csrf'] = random_str(100)
return self._render('kml.html.j2', err=err, suc=suc,
layers=layers, kml_layer_name=kml_layer_name, session=self.beaker)
示例5: postTest
def postTest():
var1 = request.forms.get('prm1')
var2 = request.forms.get('prm2')
retBody = 'Hello Post ' + var1 + ', ' + var2
r = HTTPResponse(status=200, body=retBody)
r.set_header('Content-Type', 'text/html')
return r
示例6: method_not_allowed
def method_not_allowed(res):
if request.method == 'OPTIONS':
new_res = HTTPResponse()
new_res.set_header('Access-Control-Allow-Origin', '*')
new_res.set_header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE')
return new_res
res.headers['Allow'] += ', OPTIONS'
return request.app.default_error_handler(res)
示例7: auth_fail
def auth_fail(self, gss_context=None):
resp = HTTPResponse(self._fail_response, status=401)
resp.set_header('Content-Type', self._content_type)
resp.set_header(
'WWW-Authenticate',
'Negotiate' + (' ' + gss_context if gss_context else '')
)
return resp
示例8: getTest
def getTest():
print request
var1 = request.query.prm1
var2 = request.query.prm2
retBody = 'Hello Get ' + var1 + ', ' + var2
r = HTTPResponse(status=200, body=retBody)
r.set_header('Content-Type', 'text/html')
return r
示例9: apikeyNotValid
def apikeyNotValid():
response = HTTPResponse()
response.status = 200
response.body = json.dumps(
{
'message': 'api key not valid',
}
) + "\n"
return response
示例10: method_not_allowed
def method_not_allowed(res):
if request.method == 'OPTIONS':
new_res = HTTPResponse()
new_res.headers['Access-Control-Allow-Origin'] = '*'
new_res.headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS, PUT'
new_res.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept'
return new_res
res.headers['Allow'] += ', OPTIONS'
return request.app.default_error_handler(res)
示例11: success
def success():
response = HTTPResponse()
response.status = 200
response.body = json.dumps(
{
'message': 'Success'
}
) + "\n"
return response
示例12: postTestJson
def postTestJson():
var = request.json
retBody = {
"ret": "ok",
"retPrm": "XX " + var["prm1"] + var["prm2"],
}
r = HTTPResponse(status=200, body=retBody)
r.set_header('Content-Type', 'application/json')
return r
示例13: get
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
示例14: make_error
def make_error(message):
formats = {
'json': 'application/json',
'xml': 'application/xml',
'jsonp': 'application/javascript',
}
format_ = bottle.request.query.get('format', 'json')
response = HTTPResponse(status=message, content_type=formats[format_])
response.headers['Access-Control-Allow-Origin'] = '*'
return response
示例15: cannotTweet
def cannotTweet():
response = HTTPResponse()
response.status = 500
response.body = json.dumps(
{
'message': 'Failed',
'Error': 'CannotTweet'
}
) + "\n"
return response