title: Exception handling with IP Address Module
body: <p>I'm not a software engineer by trade (Network Security Engineer), and am learning more automation techniques and coding.  I'm sure I'm just missing something pretty obvious within my code and any assistance would be appreciated.  </p>
  
  <p>I'm running an input script asking users to enter in IP addresses in either CIDR or host IPs in IPv4/v6 formats into an input box (within Tkinter).  I have two separate coding scripts that perform each of these functions (CIDR and Host IPs).  I'm trying to combine the code together so only one application needs to be run.  </p>
  
  <p>When using the ipaddress module, if the format typed in is not correct (utilizing ip_address (for host addresses) or ip_network (for CIDR addresses)), it'll throw a Value error.  This is what I want, as I'm handling those exceptions.</p>
  
  <p>The problem is I need both the CIDR and IP Hosts added to separate lists for REST API calls (as the require different call formats). And when my below code processes, if the entry into the text box is CIDR format it'll throw the Value error (as the IP Host is the first statement which is why it throws the error). </p>
  
  <p>I've been able to have it process ipaddress inputs at the same time with the following code in replace of the second elif statements (the first is just a failsafe for empty inputs): </p>
  
  <pre><code>if self.ip == str(ipaddress.ip_network(self.e1.get())) or self.ip == str(ipaddress.ip_address(self.e1.get())):
  </code></pre>
  
  <p>The problem with the above statement is I have no way to decipher self.ip and append it to the appropriate list (one for CIDR and one for IP Hosts). </p>
  
  <p>The main problem with my main code, as I stated earlier is, that if the input is CIDR, it'll throw the value error because the second elif block has ipaddress.ip_address as the first input, and once it encounters the error it will not continue the code.  </p>
  
  <p>Maybe it's a structure issue, or I'm not asking the right questions...but any help within my code to allow someone to input either a host IP address or CIDR format subnet, and the code throw a Value error if none of those two are entered.  As well append each to their appropriate list would be greatly appreciated. </p>
  
  <p>Main Code (FYI: the e1 variable is the input textbox):</p>
  
  <pre><code>  def add_ip(self):
      self.hostiplist = []
      self.cidriplist = []
      try:
        if not self.e1.get():# empty! (empty string is false value)
          messagebox.showerror(title='Error', message='Ooops, Please enter an acceptible host or network IP Address.')
          self.e1.delete(0,END)
        elif self.e1.get() == " ":
          messagebox.showerror(title='Error', message='Ooops, Please enter an acceptible host or network IP Address.')
          self.e1.delete(0,END)
        else:
          self.ip = self.e1.get()
          if self.ip == str(ipaddress.ip_address(self.e1.get())):
            self.hostiplist.append(self.ip)
            print("IP Host staged for deployment: %s" % (self.ip))
            print("")
            self.e1.delete(0,END)
          elif self.ip == str(ipaddress.ip_network(self.e1.get())):
            self.cidriplist.append(self.ip)
            print("IP Network staged for deployment: %s" % (self.ip))
            print("")
            self.e1.delete(0,END)
          else:
            print("Didn't enter valid entry")
  
      except ValueError as e:
        messagebox.showerror(title='Error', message='Ooops,' + str(e) + '.' + ' Please enter an acceptible host or network IP Address.')
        self.e1.delete(0,END)
        return 'Value'    
      except TypeError:
        messagebox.showerror(title='Error', message='Ooops,' + str(e) + '.' + ' Please enter an acceptible host or network IP Address.')
        self.e1.delete(0,END)
        return 'Type'
  </code></pre>
  
  <p>Here is my single function script that works with error handling (if anything but CIDR format is entered you'll receive a value error):</p>
  
  <pre><code>def add_ip_network():
     #IPv4 &amp; v6 Address Handling.  Nothing but those formats can be entered.
    try:
      ip = ipaddress.ip_network(e1.get())
      nonsubnethostsList.append(ip)
      print("IP Network staged for deployment: %s" % (ip))
      print("")
      e1.delete(0,END)
    except ValueError:
      print('Incorrect entry, please use IPv4 or IPv6 CIDR Formats')
      e1.delete(0,END)
      return 'Value'
    except TypeError:
      print('Incorrect entry, please use IPv4 or IPv6 CIDR Formats')
      e1.delete(0,END)
      return 'Type'
  </code></pre>
  
  <p>Regards,</p>
  
  <p>Edit:<br>
  I've also tried adjusting my Main code to match my working single format script.
  From this:</p>
  
  <pre><code>if self.ip == str(ipaddress.ip_address(self.e1.get()))
  </code></pre>
  
  <p>To this:</p>
  
  <pre><code>ip = ipaddress.ip_network(e1.get())
  </code></pre>
  
  <p>It gave me the same Value Errors, so I tried just comparing the strings with If statements.</p>