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)
如果我将此文件保存为test.py
并运行它,当我从浏览器调用网址http://127.0.0.1:5123/checking
时,我希望函数test1
将data = x
发送到{ {1}}
答案 0 :(得分:0)
你可以做以下两件事之一:
创建一个返回最终数据的函数:
def test2(x):
print('receiving data')
print(json.loads(x))
return y
或者您可以在请求中发送数据:
@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)
从烧瓶中你需要导入url_for,redirect和render_template