为了账号安全,请及时绑定邮箱和手机立即绑定

使用flask python issue获取响应status_code

使用flask python issue获取响应status_code

白板的微信 2021-11-16 14:32:30
我想使用 Flask python 获得监控服务的响应 HTTP status_code。我的代码:from flask import Flask, request, url_for, redirect, render_template, flash, Response, abortimport requestsres = requests.get('https://192.168.1.100:8000/token?api=API',verify=False)app = Flask(__name__)print(res.url) # your.domain/test/3/3print(res.status_code) # 200@app.route('/services')def check_services():   if request.method == "GET" or request.method == "POST":         if res.status_code == '201':                result =  'Sucess HTTP.Status_code 201 - Service TOKEN is working fine [OK]'               return render_template('services.html'), 201#              result = '401 -- Service TOKEN is working fine - parameter is not correct'        else:#                abort(401)                 return render_template('services401.html'),401  但我的代码没有正常工作。我的预期结果,它将由单独的 HTTP 状态代码返回:201 或 401 或 500,无论我在if条件中定义什么。任何帮助表示赞赏!!!
查看完整描述

1 回答

?
翻阅古今

TA贡献1780条经验 获得超5个赞

状态代码是int,不是str。因此,请检查201,而不是'201'。你的代码应该是:


if request.method == "GET" or request.method == "POST": 

    if res.status_code == 201:

        # do what you want here

详情请见下文。


>>> import requests

>>> r = requests.get('https://httpbin.org/get')

>>> r.status_code

200

>>> type(r.status_code)

<class 'int'>


查看完整回答
反对 回复 2021-11-16
  • 1 回答
  • 0 关注
  • 370 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信