posts - 431,  comments - 344,  trackbacks - 0

          首先必須清楚, 在使用python來創建windows服務, 需要使用pywin32類庫, 下載地址:http://sourceforge.net/projects/pywin32/
          下面我們創建一個什么也不做的服務,代碼如下:
          win32test.py

          import win32serviceutil
          import win32service
          import win32event

          class win32test(win32serviceutil.ServiceFramework):
              _svc_name_ = "Python Win32 Service"
              _svc_display_name_ = "Python Win32 Service"
              _svc_description_ = "Just for a demo, do nothing."
              def __init__(self, args):
                  win32serviceutil.ServiceFramework.__init__(self, args)
                  self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
                 
              def SvcDoRun(self):
                  win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
                 
              def SvcStop(self):
                  self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
                  win32event.SetEvent(self.hWaitStop)
             
          if __name__=='__main__':
              win32serviceutil.HandleCommandLine(win32test)

          如果你想使用這個服務在啟動或者停用時候做點事, 那么就把你要實現的業務寫給對應的方法里SvcDoRun和SvcStop.

          下面我們來使用py2exe來把我們上面的service創建成安裝文件, py2exe下載地址:http://sourceforge.net/projects/py2exe/
          setup.py

          from distutils.core import setup
          import py2exe

          class Target:
              def __init__(self, **kw):
                  self.__dict__.update(kw)
                  # for the versioninfo resources
                  self.version = "1.0.0"
                  self.company_name = "Founder Software Suzhou Co. Ltd."
                  self.copyright = "Copyright © 2009 Founder Software (Suzhou) Co., Ltd. "
                  self.name = "Jchem cartridge windows service"


          myservice = Target(
              description = 'foo',
              modules = ['win32test'],
              cmdline_style='pywin32',
              icon_resources=[(1, "cartrigde.ico")]
          )

          options = {"py2exe":  
                      {   "compressed": 1,  
                          "bundle_files": 1
                      }  
                    } 
                   
          setup(
              service=[myservice],
              options = options,
              zipfile = None,
              windows=[{"script": "win32test.py"}],
          )


          下面就可以使用python setup.py py2exe命令來生成安裝文件了.
          生成的安裝文件可以使用一下命令來執行控制服務:
          win32test.exe install
          win32test.exe start
          win32test.exe stop
          win32test.exe remove

          posted on 2009-06-28 12:24 周銳 閱讀(1944) 評論(0)  編輯  收藏 所屬分類: Python
          主站蜘蛛池模板: 四川省| 平凉市| 府谷县| 尼木县| 四川省| 永新县| 石楼县| 龙门县| 白山市| 开封县| 四川省| 鸡泽县| 宾阳县| 棋牌| 高清| 鹤峰县| 平南县| 广东省| 云梦县| 揭东县| 蚌埠市| 堆龙德庆县| 南安市| 科技| 榕江县| 泌阳县| 富锦市| 德令哈市| 长顺县| 潜山县| 香格里拉县| 巴南区| 万安县| 日喀则市| 赣州市| 仲巴县| 汉阴县| 喀什市| 景谷| 天镇县| 阿尔山市|