如何在python中指定windows服务
我正在编写一个用python启动和停止windows服务的脚本。到目前为止,我已经将其设置为可以打开/关闭任何列出的服务,但是如果列出的服务无效,我将尝试进行循环。我在计算机上创建了一个包含所有列出的服务的文本文件。我正在尝试创建一个检查是否是一个真正的服务。我的启动/停止服务语法是如何在python中指定windows服务,python,Python,我正在编写一个用python启动和停止windows服务的脚本。到目前为止,我已经将其设置为可以打开/关闭任何列出的服务,但是如果列出的服务无效,我将尝试进行循环。我在计算机上创建了一个包含所有列出的服务的文本文件。我正在尝试创建一个检查是否是一个真正的服务。我的启动/停止服务语法是 choice = input (" Would you like to start or stop a service : ") if choice == "stop" : stopping = 1
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
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")
enter a service : okay
word found
enter a service : spooler
word found
enter a service : anifd
no word
enter a service : aeiou
no word
enter a service : shpamsvc
word found
而我的支票是
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
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")
enter a service : okay
word found
enter a service : spooler
word found
enter a service : anifd
no word
enter a service : aeiou
no word
enter a service : shpamsvc
word found
无论何时我使用该检查,它在技术上都是有效的,但是如果我键入一个字符,它将被列为正确的,因为该字符可以在任何服务中找到。例如
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
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")
enter a service : okay
word found
enter a service : spooler
word found
enter a service : anifd
no word
enter a service : aeiou
no word
enter a service : shpamsvc
word found
当我输入一个普通字符时
enter a service : a
word found
我希望脚本能够判断它是否是有效的服务,只接受有效的服务输入。例如
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
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")
enter a service : okay
word found
enter a service : spooler
word found
enter a service : anifd
no word
enter a service : aeiou
no word
enter a service : shpamsvc
word found
方法1:
将服务列表存储在阵列中
services=['hello','world']
服务中的“你好”
>真的
“他”在服役
>假的
如果正在读取的文本文件中有服务列表,请尝试使用string方法创建一个要使用的数组
方法2:
在当前代码中添加一个
如果输入(“您想启动或停止服务吗?”:”。下一个()是“停止”:
尽管如此:
服务名称=输入('要停止的服务:')
尝试:
打印(“服务正在停止…”)
win32serviceutil.StopService(服务名称)
打印(“服务已停止”)
打破
除:
打印(“无效服务”)
您是否尝试在计算机中获取所有服务的名称并将其放入列表中,然后查看其中是否存在输入?这就是我所做的。我将其设置为打印列表中所有服务名称的位置,名称为Windows服务列表。但是,是的,问题还在继续发生,我得到了一个想法,使用批处理文件来简化这个过程,我正在尝试为什么不使用psutil
?这肯定也可以。我只是想在这里使用win32serviceutil模块。您好,谢谢您的想法。如果我没有293个服务可以输入,这可能会起作用。但我肯定会把这当作最后的选择,其他给我的想法都不管用。例外情况非常好,非常感谢。