本文整理匯總了Python中quart.jsonify方法的典型用法代碼示例。如果您正苦於以下問題:Python quart.jsonify方法的具體用法?Python quart.jsonify怎麽用?Python quart.jsonify使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類quart
的用法示例。
在下文中一共展示了quart.jsonify方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_cookie_jar
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import jsonify [as 別名]
def test_cookie_jar() -> None:
app = Quart(__name__)
app.secret_key = "secret"
@app.route("/", methods=["GET"])
async def echo() -> Response:
foo = session.get("foo")
bar = request.cookies.get("bar")
session["foo"] = "bar"
response = jsonify({"foo": foo, "bar": bar})
response.set_cookie("bar", "foo")
return response
client = Client(app)
response = await client.get("/")
assert (await response.get_json()) == {"foo": None, "bar": None}
response = await client.get("/")
assert (await response.get_json()) == {"foo": "bar", "bar": "foo"}
示例2: test_redirect_cookie_jar
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import jsonify [as 別名]
def test_redirect_cookie_jar() -> None:
app = Quart(__name__)
app.secret_key = "secret"
@app.route("/a")
async def a() -> Response:
response = redirect("/b")
response.set_cookie("bar", "foo")
return response
@app.route("/b")
async def echo() -> Response:
bar = request.cookies.get("bar")
response = jsonify({"bar": bar})
return response
client = Client(app)
response = await client.get("/a", follow_redirects=True)
assert (await response.get_json()) == {"bar": "foo"}
示例3: broadcast
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import jsonify [as 別名]
def broadcast():
data = await request.get_json()
for queue in app.clients:
await queue.put(data['message'])
return jsonify(True)
示例4: check_status
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import jsonify [as 別名]
def check_status():
global aredis, sr
status = dict()
try:
if await aredis.get('state') == b'running':
if await aredis.get('processed') != await aredis.get('lastProcessed'):
await aredis.set('percent', round(
int(await aredis.get('processed')) / int(await aredis.get('length_of_work')) * 100, 2))
await aredis.set('lastProcessed', str(await aredis.get('processed')))
except:
pass
try:
status['state'] = sr.get('state').decode()
status['processed'] = sr.get('processed').decode()
status['length_of_work'] = sr.get('length_of_work').decode()
status['percent_complete'] = sr.get('percent').decode()
except:
status['state'] = sr.get('state')
status['processed'] = sr.get('processed')
status['length_of_work'] = sr.get('length_of_work')
status['percent_complete'] = sr.get('percent')
status['hint'] = 'refresh me.'
return jsonify(status)
示例5: test_json
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import jsonify [as 別名]
def test_json() -> None:
app = Quart(__name__)
@app.route("/", methods=["POST"])
async def echo() -> Response:
data = await request.get_json()
return jsonify(data)
client = Client(app)
response = await client.post("/", json={"a": "b"})
assert (await response.get_json()) == {"a": "b"}
示例6: test_form
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import jsonify [as 別名]
def test_form() -> None:
app = Quart(__name__)
@app.route("/", methods=["POST"])
async def echo() -> Response:
data = await request.form
return jsonify(**data)
client = Client(app)
response = await client.post("/", form={"a": "b"})
assert (await response.get_json()) == {"a": "b"}
示例7: test_set_cookie
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import jsonify [as 別名]
def test_set_cookie() -> None:
app = Quart(__name__)
@app.route("/", methods=["GET"])
async def echo() -> Response:
return jsonify({"foo": request.cookies.get("foo")})
client = Client(app)
client.set_cookie("localhost", "foo", "bar")
response = await client.get("/")
assert (await response.get_json()) == {"foo": "bar"}
示例8: weather
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import jsonify [as 別名]
def weather(zip_code: str, country: str):
weather_data = await weather_service.get_current(zip_code, country)
if not weather_data:
quart.abort(404)
return quart.jsonify(weather_data)
示例9: sun
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import jsonify [as 別名]
def sun(zip_code: str, country: str):
lat, long = await location_service.get_lat_long(zip_code, country)
sun_data = await sun_service.for_today(lat, long)
if not sun_data:
quart.abort(404)
return quart.jsonify(sun_data)
示例10: replicas
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import jsonify [as 別名]
def replicas():
return jsonify(await get_replicas(int(request.args.get("type", "1"))))
示例11: _handle_http_event
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import jsonify [as 別名]
def _handle_http_event(self) -> Response:
if self._secret:
if 'X-Signature' not in request.headers:
self.logger.warning('signature header is missed')
abort(401)
sec = self._secret
sec = sec.encode('utf-8') if isinstance(sec, str) else sec
sig = hmac.new(sec, await request.get_data(), 'sha1').hexdigest()
if request.headers['X-Signature'] != 'sha1=' + sig:
self.logger.warning('signature header is invalid')
abort(403)
payload = await request.json
if not isinstance(payload, dict):
abort(400)
if request.headers['X-Self-ID'] in self._wsr_api_clients:
self.logger.warning(
'there is already a reverse websocket api connection, '
'so the event may be handled twice.')
response = await self._handle_event(payload)
if isinstance(response, dict):
return jsonify(response)
return Response('', 204)
示例12: usage
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import jsonify [as 別名]
def usage():
return quart.jsonify({"info": banner}), 201
示例13: recog
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import jsonify [as 別名]
def recog():
if quart.request.files.get("img"):
img = quart.request.files["img"].read()
img = Image.open(io.BytesIO(img))
y_hat = classify(img)
return quart.jsonify({"class": y_hat[0], "prob": y_hat[1]}), 201
return quart.jsonify({"status": "not an image file"})
示例14: not_found
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import jsonify [as 別名]
def not_found(error):
return quart.make_response(quart.jsonify({"error": "Not found"}), 404)
示例15: index
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import jsonify [as 別名]
def index():
return quart.jsonify(banner), 201