Python pywin32和Python Snap7结合在一起,但没有名为“Snap7”的模块出现问题

2024-09-28 22:20:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建在本地计算机上运行的windows服务。它工作得很好,直到我将两个库pywin32snap7合并在一起,即消息'No module named snap7'

但是,如果我只有一个在我的系统上运行良好的库pywin32或snap7,那么我使用的是windows 7。我尝试在pywin32文件夹中移动snap7 dll和snap7 lib,但仍然不起作用

问题是,有可能运行这些库吗?代码有什么问题,或者我必须移动dll文件

代码

import win32serviceutil
import servicemanager
import win32service
import win32event
import logging
import snap7 # if include, I got error


class MyWindowsService(win32serviceutil.ServiceFramework):
    _svc_name_          = 'AITM'
    _svc_display_name_  = 'AITM'
    _svc_description_   = 'Service Full Description'
    logging.basicConfig(
        filename    = 'c:\\Temp\\{}.log'.format(_svc_name_),
        level       = logging.DEBUG,
        format      = '%(levelname)-7.7s @ %(asctime)s: %(message)s'
    )

    @classmethod
    def parse_command_line(cls):
        '''
        ClassMethod to parse the command line
        '''
        win32serviceutil.HandleCommandLine(cls)

    def __init__(self, *args):
        self.log('Initializing service {}'.format(self._svc_name_))
        win32serviceutil.ServiceFramework.__init__(self, *args)
        self.stop_event = win32event.CreateEvent(None, 0, 0, None)

    def SvcDoRun(self):
        self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
        try:
            self.log('START: Service start')
            self.ReportServiceStatus(win32service.SERVICE_RUNNING)
            self.start()
            win32event.WaitForSingleObject(self.stop_event, win32event.INFINITE)
        except Exception as e:
            self.log('Exception: {}'.format(e))
            self.SvcStop()

    def SvcStop(self):
        self.log('STOP: Service stopping...')
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        self.stop()
        win32event.SetEvent(self.stop_event)
        self.ReportServiceStatus(win32service.SERVICE_STOPPED)

    def log(self, msg):
        servicemanager.LogInfoMsg(str(msg))  #system log
        logging.info(str(msg))               #text log

    def start(self):
        pass

    def stop(self):
        self.runflag = False
        self.log('Stop received')


if __name__ == '__main__':
    MyWindowsService.parse_command_line()

Tags: nameimportselflogformatloggingdefservice