为了账号安全,请及时绑定邮箱和手机立即绑定

程序开始处的Python函数调用

程序开始处的Python函数调用

慕慕森 2021-03-18 17:13:19
我是python的新手,并且相信对此有一个简单的答案,但似乎无法弄清楚。在此程序的开头是否可以进行函数调用。如您所见,我正在运行代码,然后要求再次运行它,这是它现在执行函数调用的位置。这按原样工作,但我希望摆脱while循环和函数之外的所有内容,以便清理代码。谢谢。import subprocessimport reprint ("This will print any service that is stopped or unknown \n")#serversserver = str(input("Enter server to query(IP Address): "))server_list = server.split(",")server_count = len(server_list)#servicesservice = str(input("Enter service to query: "))service_list = service.split(",")service_count = len(service_list)#server loopfor 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)
查看完整描述

2 回答

?
BIG阳

TA贡献1859条经验 获得超6个赞

这是这样的:


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')



查看完整回答
反对 回复 2021-03-24
  • 2 回答
  • 0 关注
  • 197 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信