Python 3 executable as windows service

后端 未结 3 1257
孤街浪徒
孤街浪徒 2021-02-05 19:33

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. I\'ve successfully created a service like th

相关标签:
3条回答
  • 2021-02-05 20:21

    It appears Win32Service was updated with Python 3.x support within the cx_Freeze project as a result of this thread. 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. This executable should be built when cx_Freeze gets built/installed.

    If it's not too much to ask, can search the installed cx_Freeze installation directory for "Win32Service.exe" and confirm that it exists. Hopefully this gets you one step closer.

    0 讨论(0)
  • 2021-02-05 20:32

    tldr Python 3.5 Windows Service build with pyinstaller : gist

    Here a simple Windows Service with 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 :

    (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.

    Stop and clean

    (env) dist\WindowsService.exe stop
    (env) dist\WindowsService.exe remove
    
    0 讨论(0)
  • 2021-02-05 20:35

    I've edited the Win32Service.c in the cx_freeze src for python3 support

    Edited it with some preprocessor commands, for python2 support too (but not tested for python2 yet)

    1. download sources of cx_freeze from pypi and extract it
    2. download cx_logging and extract it to the cx_freeze-4.3.4 directory
    3. replace the Win32Service.c file in cx_Freeze-4.3.4\source\bases with my edited version
    4. edit line 170 in setup.py from if moduleInfo is not None and sys.version_info[:2] < (3, 0): to if moduleInfo is not None:
    5. run python setup.py build in cx_freeze-4.3.4 directory (you must have MSC installed to compile the C source files)
    6. copy 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)
    7. now you can create frozen exe with the Win32Service base (no more the cx_Freeze.freezer.ConfigError: no base named Win32Service error)
    0 讨论(0)
提交回复
热议问题