2

I used the Pywin32 tools and NSSM to create a windows service for my Flask application. I noticed that the service wouldn't start giving me a message :

The service did not return an error. This could be an internal Windows error or an internal service error

I noticed that when I removed all reference to a config.json file(used to connect to the DB) the created service starts. My service.py is :

import win32serviceutil
import win32service
import win32event
import servicemanager
from multiprocessing import Process

from app import app


class Service(win32serviceutil.ServiceFramework):
    _svc_name_ = "TestService"
    _svc_display_name_ = "Test Service"
    _svc_description_ = "Tests Python service framework by receiving and echoing messages over a named pipe"

    def __init__(self, *args):
        super().__init__(*args)

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        self.process.terminate()
        self.ReportServiceStatus(win32service.SERVICE_STOPPED)

    def SvcDoRun(self):
        self.process = Process(target=self.main)
        self.process.start()
        self.process.run()

    def main(self):
        app.run()


if __name__ == '__main__':
    if len(sys.argv) == 1:
        servicemanager.Initialize()
        servicemanager.PrepareToHostSingle(RouterService)
        servicemanager.StartServiceCtrlDispatcher()
    else:
        win32serviceutil.HandleCommandLine(RouterService)

The following sample implementation of app.py works :

from flask import Flask
import json
import socket

app = Flask(__name__)


host = "<IP>"
user = "<username>"
passwd = "XXXXX"
DB = "YYYYY"

@app.route('/')
def hello_world():
    return 'Hello, World!'

app.run(host = "0.0.0.0",debug = False, port=9000, threaded=True)

But as soon as I add code to read the DB credentials from a config.json file, the created service gives me an error :

conf = open('.\\config.json', "r")
data = json.loads(conf.read())
db_conf = data['db_connection']

host = db_conf['host']
user = db_conf['username']
passwd = db_conf['password']
DB = db_conf['DB']

Are there any issues with pywin32 reading json files? When I run the same app.py file from the command prompt it reads all the json files and runs without issues.

s_om
  • 661
  • 2
  • 9
  • 24
  • 1
    Where are your 2 *.py* and the *.json* files located? – CristiFati Nov 05 '19 at 10:00
  • They are all in the same folder as of now. – s_om Nov 05 '19 at 15:52
  • You should use try except syntaxt to catch [exception](https://docs.python.org/3/library/exceptions.html) and see what's going wrong. Start by check if the file is correctly opened or print the error, then if json loads it correctly, etc.. Let us know what you see. Is the flask app.py with json config file working without pywin32 ? – Pyglouthon Nov 06 '19 at 09:01
  • Yes app.py works fine if I run it from CMD. I will add login to see if that makes things clearer – s_om Nov 06 '19 at 15:43
  • @Pyglouthon : I addess try - except logic but it didnt log anything. The service just fails to start. Have you tried this in your machine? – s_om Nov 06 '19 at 20:11
  • Which version of python and pywin32 do you use? – Pyglouthon Nov 07 '19 at 07:16

0 Answers0