从Windows系统服务python调用.exe?

时间:2012-08-19 19:55:00

标签: python windows

我正在尝试编写Windows系统服务。我正在尝试使用POS机的接口,所以理想情况下我想在系统服务中包含这些代码。然而,一些实验让我相信Windows系统服务只会执行基本任务而不是迭代。

我有另一个需要每x秒调用一次的函数,这个附加函数是一个while循环,但是我无法获得我的函数和win32循环来等待系统调用一起很好地运行。我将在下面的代码中详细介绍。

import win32service  
import win32serviceutil  
import win32event

class PySvc(win32serviceutil.ServiceFramework):  
    # net name  
    _svc_name_ = "test"  

    _svc_display_name_ = "test"  

    _svc_description_ = "Protects your computer."  

    def __init__(self, args):  
        win32serviceutil.ServiceFramework.__init__(self,args)  
        # create an event to listen for stop requests on  
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)


    # core logic of the service     
    def SvcDoRun(self):


        # if the stop event hasn't been fired keep looping
        while rc != win32event.WAIT_OBJECT_0:




            # block for 60 seconds and listen for a stop event  
            rc = win32event.WaitForSingleObject(self.hWaitStop, 60000)

        ## I want to put an additional function that uses a while loop here.
        ## The service will not work correctly with additional iterations, inside or 
        ## the above api calls.    
        ## Due to the nature of the service and the api call above, 
        ## this leads me to have to compile an additional .exe and somehow call that 
        ## from the service.     

    # called when we're being shut down      

    def SvcStop(self):  
            # tell the SCM we're shutting down  
            self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)  
            # fire the stop event  
            win32event.SetEvent(self.hWaitStop)  

if __name__ == '__main__':  

    win32serviceutil.HandleCommandLine(PySvc) 

我的研究表明,我需要以某种方式从Windows系统服务调用.exe。有谁知道如何做到这一点?我已经尝试使用os.system,并且子进程模块的变量调用无济于事,似乎windows只是忽略它们。有什么想法吗?

编辑:恢复原始问题

1 个答案:

答案 0 :(得分:0)

不能说因为我熟悉Windows开发,但在* nix中我发现套接字在两个东西不能按照定义说话但你无论如何需要它们的情况下非常有用。使Web浏览器启动桌面应用程序,使剪贴板与浏览器等交互。

在大多数情况下,UDP套接字都是一点IPC所需要的,而且在Python中编写代码非常简单。你必须要格外小心,通常有限制是有充分理由的,你需要在破坏它之前真正理解规则...记住任何人都可以发送UDP数据包以确保接收应用程序只接受来自localhost的数据包,并确保您检查所有传入的数据包,以防止本地黑客/恶意软件。如果传输的数据特别敏感或启动的动作很强大,那么根本不是一个好主意,只有你知道你的应用程序才能说得真实。

相关问题