0

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)
shivas
  • 913
  • 10
  • 15
  • you'll need a web-server (or a wsgi server) – thebjorn Oct 18 '16 at 18:48
  • I have tried this: https://medium.com/@as_w/running-a-python-web-app-flask-on-windows-server-iis-using-fastcgi-and-wfastcgi-py-b49875b637f7#.kiz03qvsl but am i missing something, why is it so difficult to set this up on the server. I do not see any documents to do this in a direct way – shivas Oct 18 '16 at 18:50
  • I can give you the error from IIS: HTTP Error 500.0 - Internal Server Error C:\Python34\python.exe - The FastCGI process exited unexpectedly – shivas Oct 18 '16 at 18:52
  • What is the recommended way to run flask on a server? Any pointers to set this up would be helpful – shivas Oct 18 '16 at 18:53
  • The general structure of a Python windows service is something like: http://code.activestate.com/recipes/576451-how-to-create-a-windows-service-in-python/ I have no experience with the technologies you're linking to though.. – thebjorn Oct 18 '16 at 18:57
  • 1
    @shivas We run Flask in production on Microsoft IIS. That would be the recommended way to do this on Windows. The learning curve on the setup on IIS can be steep compared to its linux counterparts, but Microsoft is kind enough to have IIS web platform installers for Python to do exactly this. Visual Studio also has nice Flask templates that include the necessary `web.config` files. Lots of guides out there for this, too. I would not recommend trying to daemonize your app through a Windows service. Use IIS - that's what it was made to do. – sytech Oct 18 '16 at 20:50
  • @Gator_Python I would like to use IIS, i followed two blogs on the setup and was unable to run the python code in IIS. That is the reason why i reach out here to see why this is so difficult to setup. I will check out the VS templates. But any documents you can point me to will be great – shivas Oct 18 '16 at 23:33

1 Answers1

4

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

Undo
  • 25,519
  • 37
  • 106
  • 129
shivas
  • 913
  • 10
  • 15