从python到flask的传入请求错误
我将请求从python传递到flask。我无法访问flask中python发送的请求。 这是我的python函数-从python到flask的传入请求错误,python,flask,Python,Flask,我将请求从python传递到flask。我无法访问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 fro
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)
我无法访问flask中的请求。有人能告诉我哪里做错了吗?您需要在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)
您应该使用
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)
不确定这是否是唯一的问题,但您使用的是
dealear\u roll
和dealer\u roll
-即不同keys@buran我现在更新了变量。你可以检查这个问题。即使同样的错误,同样的错误也不能告诉我们太多。将您获取的完整回溯从args.get更改为form.get,但它不会打印任何响应。当我终止程序时,它会打印响应,但不会在发送请求时打印。等等,让我检查一下我的系统,它工作正常。我正在使用GITBash编译程序。会有什么问题吗?我在linux上测试过,它正在工作。非常感谢@Nihal