简体   繁体   中英

Python Flask with Waitress server cannot run, when making it as a Windows service

I built a Python application with Flask and run with Waitress server.

The app use local .csv files as the input data.

It can run well when running by command line. (ie. python webserver.py), I can load csv to read data, upload (to overwrite) the csv files.

But when I add it as a window's service (with nssm, or Window Resource Kit), my app can run, but the csv files only can be loaded by JS, not by the python.

It means, in service mode, if I load csv using js, It's ok, but when loading or uploading file (using python script) It returns "Internal Server Error".

My question is, how are running by command line and adding as Window's service different? How to make python script works with csv file when making it as service?

Any help is appreciated. Thank you so much.

This is the upload code.

@app.route('/uploadss', methods = ['GET', 'POST'])
def upload():
    import os
    print(request.files['file'])
    if request.method=='POST':
        file = request.files['file']
        filenames = ['temperature.csv','inlet_clean_info.csv','log_data.csv','medium-term-temperature.csv']
        if file.filename in filenames:
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], file.filename))
            return 'file uploaded successfully'
        else:
            return 'filename is not acceptable'

and I have add waitress webserver as service with:

> -(nssm) nssm.exe install MyService. Then add python path and the executed python file.
> -(Window ResKit) instsrv.exe LNG c:\reskit\srvany.exe. Then add "Parameter" key in Regedit, add "Application" String to point to <python path> <executed path>

In both cases, it returns "Internal Server Error"

This is the Error message, the returned response

" 500 Internal Server Error

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

"

The primary differences when running an app as a service are around environment.

Some things specifically:

Which user the service is running as.
When you start it from the command line, it's likely running as your user account. Your user account is likely to have different permissions from the Windows system account. This tends to cause issues with accessing files more than, for example, opening a port for an HTTP server. There may be other permissions related problems.

Environment variables, including PATH.
When you're in a command shell, windows has a PATH variable that tells it where to look for executable files. So if you type python it looks for python.exe in the current folder, then it searches through the PATH variable until it finds python.exe

You also have other environment variables that can be defined such as TMP (temporary folder), etc.

When it's running as a service, it's generally running in a different user context, which will have different environment variables, both the %TMP% and PATH. So one thing that could be happening is that it's trying to run python.exe but there's no python.exe on the path for the service user.

HKCU Registry entry
If your app uses the registry, and uses the HKEY_CURRENT_USER tree, this is likely different when it's running as a service. (HKEY_CURRENT_MACHINE is likely the same).

The folder that the application starts in
When you run it from the command line, you generally start it in the current folder. This means that it's possible to use relative paths (eg .\\images ) instead of absolute paths ( c:\\website\\images ).

If you were to change to a different folder, then the .\\images version may not work.

When a program runs as a service, it usually seems to start in C:\\Windows\\System32

So you could investigate whether using absolute paths works. Or you could investigate whether there's a way to specify the startup folder.

Another thing to check (possibly first) - look for the log file

Normally web servers write a log file on the server somewhere.

The 500 error would go to the user, but there'd be a more detailed error written to a log. So find where that log file should be and check it out. (It's possible that it's not where it should be, and that could be to do with permissions associated with the User the service is running as). If it is there, it may help track down the particular issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM