首页 技术 正文
技术 2022年11月14日
0 收藏 904 点赞 2,300 浏览 3454 个字

上一篇介绍了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开始计数)

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

import win32service
import win32serviceutil
import win32apidef GetSvcStatus(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)
if status:
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))

输出结果如下:

python管理Windows服务

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

上一篇: TPS与QPS
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,490
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,905
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,738
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,491
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,129
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,292