繁体   English   中英

Python Win32服务

[英]Python win32 service

我对python相当陌生,并且没有编写Windows服务的经验。 我试图根据我在那儿找到的一些教程来一起破解Windows服务。

我需要此服务来不断监视目录中的更改,并在看到更改时运行脚本。 这是我到目前为止所拥有的:

import win32service
import win32serviceutil
import time

class aservice(win32serviceutil.ServiceFramework):
    _svc_name_ = "aservice"
    _svc_display_name_ = "aservice - It Does nothing"

    def __init__(self,args):
        win32serviceutil.ServiceFramework.__init__(self,args)
        self.isAlive = True

    def SvcDoRun(self):
        import servicemanager
        while self.isAlive:
            # This is where i am trying to run my code...is that right?
            self.main()
            #servicemanager.LogInfoMsg("aservice - is alive and well")
            #time.sleep(5)
            #servicemanager.LogInfoMsg("aservice - Stopped")

    def SvcStop(self):
        import servicemanager
        servicemanager.LogInfoMsg("aservice - Recieved stop signal")
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        self.isAlive = False #this will make SvcDoRun() break the while loop at the next iteration.

    # This code monitors a directory for changes and calls a script when there is a change
    # At the moment, it creates a log file instead of calling the script
    def main(self):
        import os
        import win32file
        import win32con
        ACTIONS = {
            1: "Created",
            2: "Deleted",
            3: "Updated",
            4: "Renamed from something",
            5: "Ranamed to something"
        }

        FILE_LIST_DIRECTORY = 0x0001

        path_to_watch = "folder"
        hDir = win32file.CreateFile (
            path_to_watch,
            FILE_LIST_DIRECTORY,
            win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
            None,
            win32con.OPEN_EXISTING,
            win32con.FILE_FLAG_BACKUP_SEMANTICS,
            None
        )
        while 1:
            results = win32file.ReadDirectoryChangesW (
                hDir,
                1024,
                True,
                win32con.FILE_NOTIFY_CHANGE_FILE_NAME |
                win32con.FILE_NOTIFY_CHANGE_DIR_NAME |
                win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES |
                win32con.FILE_NOTIFY_CHANGE_SIZE |
                win32con.FILE_NOTIFY_CHANGE_LAST_WRITE |
                win32con.FILE_NOTIFY_CHANGE_SECURITY,
                None,
                None
            )
            for action, file in results:
                #import script
                log = open ('log.txt', 'w')
                full_filename = os.path.join(path_to_watch)
                log.write(full_filename + ACTIONS.get(action, "Unknown"))
                log.close()

if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(aservice)

关于我在做什么错的任何想法吗?

编辑:这是我正在获取的事件日志错误。

Event Type: Error
Event Source:   aservice
Event Category: None
Event ID:   3
Date:       21/01/2010
Time:       11:27:43 AM
User:       N/A
Computer:
Description:
The instance's SvcRun() method failed 
Traceback (most recent call last):
  File "C:\Python25\Lib\site-packages\win32\lib\win32serviceutil.py", line 806, in SvcRun
    self.SvcDoRun()
  File "D:\TEST\simpleservice2.py", line 35, in SvcDoRun
    self.main()
  File "D:\TEST\simpleservice2.py", line 72, in main
    None
error: (2, 'CreateFile', 'The system cannot find the file specified.') 
%2: %3

Windows服务要记住的一件事是,默认情况下,它们在LOCAL_SYSTEM帐户下运行。 这意味着将应用对本地卷的权限(肯定可以拒绝LOCAL_SYSTEM访问任何给定的文件夹),并且LOCAL_SYSTEM对任何网络卷都没有访问权限。

我不熟悉pywin32的ServiceFramework ,但根据您的代码和错误消息,我敢打赌您path_to_watch遇到问题。

我假设您已经为示例清理了文件夹名称,而您实际上并不是在寻找folder

怎么样:

  • 您是否正在使用文件夹的完整路径? 服务的当前工作目录可能与您期望的不一样。
  • 您是否在文件夹路径中转义了任何反斜杠?

尝试使用:

path_to_watch = r"c:\foo\bar" + "\\" 

暂无
暂无

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

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