安装并启动 CherryPy 服务器脚本作为 Windows 服务(使用 sc.exe)
我正在尝试安装并启动一个简单的 CherryPy 服务器作为 Windows 服务。
这是脚本:(删除了一些行以使其简短。从命令行手动执行时它完全可以工作)
app = AdminMediaHandler(django.core.handlers.wsgi.WSGIHandler())
logged_app = TransLogger(app)
server = wsgiserver.CherryPyWSGIServer( ('127.0.0.1', 8632), logged_app, server_name='localhost', numthreads=20 )
try:
server.start()
except KeyboardInterrupt:
server.stop()
我使用 sc.exe
来安装和启动服务。安装顺利,但我似乎无法启动该服务。
使用的命令是:(注意路径中有空格,尽管我用双引号处理它,并且 binPath
在通过命令行手动执行其字符串时正在工作)
> sc.exe create "ServiceName" binPath= "\"C:\Path to Python\python.exe\" \"C:\Path to CherryPy Script\cherryserver.py\""
> sc.exe start "ServiceName"
我无论尝试使用 sc.exe
还是通过 services.msc
GUI 启动服务,都会不断收到此错误:
[SC] StartService FAILED 1053:
The service did not respond to the start or control request in a timely fashion.
据我了解,发生这种情况是因为 python.exe
没有实现 Windows 服务API。
我不希望使用 py2exe 从脚本创建 .exe
。
我发现这个答案建议使用与 sc.exe
不同的工具安装该服务,称为 srvany.exe
& instsrv.exe
。但是,我在 Win2K 资源工具包网站。
有谁知道如何安装和作为 Windows 成功启动此 .py
吗?
有谁知道吗
I am trying to install and start a simple CherryPy server as a Windows service.
Here is the script: (Removed some lines to cut it short. It's fully working when executing manually from the command-line)
app = AdminMediaHandler(django.core.handlers.wsgi.WSGIHandler())
logged_app = TransLogger(app)
server = wsgiserver.CherryPyWSGIServer( ('127.0.0.1', 8632), logged_app, server_name='localhost', numthreads=20 )
try:
server.start()
except KeyboardInterrupt:
server.stop()
I'm using sc.exe
to install and start the service. Installation goes fine, but I can't seem to start the service.
The command used is: (note there're spaces in the paths, though I'm handeling this with double-quotes, and the binPath
is working when executing its string manually through the command-line)
> sc.exe create "ServiceName" binPath= "\"C:\Path to Python\python.exe\" \"C:\Path to CherryPy Script\cherryserver.py\""
> sc.exe start "ServiceName"
I keep getting this error, no matter if attempting to start the service using sc.exe
or through services.msc
GUI:
[SC] StartService FAILED 1053:
The service did not respond to the start or control request in a timely fashion.
From what I understand, this is happenning because python.exe
doesn't implement the Windows Service API.
I do not wish to create an .exe
from the script, using py2exe.
I have found this answer that suggests to install the service using different tools than sc.exe
, called srvany.exe
& instsrv.exe
. However, I can't find them in the Win2K Resource Kit website.
Does anybody know how to install & start this .py
as a Windows succesfully?
Does anybody know
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
CherryPy 附带作为 Windows 服务启动的模块。请参阅这个其他问题 有关如何安装和运行它的说明。您可能想要从当前的方法(将 Django 应用程序直接传递到 WSGIServer)切换并使用 cherrypy.tree.graft 代替。
CherryPy ships with a module for starting as a Windows service. See this other SO question for instructions on how to install and run it. You'll probably want to switch from your current approach (of passing the Django app directly to the WSGIServer) and use cherrypy.tree.graft instead.
我更喜欢 nssm 将普通脚本安装为服务。
您可以根据您的系统将nssm.exe复制到C:\Windows\system32或C:\Windows\SysWOW64目录中。之后,您可以按如下方式安装服务:
对于 python 脚本,您必须将应用程序路径设置为 python.exe,参数是您的脚本自身。
用于启动/停止/编辑服务的其他常见命令是:
I prefer nssm for installing normal scripts as a service.
You can copy the nssm.exe in the C:\Windows\system32 or C:\Windows\SysWOW64 directory depending on your system. After that you are able to install a Service as follow:
For a python script you must set the application path to your python.exe and the argument is your script self.
Other common commands for start/stop/edit your service are:
我最终使用了ServiceInstaller又名SMaster,作为此答案中所述。给定答案中的 URL 已损坏,我找不到有效的 URL。我事先就在本地提供了 srunner.exe。
请注意,还有另一个障碍需要克服,因为 ServiceInstaller 无法处理路径中带有空格的文件。
因此,我使用旧的 DOS 路径格式来进行服务注册。
我没有注册
C:\Program Files\MyApp\python.exe
,而是注册了C:\PROGRA~1\MyApp\python.exe
。I eventually used ServiceInstaller aka SMaster, as stated in this answer. The URL in the given answer is broken, and I couldn't find a working URL. I just had
srunner.exe
available locally beforehand.Note there was another obstacle to overcome though, as ServiceInstaller can't handle files with spaces in their paths.
So, I used the old DOS path formatting for service registration.
Instead of registering
C:\Program Files\MyApp\python.exe
, I registeredC:\PROGRA~1\MyApp\python.exe
.