简体   繁体   中英

How to pass a variable from local flask app to remote flask app

I have two flask files. One is home.py and the other is remote.py .

The remote.py will be on another computer in LAN which acts as a server. The home.py will be in my computer.

In the home.py , i pass the url for accessing the remote.py file and if the url gets accessed,the home.py executes the route for adding two numbers and store the result in c variable and sent c to the remote.py file.

In remote.py file, I have a route for getting c value from the home system and store it in a get_c variable.

Here's the Sample Code i tried:

home.py

from flask import Flask

app = Flask(__name__)
@app.route('/sum_value_pass', methods=['POST'])
def sum_value_pass():
    try:
        url = '192.168.1.3:5001/sum_value_get'
        if url accessed:
            a = 4
            b = 4

            c = a + b
            print(c)
            # send c value to remote system
    except:
        print("URL offline")
if __name__ == '__main__':

    app.run(host='0.0.0.0', port=5002)

remote.py

from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/sum_value_get', methods=['POST'])
def sum_value_get():
    get_c = []
    print(get_c)
    # get c from home system not yet written



if __name__ == '__main__':
    app.run(host='192.168.1.3', port=5001)

This is what i tried. I don't know how to pass variable between two flask app. Help me with some solutions to make it possible.

In your remote.py, does the method supported by just GET and not POST

@app.route('/sum_value_get', methods=['GET'])
def sum_value_get():
    get_c = []
    print(get_c)
    # get c from home system not yet written

This would indeed be the correct way to go about it.

If you are needing help with sending get and post requests form your server(s),

https://www.geeksforgeeks.org/get-post-requests-using-python/ might be a good starting point

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM