Python Forum
Best section to do a while loop in my code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best section to do a while loop in my code
#1
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to add multi-line comment section? Winfried 1 139 Mar-24-2024, 04:34 PM
Last Post: deanhystad
  Append data to Yaml section tbaror 0 6,890 Feb-09-2022, 06:56 PM
Last Post: tbaror
  Create an isolated section in bash? yxk 1 1,911 Feb-27-2020, 11:16 AM
Last Post: Larz60+
  ConfigParser.NoSectionError: No section: 'Default' degenaro 1 14,361 Jun-08-2018, 08:33 PM
Last Post: ljmetzger
  Help understanding code section raz631 4 3,197 Dec-14-2017, 09:32 PM
Last Post: raz631

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020