在Windows上将python脚本作为服务运行时,权限被拒绝读取文件

时间:2017-11-30 11:04:20

标签: python winapi windows-services

我正在尝试在python中运行其中一个项目作为Windows服务。它可以作为前台进程和服务运行。当我们将它作为前台进程运行时,一切正常。但是当我们将它作为服务运行时,它会为前台进程甚至使用的文件配置文件提供权限被拒绝错误。 是的我以管理员身份运行它。 以下是Windows服务的代码:

# Python script to run tevico_scanner as a windows service
# We are using pywin32 python extension modules for windows.

import servicemanager
import win32event
import win32service
import win32serviceutil

from tevico_scanner import constants
from tevico_scanner import watchman


class WindowsService(win32serviceutil.ServiceFramework):

    # Service name that we use to start/stop the service
    _svc_name_ = "<service_name>"

    # Service Display Name
    _svc_display_name_ = "<service_display_name>"

    # Description for our scanner
    _svc_service_description_ = "<description>"

    config_file = constants.DEFAULT_CFG_FILE

    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.wman = watchman.Watchman(self.config_file)

        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

    def SvcDoRun(self):
        # Run our watchman
        watchman._CW_FLAG = False
        watchman._DEBUGGING = False
        self.wman.run()

    def SvcDoStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)


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

错误讯息:

Python could not construct the class instance 
Traceback (most recent call last):
  File "C:\Users\Administrator\scanner-agent-7\scripts\bin\windows_service.py", line 28, in __init__
    self.wman = watchman.Watchman(self.config_file)
  File "C:\Python27\lib\site-packages\tevico_scanner-0.6-py2.7.egg\tevico_scanner\watchman.py", line 80, in __init__
    self.config = config.Config(self.config_file)
  File "C:\Python27\lib\site-packages\tevico_scanner-0.6-py2.7.egg\tevico_scanner\config.py", line 26, in __init__
    self.load_from_file(config_file)
  File "C:\Python27\lib\site-packages\tevico_scanner-0.6-py2.7.egg\tevico_scanner\config.py", line 36, in load_from_file
    with open(config_file, 'r') as fh:
IOError: (13, 'Permission denied', 
'C:\\Users\\Administrator\\tevico_scanner\\etc\\config\\scanner.cfg') 
%2: %3

感谢。

0 个答案:

没有答案