0

私はすでに他の人が投稿したこの質問の解決策を見て、試しました。あるユーザーは、setup.pyファイルを次の場所から変更しようとしています。

from distutils.core import setup
import py2exe

setup(console=["dev.py"])

from distutils.core import setup
import py2exe

setup(service=["dev.py"])

次の結果が得られました。

running py2exe
*** searching for required modules ***
Traceback (most recent call last):
  File "C:\Python27\Scripts\distutils-setup.py", line 5, in <module>
  setup(service=["C:\Python27\Scripts\dev.py"])
File "C:\Python27\lib\distutils\core.py", line 152, in setup
  dist.run_commands()
File "C:\Python27\lib\distutils\dist.py", line 953, in run_commands
  self.run_command(cmd)
File "C:\Python27\lib\distutils\dist.py", line 972, in run_command
  cmd_obj.run()
File "C:\Python27\lib\site-packages\py2exe\build_exe.py", line 243, in run
  self._run()
File "C:\Python27\lib\site-packages\py2exe\build_exe.py", line 296, in _run
  self.find_needed_modules(mf, required_files, required_modules)
File "C:\Python27\lib\site-packages\py2exe\build_exe.py", line 1274, in
find_needed_modules
  mf.import_hook(mod)
File "C:\Python27\lib\site-packages\py2exe\mf.py", line 719, in import_hook
  return Base.import_hook(self,name,caller,fromlist,level)
File "C:\Python27\lib\site-packages\py2exe\mf.py", line 136, in import_hook
  q, tail = self.find_head_package(parent, name)
File "C:\Python27\lib\site-packages\py2exe\mf.py", line 204, in find_head_package
  raise ImportError, "No module named " + qname
ImportError: No module named dev

セットアップスクリプトで「console」を使用してpy2exeを実行すると、正常に機能しますが、サービスが開始されず、エラーが発生します。セットアップスクリプトで「service」を指定してpy2exeを実行すると、py2exeが実行されず、モジュールが見つからないと表示されます。

py2exeを解決せずに再インストールしようとしました。私も変更しようとしました:

def SvcDoRun(self):
    servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                          servicemanager.PYS_SERVICE_STARTED,
                          (self._svc_name_,''))

def  SvcDoRun(self):
     self.ReportServiceStatus(win32service.SERVICE_RUNNING)
     win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)

違いもありませんでした。誰かが私を助けてくれますか?これが私が取り組んでいることです。サーバーを監視し、60秒ごとにテキストファイルを吐き出します。このファイルを使用して、任意の1分にサーバーを監視します。あなたたちとギャルが与えることができるどんな助けも素晴らしいでしょう。

import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import wmi
import _winreg
from time import sleep
import os

class SrvMonSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "SrvMonSvc"
_svc_display_name_ = "Server Monitor"

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):
    servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                          servicemanager.PYS_SERVICE_STARTED,
                          (self._svc_name_,''))
    self.main()

def main(self):
    host = wmi.WMI(namespace="root/default").StdRegProv
    try:
        result, api = host.GetStringValue(
        hDefKey = _winreg.HKEY_LOCAL_MACHINE,
        sSubKeyName = "SOFTWARE\Server Monitor",
        sValueName = "API")
        if api == None:
            raise Exception
        else:
            pass
    except:
        exit()

    while 1 == 1:
        with open("C:/test.txt", "wb") as b:
            computer = wmi.WMI(computer="exsan100")
            for disk in computer.Win32_LogicalDisk (DriveType=3):
                name = disk.caption
                size = round(float(disk.Size)/1073741824, 2)
                free = round(float(disk.FreeSpace)/1073741824, 2)
                used = round(float(size), 2) - round(float(free), 2)
                for mem in computer.Win32_OperatingSystem():
                    a_mem = (int(mem.FreePhysicalMemory)/1024)
                for me in computer.Win32_ComputerSystem():
                    t_mem = (int(me.TotalPhysicalMemory)/1048576)
                    u_mem = t_mem - a_mem
                for cpu in computer.Win32_Processor():
                    load = cpu.LoadPercentage
                print >>b, api
                print >>b, name
                print >>b, size
                print >>b, used
                print >>b, t_mem
                print >>b, u_mem
                print >>b, load
        b.close()
        date_list = []
        stamp = time.strftime("%c",time.localtime(time.time()))
        date_list.append(stamp)
        name = re.sub(r"[^\w\s]", "",date_list[0])
        os.rename("C:/test.txt", ("C:/%s.txt" % name))

        try:
            sleep(60.00)
        except:
            exit()

if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(SrvMonSvc)
4

1 に答える 1

2

元の問題から進歩しましたか。Pythonサービスでも同様の問題が発生し、「システムパス」(ユーザーパスではない)が完全ではなかったため、DLLが欠落していることがわかりました。

正しいPATH環境変数を使用しているため、コマンドプロンプトから-debugを指定してpythonservice.exeを実行しても問題はありませんでしたが、サービスがシステムサービスとしてインストールされている場合は、システムパス変数に必要なDLLのすべてのパスがあるかどうかを確認する価値があります(MSVC、Python、System32)。私にとっては、Python DLLパスがありませんでしたが、その後は再び機能しました。

于 2013-01-29T10:11:22.557 に答える