[英]How do you specify a windows service in python
I am writing a script that starts and stops windows services in python.我正在编写一个在 python 中启动和停止 Windows 服务的脚本。 So far I've set it to where I can turn on/off any listed service, but I am attempting to have a loop if the listed service is invalid.
到目前为止,我已经将它设置为可以打开/关闭任何列出的服务,但是如果列出的服务无效,我会尝试进行循环。 I created a text file with all listed services on my computer.
我在我的计算机上创建了一个包含所有列出的服务的文本文件。 I am attempting to have create check on whether or not its a real service.
我正在尝试创建检查它是否是真正的服务。 My syntax for start/stop services would be
我的启动/停止服务语法是
choice = input (" Would you like to start or stop a service : ")
if choice == "stop" :
stopping = 1
while stopping == 1:
service_name = input (' Which service would you like to stop : ')
win32serviceutil.StopService(service_name)
print ("Service is stopping")
text = ". . . . . . "
for char in text:
sys.stdout.write(char)
time.sleep(0.5)
print ( "Service has stopped" )
break
And my attempt at a check would be我的支票尝试是
serviceList = r"C:\Users\ethanm\Desktop\Python Text Files\Windows
Services List.txt "
inFile = open(serviceList, 'r')
line = inFile.read()
serviceList = line.split()
print (serviceList)
serviceWord = input ("enter a service")
if serviceWord in open (r"C:\Users\ethanm\Desktop\Python Text
Files\Windows Services List.txt ").read():
print ("word found")
else:
print ("no word")
Whenever I use the check, it *technically * works, but if I type a character, it gets listed as correct because that character can be found in any of the services.每当我使用支票时,它*技术上*有效,但是如果我输入一个字符,它会被列为正确的,因为该字符可以在任何服务中找到。 An example would be
一个例子是
enter a service : okay
word found
And when I enter a normal character当我输入一个普通字符时
enter a service : a
word found
I would like the script to be able to tell whether or not it is a valid service, only accepting valid service inputs.我希望脚本能够判断它是否是一个有效的服务,只接受有效的服务输入。 An example would be
一个例子是
enter a service : spooler
word found
enter a service : anifd
no word
enter a service : aeiou
no word
enter a service : shpamsvc
word found
Store your list of services in an array.将您的服务列表存储在一个数组中。
services = ['hello', 'world']
'hello' in services
> True
'he' in services
> False
If you have your list of services in a text file that you are reading from, try using the .split() string method to create an array to work with.如果您在读取的文本文件中有服务列表,请尝试使用.split()字符串方法创建要使用的数组。
Add an exception to your current code.为您当前的代码添加一个例外。
if input("Would you like to start or stop a service? : ").lower() is "stop":
while True:
service_name = input ('Service to stop: ')
try:
print ("Service is stopping...")
win32serviceutil.StopService(service_name)
print("Service has stopped.")
break
except:
print("Invalid service.")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.