goe
goe

Reputation: 337

flaskapp - Two api resources in two different python scripts

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?

Upvotes: 0

Views: 211

Answers (1)

chowmean
chowmean

Reputation: 152

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.

Upvotes: 1

Related Questions