[英]Can I call a function which calls another function with the same file with a url request in Python Flask?
from flask import Flask,request
import requests, json
app = Flask(__name__)
@app.route('/checking')
def test1():
print 'in the test1 function'
x = json.dumps({'abcd': 1234})
requests.post('http://127.0.0.1:5123/sending', data = x)
return 'checking done'
@app.route('/sending', methods= ['POST'])
def test2():
print 'receiving data'
request.data
y = json.loads(request.data)
print y
return
if __name__ == '__main__':
app.run(debug= True,host='0.0.0.0', port = 5123)
If I save this file as test.py
and run it, when I call the url http://127.0.0.1:5123/checking
from my browser I would expect the function test1
to send data = x
to test2
如果我将此文件另存为test.py
并运行,当我从浏览器中调用URL http://127.0.0.1:5123/checking
,我希望函数test1
将data = x
发送到test2
you could do one of two things: 您可以执行以下两项操作之一:
make a function that that returns the final data: 创建一个返回最终数据的函数:
def test2(x):
print('receiving data')
print(json.loads(x))
return y
or you could send the data in the request: 或者您可以在请求中发送数据:
@app.route('/checking')
def test1():
x = json.dumps({'abcd':'1234'})
return redirect(url_for('test2', data=x))
@app.route('/sending/<data>')
def test2(data):
print('receiving data')
print(json.loads(data))
return render_template('sending.html', data=data)
from flask you will need to import url_for, redirect and render_template 从烧瓶中,您将需要导入url_for,redirect和render_template
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.