简体   繁体   English

使用Waitress服务器的Python Flask在将其作为Windows服务时无法运行

[英]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. 我用Flask构建了一个Python应用程序并使用Waitress服务器运行。

The app use local .csv files as the input data. 该应用程序使用本地.csv文件作为输入数据。

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. (即python webserver.py),我可以加载csv来读取数据,上传(覆盖)csv文件。

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. 但是当我将它添加为窗口服务(使用nssm或Window Resource Kit)时,我的应用程序可以运行,但csv文件只能由JS加载,而不能由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". 这意味着,在服务模式下,如果我使用js加载csv,没关系,但是当加载或上传文件时(使用python脚本)它返回“内部服务器错误”。

My question is, how are running by command line and adding as Window's service different? 我的问题是,如何通过命令行运行并添加Window的服务不同? How to make python script works with csv file when making it as service? 如何将python脚本作为服务使用csv文件?

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 “ 500内部服务器错误

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. 您的用户帐户可能具有Windows系统帐户的不同权限。 This tends to cause issues with accessing files more than, for example, opening a port for an HTTP server. 这往往会导致访问文件的问题,例如,打开HTTP服务器的端口。 There may be other permissions related problems. 可能存在其他与权限相关的问题。

Environment variables, including PATH. 环境变量,包括PATH。
When you're in a command shell, windows has a PATH variable that tells it where to look for executable files. 当你在命令shell中时,windows有一个PATH变量,告诉它在哪里查找可执行文件。 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 因此,如果您键入python它会在当前文件夹中查找python.exe ,然后搜索PATH变量,直到找到python.exe

You also have other environment variables that can be defined such as TMP (temporary folder), etc. 您还可以定义其他可以定义的环境变量,例如TMP(临时文件夹)等。

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. 当它作为服务运行时,它通常在不同的用户上下文中运行,这将具有不同的环境变量,%TMP%和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. 所以可能发生的一件事是它正在尝试运行python.exe但是服务用户的路径上没有python.exe

HKCU Registry entry HKCU注册表入口
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_USER树,则当它作为服务运行时可能会有所不同。 (HKEY_CURRENT_MACHINE is likely the same). (HKEY_CURRENT_MACHINE可能相同)。

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 ). 这意味着可以使用相对路径(例如.\\images )而不是绝对路径( c:\\website\\images )。

If you were to change to a different folder, then the .\\images version may not work. 如果您要更改为其他文件夹,则.\\images版本可能无法正常工作。

When a program runs as a service, it usually seems to start in C:\\Windows\\System32 当程序作为服务运行时,它通常似乎从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. 通常,Web服务器会在某处的服务器上写入日志文件。

The 500 error would go to the user, but there'd be a more detailed error written to a log. 500错误将发送给用户,但是会有更详细的错误写入日志。 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. 如果它存在,它可能有助于追踪特定问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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