1

I create two apps say: App1 and App2 with flask.

App1

@App1.route('/api/v1.0/call_database')

def _database():

 ...
 ...

App2

@App2.route('/api/v1.0/calculate')

def _calculate():
   ...
   ...

App1 is centrally contacting my database. How can I use App2 to call App1 ?

What I was trying is:

@App2.route('/api/v1.0/calculate')
def _calculate():
 ...
      response = requests.get(url = ('http://{}:{}/api/v1.0/call_database'.format(data_store_url, data_store_port)), data = parameters)

...
1
  • Your question is not very clear. Since you are building it. Why would u do a circular call? Just let one api do one thing only... Commented Nov 23, 2017 at 11:38

1 Answer 1

2

This is your first app script:

from flask import Flask, request

app = Flask(__name__)


@app.route("/app1/")
def app1():
    return str(request.args)


app.run(port=5000)

This is your second app script:

from flask import Flask, request
import requests

app = Flask(__name__)


@app.route("/app2/")
def app2():
    # requests.get(url, params={})
    res = requests.get("http://127.0.0.1:5000/app1/", params={"a": "123"})
    return str(res.text)


app.run(port=5001)

When you go to http://127.0.0.1:5001/app2/ - you get:

ImmutableMultiDict([('a', u'123')])

That's expected. That's it.

Sign up to request clarification or add additional context in comments.

4 Comments

May I know how to achieve if app1 is in different machine and i create app2 in another machine to call app1 and they are connected to same network?
@Codenewbie just specify ip address of another machine instead of 127.0.0.1
and what about port in app.run(port=) ?
@Codenewbie this is the port to be listened on machine where flask app is running. You can choose it whatever you want but keep in mind that you will need root (administrator) permissions if port number will be less than 1025, so it's recommended to use the following port range 1025-65535, because it's not restricted for non-root user. More info about ranges here: unix.stackexchange.com/questions/16564/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.