美文网首页
flask 通用简易post请求api

flask 通用简易post请求api

作者: 是东东 | 来源:发表于2021-10-27 00:55 被阅读0次
from flask import Flask
from flask import request
from requests import post
from json import dumps

app = Flask(__name__)


@app.route('/post', methods=['GET', 'POST'])
def hello_world():

    result = {}
    result['code'] = 0
    result['msg'] = 'failed'

    if request.method == 'POST':
        try:
            post_url = ''
            data = dumps(request.form)
            res = post(url=post_url, data=data, timeout=20)
            code = res.status_code
            result['code'] = code
            html = res.text
            result['html'] = html
            if code == 200:
                result['msg'] = 'success'
        except Exception as e:
            result['code'] = 408
            result['error_msg'] = f'{e}'
    else:
        result['code'] = 405
        result['error_msg'] = '请使用post请求'
    return dumps(result)


if __name__ == '__main__':
    app.run(host='', port='8080')

相关文章

网友评论

      本文标题:flask 通用简易post请求api

      本文链接:https://www.haomeiwen.com/subject/injualtx.html