1

I have created 2 apps in FLASK, one app will issue a GET request and pull the data, 2nd app will take that data from app 1 and issue a POST request to push data into other system.

APP1.py

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

    @app.route('/pushdata', methods=['GET', 'POST'])
    def testfn():
        # POST request
         if request.method == 'POST':
            print(request.get_json())  # parse as JSON
            req = request.get_json()
            print(type(req))
            print(req)
            return 'Success!! Reached Flask', 200

    # main driver function
     if __name__ == '__main__':
        app.run(debug=True)

APP2.py

    from flask import Flask,jsonify,request,make_response,url_for,redirect
    import requests, json
    app = Flask(__name__)

    url = 'http://xx.xx.xx.xxx:<PORT>/lemte/7/rest/tenten/'

    json_data = **<JSON DATA received from APP1....................>**

    headers = {'content-type': 'application/json'}

    res = requests.post(url, headers=headers, auth=('XXXXX', 'YYYYY'), data=json.dumps(json_data))
    print ('response from server:',res.text)
    dictFromServer = res.json()

    if __name__ == '__main__':
        app.run(host='localhost',debug=False, use_reloader=True)

How can I achieve this? searched a lot, but unable to find any hints/suggestions.

James Z
  • 12,209
  • 10
  • 24
  • 44
Oxxodome
  • 13
  • 4
  • Probably restructuring your code around two Flask **blueprints** could be what you are looking for. – Kate May 09 '21 at 14:29
  • What is your actual problem? What have you tried? Your description doesn't offer a hint where you have issues. As a new user here, please take the [tour] and read [ask]. BTW: Parts of your code isn't indented correctly, which is not acceptable for Python. – Ulrich Eckhardt May 09 '21 at 14:44
  • Actual problem is I can't pass the JSON obtained from APP1.py to APP2.py if you have a closer look at the APP2.py , there is code line which says : json_data = , so i need a hint as in how can i correct this line hope that clarifies – Oxxodome May 09 '21 at 17:26
  • Any suggestions !! – Oxxodome May 10 '21 at 16:37

3 Answers3

1

Perhaps you could try through a POST request from APP1 to APP2 or through sockets where APP1 would be the server and APP2 the client.

Edit:

This might help: communication-between-two-python-scripts and communication-between-two-python-scripts

They both have the same title but different answers

1

this solves your problem in order to let 2 flask app talk to each other : In APP1.py:

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

@app.route('/lemte/7/rest/tenten', methods=['GET', 'POST'])
def testfn():
    if request.method == 'POST':
           print(request.get_json())  # parse as JSON
           req = request.get_json()
           print(type(req))
           print(req)
           return jsonify({"message":"success!! readched flask"}),200
    # return 'Success!! Reached Flask', 200


if __name__ == '__main__':
    app.run(debug=True,port=5201)

In APP2.py:

from flask import Flask,jsonify,request,make_response,url_for,redirect 
import requests, json

app = Flask(__name__)

url = 'http://localhost:5201/lemte/7/rest/tenten'

json_data = {"test":"hello"}

headers = {'content-type': 'application/json'}

res = requests.post(url, headers=headers, auth=('XXXXX', 'YYYYY'), 
data=json.dumps(json_data))
print ('response from server:',res.text)
dictFromServer = res.json()

if __name__ == '__main__':
    app.run(host='localhost',debug=False, use_reloader=True,port=5200)
Mohsenab
  • 11
  • 1
0

You did not not specified any error here

I assume the error is that you tried to run both in same port which is not possible

You can run each in different ports by specifying port in app.run

example:

app1

from flask import Flask

app = Flask(__name__)

if __name__ == "__main__":
    app.run(host="localhost", port=5000)

app2

from flask import Flask

app = Flask(__name__)

if __name__ == "__main__":
    app.run(host="localhost", port=5001)

You can use python app1.py and python app2.py in two different terminals to run the servers

ipinak
  • 5,739
  • 3
  • 23
  • 41
  • Thanks for the comment, but my question is how the APP1 will pass JSON data to APP2 , if you see in the APP2 code , json_data = **** , I'm exploring this ??? – Oxxodome May 09 '21 at 13:04