[英]Python 3 executable as windows service
I'm trying to write a windows Service in python, but the tricky part is i want to deploy it on a machine that doesn't have python. 我正在尝试在python中编写一个Windows服务,但棘手的部分是我想将它部署在没有python的机器上。 I've successfully created a service like this , and it works if i run from my machine. 我已经成功地创建了像服务这样的,如果我从我的机器上运行它的工作原理。 the problem starts when i try to convert it to an exe and then try to install it. 当我尝试将其转换为exe然后尝试安装它时,问题就开始了。 first i tried to use cx_freeze service example, (seen here ), the setup.py look like this : 首先我尝试使用cx_freeze服务示例(在这里看到),setup.py看起来像这样:
from cx_Freeze import setup, Executable
options = {'build_exe': {'includes': ['ServiceHandler']}}
executables = [Executable('Config.py', base='Win32Service', targetName='gsr.exe')]
setup(name='GSR',
version='0.1',
description='GSR SERVICE',
executables=executables,
options=options
)
and config.py is: 和config.py是:
NAME = 'GSR_%s'
DISPLAY_NAME = 'GSR TEST - %s'
MODULE_NAME = 'ServiceHandler'
CLASS_NAME = 'Handler'
DESCRIPTION = 'Sample service description'
AUTO_START = True
SESSION_CHANGES = False
but when i try to build it (python setup.py build) i get an error: "cx_Freeze.freezer.ConfigError: no base named Win32Service" 但是当我尝试构建它(python setup.py build)时,我收到一个错误:“cx_Freeze.freezer.ConfigError:没有名为Win32Service的基地”
Second, i tried using a regular cx_freeze setup, the exe i get installs the service fine but once i try to start it i get an error: "Error 1053: The service did not respond to the start or control request in a timely fashion" 其次,我尝试使用常规cx_freeze设置,exe我得到安装服务罚款,但一旦我尝试启动它我得到一个错误:“错误1053:服务没有及时响应启动或控制请求”
setup.py - python 3.3 regualr exe, installs the service but when trying to start it sends an error: setup.py - python 3.3 regualr exe,安装服务,但在尝试启动时发送错误:
from cx_Freeze import setup, Executable
packages = ['win32serviceutil','win32service','win32event','servicemanager','socket','win32timezone','cx_Logging','ServiceHandler']
build_exe_options = {"packages": packages}
executable = [Executable("ServiceHandler.py")]
setup( name = "GSR_test",
version = "1.0",
description = "GSR test service",
options = {"build_exe": build_exe_options},
executables = executable)
finally, I managed to get it to work in python 2.7 using py2exe, but py2exe isn't available for python 3.3 and I my code is in 3.3 最后,我设法使用py2exe在python 2.7中使用它,但py2exe不适用于python 3.3而且我的代码在3.3中
i guess the problem is in the configuration of the setup.py im using with cx_freeze. 我想问题是在使用cx_freeze的setup.py im的配置中。 any ideas ?? 有任何想法吗 ??
my ServiceHandler: 我的ServiceHandler:
import pythoncom
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
from test import test
import threading
class AppServerSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "GSR_test"
_svc_display_name_ = "GSR test Service"
def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.hWaitStop = win32event.CreateEvent(None,0,0,None)
socket.setdefaulttimeout(60)
self.app = test()
self.flag = threading.Event()
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
self.flag.set()
def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,''))
self.main()
def main(self):
t = threading.Thread(target=self.app.run)
t.start()
self.flag.wait()
raise SystemExit
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(AppServerSvc)
Setup.py , python 2.7 using py2exe that works: (taken from here ) Setup.py,python 2.7使用py2exe工作:(取自这里 )
from distutils.core import setup
import py2exe
setup( service = ["ServiceHandler"],
description = "SERVICE TEST",
modules = ["GSR_test"],
cmdline_style='pywin32', )
Thanks, Amit 谢谢,阿米特
tldr Python 3.5 Windows Service build with pyinstaller : gist tldr Python 3.5使用pyinstaller进行Windows Service构建: gist
Here a simple Windows Service with python : 这里有一个简单的Windows服务与python:
WindowsService.py
import servicemanager
import socket
import sys
import win32event
import win32service
import win32serviceutil
class TestService(win32serviceutil.ServiceFramework):
_svc_name_ = "TestService"
_svc_display_name_ = "Test Service"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
socket.setdefaulttimeout(60)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
rc = None
while rc != win32event.WAIT_OBJECT_0:
with open('C:\\TestService.log', 'a') as f:
f.write('test service running...\n')
rc = win32event.WaitForSingleObject(self.hWaitStop, 5000)
if __name__ == '__main__':
if len(sys.argv) == 1:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(TestService)
servicemanager.StartServiceCtrlDispatcher()
else:
win32serviceutil.HandleCommandLine(TestService)
Create an exe with pyinstaller ( pip install pyinstaller
) and python 3.5 : 使用pyinstaller( pip install pyinstaller
)和python 3.5创建一个exe:
(env)$ python -V
Python 3.5.2
(env)$ pip freeze
PyInstaller==3.2
(env)$ pyinstaller -F --hidden-import=win32timezone WindowsService.py
Install and run the service 安装并运行该服务
(env) dist\WindowsService.exe install
Installing service TestService
Service installed
(env) dist\WindowsService.exe start
Starting service TestService
Check C:\\\\TestService.log
file. 检查C:\\\\TestService.log
文件。
Stop and clean 停下来打扫卫生
(env) dist\WindowsService.exe stop
(env) dist\WindowsService.exe remove
It appears Win32Service was updated with Python 3.x support within the cx_Freeze project as a result of this thread . 由于此线程,在cx_Freeze项目中使用Python 3.x支持更新了Win32Service。 This user originally had the same issue you reported, so I'm assuming this will also resolve your issue. 此用户最初遇到了您报告的相同问题,因此我假设这也将解决您的问题。
Based on the error reported, it's caused when _GetBaseFileName() within Freezer.py fails to find the compiled Win32Service.exe. 根据报告的错误,当Freezer.py中的_GetBaseFileName()无法找到已编译的Win32Service.exe时,会导致该错误。 This executable should be built when cx_Freeze gets built/installed . 应该在构建/安装 cx_Freeze时构建此可执行文件。
If it's not too much to ask, can search the installed cx_Freeze installation directory for "Win32Service.exe" and confirm that it exists. 如果要求不是太多,可以在已安装的cx_Freeze安装目录中搜索“Win32Service.exe”并确认它是否存在。 Hopefully this gets you one step closer. 希望这能让您更近一步。
I've edited the Win32Service.c
in the cx_freeze
src for python3
support 我在cx_freeze
src中编辑了Win32Service.c
以获得python3
支持
Edited it with some preprocessor commands, for python2 support too (but not tested for python2 yet) 使用一些预处理器命令编辑它,也支持python2(但尚未测试python2)
cx_freeze
from pypi and extract it 从pypi下载cx_freeze
源代码并解压缩 cx_freeze-4.3.4
directory 下载cx_logging并将其解压缩到cx_freeze-4.3.4
目录 Win32Service.c
file in cx_Freeze-4.3.4\\source\\bases
with my edited version 用我编辑的版本替换cx_Freeze-4.3.4\\source\\bases
的Win32Service.c
文件 setup.py
from if moduleInfo is not None and sys.version_info[:2] < (3, 0):
to if moduleInfo is not None:
编辑setup.py
第170行, if moduleInfo is not None and sys.version_info[:2] < (3, 0):
if moduleInfo is not None:
if moduleInfo is not None and sys.version_info[:2] < (3, 0):
if moduleInfo is not None:
python setup.py build
in cx_freeze-4.3.4
directory (you must have MSC installed to compile the C source files) 在cx_freeze-4.3.4
目录中运行python setup.py build
(必须安装MSC才能编译C源文件) cx_Freeze-4.3.4\\build\\lib.win32-3.4\\cx_Freeze\\bases\\Win32Service.exe
to C:\\Python34\\Lib\\site-packages\\cx_Freeze\\bases
(or the path where your python3 is installed) 将cx_Freeze-4.3.4\\build\\lib.win32-3.4\\cx_Freeze\\bases\\Win32Service.exe
到C:\\Python34\\Lib\\site-packages\\cx_Freeze\\bases
(或安装python3的路径) Win32Service
base (no more the cx_Freeze.freezer.ConfigError: no base named Win32Service
error) 现在你可以使用Win32Service
基础创建冻结的exe(不再是cx_Freeze.freezer.ConfigError: no base named Win32Service
错误)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.