简体   繁体   English

我可以在Python Flask中调用带有相同文件并带有url请求的函数吗?

[英]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 ,我希望函数test1data = 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.

相关问题 我如何知道哪个URL负责在Python Flask中调用路由函数 - How do I know which URL is responsible for the call to a route function from inside of it in Python Flask 我可以将函数结果转换为python / FLASK中另一个函数的变量吗? - Can I transform a function result into a variable for another function in python/FLASK? python如何执行调用从同一对象调用另一个方法的函数的对象方法 - python how to execute object method that calls a function which calls another method from same object 通过Flask中的URL调用函数 - Call function by URL in Flask 如何在Python的同一文件中的另一个函数内调用函数? - How do I call a function inside another function in the same file in Python? Python:我可以在另一个函数的* args列表中调用一个函数吗? - Python: Can I call a function as part of the *args list in another function? 我可以将函数调用作为参数传递给另一个函数吗? 蟒蛇 - Can I pass a function call as a parameter to another function? Python Python:如何调用以另一个函数作为参数的函数? - Python: How can I call a function that has another function as argument? 在python中运行js函数调用另一个js函数 - run js function in python which calls another js function 定义一个函数,该函数针对各种字符串python调用另一个函数 - defining a function which calls another function for a variety of strings python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM