从python到烧瓶的传入请求错误

时间:2018-12-05 07:16:00

标签: python flask

我将python的请求传递给flask。我无法访问烧瓶中从python发送的请求。 这是我的python函数-

import requests

dice_roll = 1
dealear_roll = 2

url = 'http://127.0.0.1:5000/dice'
data = {'dice_roll':dice_roll,'dealear_roll':dealear_roll}
r = requests.post(url=url,data=data)

这是烧瓶api

from flask import Flask, render_template
import random
from flask import request

app = Flask(__name__)

@app.route('/dice')
def dice_roll():

    dice_roll = request.args.get('dice_roll')
    dealear_roll = request.args.get('dealear_roll')

    print('dice_roll',dice_roll)
    print('dealear_roll',dealear_roll)



if __name__ == '__main__':
    app.run(debug=True)

我无法访问烧瓶中的请求。谁能告诉我我在哪里做错了?

2 个答案:

答案 0 :(得分:1)

您应该使用request.form.get而不是request.args.get

from flask import Flask, render_template
import random
from flask import request

app = Flask(__name__)

@app.route('/dice', methods=['GET', 'POST'])
def dice_roll():
    if request.method == 'POST':
        dice_roll = request.form.get('dice_roll')
        dealear_roll = request.form.get('dealear_roll')

        print('dice_roll', dice_roll)
        print('dealear_roll', dealear_roll)
    return ''



if __name__ == '__main__':
    app.run(debug=True)

答案 1 :(得分:0)

您需要在GET,POST这样的路由中添加方法处理程序。

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

    dice_roll = request.args.get('dice_roll')
    dealer_roll = request.args.get('dealer_roll')

    print('dice_roll',dice_roll)
    print('dealer_roll',dealer_roll)