python 管理windows客户端_python管理Windows服务

上一篇介绍了pywin32模块,它的win32service子模块提供了对服务管理API的包装,相关API如下:

ChangeServiceConfig

ChangeServiceConfig2

CloseServiceHandle

ControlService

CreateDesktop

CreateService

CreateWindowStation

DeleteService

EnumDependentServices

EnumServicesStatus

EnumServicesStatusEx

EnumWindowStations

GetProcessWindowStation

GetServiceDisplayName

GetServiceKeyName

GetThreadDesktop

GetUserObjectInformation

LockServiceDatabase

OpenDesktop

OpenInputDesktop

OpenSCManager

OpenService

OpenWindowStation

QueryServiceConfig

QueryServiceConfig2

QueryServiceLockStatus

QueryServiceObjectSecurity

QueryServiceStatus

QueryServiceStatusEx

SetServiceObjectSecurity

SetServiceStatus

SetUserObjectInformation

StartService

UnlockServiceDatabase

有了这些API,完成一些服务的基本操作,比如安装服务、停止服务、启动服务等完全不在话下,只不过都是直接调用API而已。

不过pywin32还有一个对win32service模块的包装,叫做 win32serviceutil,使用它可以更方便地对服务进行控制。

InstallService(pythonClassString, serviceName, displayName, startType =None,

errorControl= None, bRunInteractive = 0, serviceDeps = None, userName =None,

password= None, exeName = None, perfMonIni = None,perfMonDll =None,

exeArgs= None,description = None, delayedstart = None) #安装服务

ChangeServiceConfig(pythonClassString, serviceName, startType=None,

errorControl= None, bRunInteractive = 0, serviceDeps =None,

userName= None, password = None,exeName = None, displayName =None,

perfMonIni= None, perfMonDll = None,exeArgs =None,

description= None, delayedstart = None) #更改服务设置

#以上两个函数的第一个参数pythonClassString是为Python编写的Windows服务准备的,非Python服务则不需要,直接填None即可

RemoveService(serviceName)#删除服务

ControlService(serviceName, code, machine = None) #控制服务,code的具体定义参考MSDN的对ControlService的介绍

StopService(serviceName, machine = None) #停止服务

StartService(serviceName, args = None, machine = None) #启动服务

RestartService(serviceName, args = None, waitSeconds = 30, machine = None) #重启服务

QueryServiceStatus(serviceName, machine=None) #查询服务状态

大部分API只需要提供服务名称就可以了,调用起来非常简单。

比如 win32serviceutil.StopService("Spooler") 就可以停止Spooler服务了。

对于 win32serviceutil.QueryServiceStatus()这个函数,它实际返回的是win32service.QueryServiceStatus的返回结果。

从MSDN和pywin32的帮助文件可知,win32service.QueryServiceStatus的返回值是一个元组,如下:

SERVICE_STATUS Object

A Win32 service status object is represented by a tuple:

Items

[0] int : serviceType

The type of service.

[1] int : serviceState

The current state of the service.

[2] int : controlsAccepted

The controls the service accepts.

[3] int : win32ExitCode

The win32 error code for the service.

[4] int : serviceSpecificErrorCode

The service specific error code.

[5] int : checkPoint

The checkpoint reported by the service.

[6] int : waitHint

The wait hint reported by the service

服务状态实际上是第1个元素(从0开始计数)

写个函数包装一下,并附带上控制功能:

importwin32serviceimportwin32serviceutilimportwin32apidefGetSvcStatus(svcname):

svcstatusdic={#The service continue is pending.

win32service.SERVICE_CONTINUE_PENDING:"continue pending",#The service pause is pending.

win32service.SERVICE_PAUSE_PENDING:"pause pending",#The service is paused.

win32service.SERVICE_PAUSED:"paused",#The service is running.

win32service.SERVICE_RUNNING:"running",#The service is starting.

win32service.SERVICE_START_PENDING:"start pending",#The service is stopping.

win32service.SERVICE_STOP_PENDING:"stop pending",#The service is not running.

win32service.SERVICE_STOPPED:"stoped"}

status=win32serviceutil.QueryServiceStatus(svcname)ifstatus:return svcstatusdic.get(status[1],"unknown")else:return "error"svcname= "Spooler"status=GetSvcStatus(svcname)print("{} current status : {}".format(svcname,status))if status == "running":

win32serviceutil.StopService(svcname)else:

win32serviceutil.StartService(svcname)

win32api.Sleep(1000)

status=GetSvcStatus(svcname)print("After Control,{} current status : {}".format(svcname,status))

输出结果如下:

2c9c3de65e35eabba92597d1f53b0423.png

掌握该模块的使用,对Windows平台的运维管理还是有相当帮助的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值