Search code examples
pythonapacheapiflaskflask-restful

How can I host a flask api on local web server?


I have made an flask API which would take input directory as input parameter by get http request. It is working fine on port 5000 on the local machine. Now I want that working api to run on a web server so that I can use it over internet. I tried apache 2 server to do the work but it wasn't successful.

    # API
app = Flask(__name__)

@app.route('/')
def home():
    return  "HOME PAGE LOADED"

@app.route('/runDocumentManager', methods=["GET"])
def runDocumentManager():
    # pdf path
    file = request.args.get('input_path')

.
.
.
if __name__ == '__main__':
    app.run()

Solution

  • Once you start your website it will be hosted on localhost:5000
    So you want to make it host on your network first. Do so with:

     app.run(host="0.0.0.0")
    

    This will make your app run on your local network.
    Then you will need to portforward it on your router. This is so that if someone types in your ip then it will send them to your router.
    There you want your router to route it to your machine's server.
    So if someone connects to (Your IP):5000 then it will place them on (192.168.0.X):5000 (aka. The server python webserver you are running). I highly recomend googling on how to portforward as it differs slightly on different routers

    Usualy you can find it at either:

    1. https://192.168.0.1
    2. https://192.168.1.1
    3. https://192.168.2.1

    You might be required to type in the password and username which usualy is "admin" for both but may be different depending on your ISP. You can check that online as well.