Python Forum

Full Version: Best section to do a while loop in my code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, this is my goal:
User will enter password thru a function getpass(). That function to get password will occur just one time.
Then a list of IP addresses should be retrieved from a .txt file. That list may contain hundreds of IPs.
So my intention is to let user provide password one time and execute script for all IP address in the .txt file.

Question:
Can you please recommend what would be the adequate "While True" condition in this case? Should I check for a While True "Exception" or check maybe for the "mcast_group_ip" to equal "EOF"? I would appreciate advice on how to approach this.


(...)
def get_mcastgroup():
     #This function reads a txt file with a list of IP addresses. It should process IP address by IP address in that list.
     with open(MCAST_GROUP_FILE) as f:
          line =  f.readline().strip()
          if not line:
              break
          print(f'******* line:{line}')

          return line

def main():

    try:
        #Get secure password from user. Username is a global variable USERNAME
        print ('* Starting mcast.py. Enter Password to Login: ')
        node_pass = get_credentials()
    except Exception as err:
        print('Error during get_credentials() call: ' + str(err))
    ### <===== Is this the best position for a "While True" loop; should I check end of file in this case?
        try:
            #Open file and retrieve group list
            mcast_group_ip = get_mcastgroup()
        except Exception as err:
            print('Error opening file function: ' + str(err))


        try:
             
            device_hdl = ssh_nodes(USERNAME, node_pass, TARGET_SERVER, mcast_group_ip)
        except Exception as err:
            print ('Error during get_platform_data: ' + str(err))

        try:
            #get_analysis_int = olist(get_platform_data)
            get_analysis_int = olist(device_hdl, mcast_group_ip)

        except Exception as err:
            print('Error during olist(): ' + str(err))

        try:
            get_action_list  = verify_threshold(get_analysis_int)
        except Exception as err:
            print('Error during verify_threshold(): ' + str(err))

        try:
            action_mcast(get_action_list, device_hdl)
        except Exception as err:
            print(f'Error during action mcast:{err}')

    sys.exit(0)

if __name__ == '__main__':
    main()