我是python的新手,并相信有一个简单的答案,但似乎无法弄明白。有没有办法在这个程序的开头做一个函数调用。正如您所看到的,我正在运行代码,然后要求再次运行它,这就是它现在正在执行函数调用的地方。这是按原样工作,但我想摆脱除了while循环和函数之外的所有东西,所以我可以清理代码。感谢。
import subprocess
import re
print ("This will print any service that is stopped or unknown \n")
#servers
server = str(input("Enter server to query(IP Address): "))
server_list = server.split(",")
server_count = len(server_list)
#services
service = str(input("Enter service to query: "))
service_list = service.split(",")
service_count = len(service_list)
#server loop
for a in range(len(server_list)):
server_net_use = 'net use'+r' \\'+server_list[a]+r'\ipc$ /user:Administrator itv'
server_disconnect = 'net use'+r' \\'+server_list[a]+r'\ipc$ /d'
server_sc = r'\\'+server_list[a]
subprocess.check_output(server_net_use, universal_newlines=True)
#service loop
for i in range(len(service_list)):
x = subprocess.check_output(['sc', server_sc, 'query', service_list[i]], universal_newlines=True)
y = re.sub('\s+',' ',x)
if 'FAILED' in y:
print ('\n'+server_list[a]+' - '+service_list[i]+' - Unknown Service!')
elif 'STOPPED' in y:
print ('\n'+server_list[a]+' - '+service_list[i]+' - '+y.split(" ")[10])
def repeat():
#servers
server = str(input("Enter server to query(IP Address): "))
server_list = server.split(",")
server_count = len(server_list)
#services
service = str(input("Enter service to query: "))
service_list = service.split(",")
service_count = len(service_list)
#server loop
for a in range(len(server_list)):
server_net_use = 'net use'+r' \\'+server_list[a]+r'\ipc$ /user:Administrator itv'
server_disconnect = 'net use'+r' \\'+server_list[a]+r'\ipc$ /d'
server_sc = r'\\'+server_list[a]
subprocess.check_output(server_net_use, universal_newlines=True)
#service loop
for i in range(len(service_list)):
x = subprocess.check_output(['sc', server_sc, 'query', service_list[i]], universal_newlines=True)
y = re.sub('\s+',' ',x)
if 'FAILED' in y:
print ('\n'+server_list[a]+' - '+service_list[i]+' - Unknown Service!\n')
elif 'STOPPED' in y:
print ('\n'+server_list[a]+' - '+service_list[i]+' - '+y.split(" ")[10]+'\n')
while(True):
a = str(input("Do you want to query another service? y/n: "))
if 'y' in a:
subprocess.check_output(server_disconnect, universal_newlines=True)
print ("\n")
repeat()
elif 'n' in a:
subprocess.check_output(server_disconnect, universal_newlines=True)
break;
else:
print ('Enter y/n')
答案 0 :(得分:3)
这是:
import subprocess
import re
def server_checker():
print ("This will print any service that is stopped or unknown \n")
#servers
server = str(input("Enter server to query(IP Address): "))
server_list = server.split(",")
server_count = len(server_list)
#services
service = str(input("Enter service to query: "))
service_list = service.split(",")
service_count = len(service_list)
#server loop
for a in range(len(server_list)):
server_net_use = 'net use'+r' \\'+server_list[a]+r'\ipc$ /user:Administrator itv'
server_disconnect = 'net use'+r' \\'+server_list[a]+r'\ipc$ /d'
server_sc = r'\\'+server_list[a]
subprocess.check_output(server_net_use, universal_newlines=True)
#service loop
for i in range(len(service_list)):
x = subprocess.check_output(['sc', server_sc, 'query', service_list[i]], universal_newlines=True)
y = re.sub('\s+',' ',x)
if 'FAILED' in y:
print ('\n'+server_list[a]+' - '+service_list[i]+' - Unknown Service!\n')
elif 'STOPPED' in y:
print ('\n'+server_list[a]+' - '+service_list[i]+' - '+y.split(" ")[10]+'\n')
if __name__ == "__main__":
server_checker()
while(True):
answer = str(input("Do you want to query another service? y/n: "))
if answer.lower() in ["yes","y","letsgo"]:
subprocess.check_output(server_disconnect, universal_newlines=True)
print ("\n")
server_checker()
elif answer.lower() in ["no","n,","nowayhosay"]:
subprocess.check_output(server_disconnect, universal_newlines=True)
break;
else:
print ('Enter y/n')
答案 1 :(得分:0)
实际上非常简单。只需尽快定义函数,然后在定义后立即调用它。
def saysomething(say):
print say
saysomething("Hello, World!")
如果一切顺利,你应该得到:
Hello, World!
如果没有,请检查错误。