Search code examples
pythonflaskflask-restful

Running a flask service as a windows service


This must be really trivial, but i just cannot find a way to run a flask web service on a windows server (win server 2008). I can run it manually but how dot i get it to startup as a service so i can consume the service it code exposes.

Here is a simple example i am trying to deploy to a windows server:

from flask import Flask, request
from flask_restful import Resource, Api

from flask_cors import CORS

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

class Root(Resource):
    def get(self):
        return {'hello': 'world Root'}


api.add_resource(Root, '/');

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

Solution

  • The easiest way I found to install a python program as a windows service is by using NSSM

    Run "nssm install " and in the fields for:

    Path: <path to python.exe>
    Arguments: <path to your python file>
    

    Once this is done you will have a windows service installed and then you can start that and this will stay running all the time hosting your flask app