0

I have a use case were I need to run multiple python scripts from a same server through flask.

script1.py is as below

app = Flask(__name__)
api = Api(app)

class App(Resource):
   def post(self):
       resp = Response('successfully tested')
       return(resp)
        
api.add_resource(App, '/testapp')


if __name__ == "__main__":

    app.run(port=6000, host="0.0.0.0", use_reloader=True)

Similarly script2.py is

app = Flask(__name__)
api = Api(app)

class Test(Resource):
   def post(self):
   resp = Response('successfully tested')
   return(resp)
        
api.add_resource(Test, '/test')


if __name__ == "__main__":

    app.run(port=5000, host="0.0.0.0", use_reloader=True)

Individually when I execute both works as expected, http://0.0.0.0:5000/test and http://0.0.0.0:5000/testapp works.

But when I configure these scripts as service and try to post the URLs one of them will work and other is failing.

Am I doing right?

2
  • what do you mean as service? Are you using some sort of proxy? Do you want implement a load balancer? The code you show seems not relevant. Commented Aug 5, 2020 at 17:48
  • @TheFool I have converted the script as Windows Service. Yes you are right I need to run through loadbalancer but before that I need both scripts work on local PC. The code is not full there are two API resources with 2 end points "/test" and "/testapp" Will this not work this way? Commented Aug 5, 2020 at 17:52

1 Answer 1

1

You cannot do this as the flask server needs to bind to a port[5000]. You have to run these two scripts on a different port and then you can use Nginx to proxy pass them based on the API rules. Something like below

https://serverfault.com/questions/650117/serving-multiple-proxy-endpoints-under-location-in-nginx You can use any other reverse proxy also you are not bound to use Nginx.

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

3 Comments

Thank you heaps. This helps.
If this has to be through https SSL 443 how to handle?
You can simply do the ssl-offloading on the nginx and then forward it to the respective upstream. Let you nginx listen for requests on 443 and based on your resource like /api/v1 or api/v2 you can forward to 5000 service1 or 5001 service2

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.