使用ipaddress模块​​进行Python输入验证

时间:2017-06-09 15:48:31

标签: python-3.x rest ip-address

我目前正在创建一个REST API应用程序。我遇到了IP地址输入验证问题。我只使用主机IP地址或网络地址。我想将主机和网络组合成一个功能,所以我不必有两个功能执行类似的操作。

def add_ip_network():
   #IPv4 & 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))
    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' 

def add_ip_host():
   #IPv4 & v6 Address Handling.  Nothing but those formats can be entered.
  try:
    ip = ipaddress.ip_ipaddress(e1.get())
    nonsubnethostsList.append(ip)
    print("IP Host staged for deployment: %s" % (ip))
    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' 

这是我工作的两个功能。当try语句发生时...如果ip变量不等于ipaddress.ipaddress或.ip_network的相应格式,它将抛出错误,因为模块不允许nonIPaddress输入(这是我想要的)。

我想将这些(如前所述)组合成一个条件语句。这是目前我的UGLY设置无效(它在Tkinter GUI中)。错误处理有效,问题是在尝试self.ip = self.e1.get()之后它不执行if和elif语句。

我正在尝试让他们以主机格式或CIDR格式输入IP地址...如果输入了一个或另一个,它会执行等效追加到列表并让他们知道它已被添加到清单。

任何有关正确格式化的建议都将受到赞赏。

  def add_ip(self):
    self.hostiplist = []
    self.cidriplist = []
    try:
      if not self.e1.get():
        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:
        try: 
          self.ip = self.e1.get() 
#Tried both directions of IPs with e1 in the begining or self.ip being in the front.
          if self.e1.get() == ipaddress.ip_address(self.ip):
            self.hostiplist.append(ip)
            print("IP Host staged for deployment: %s" % (ip))
            print("")
            self.e1.delete(0,END)
          elif self.ip == ipaddress.ip_network(self.e1.get()):
            self.cidriplist.append(ip)
            print("IP Network staged for deployment: %s" % (ip))
            print("")
            self.e1.delete(0,END)
          else:
            print("Didn't enter valid entry") # Placeholder for tests
        except:
          print('Nothing happened') # Placeholder for tests
    except ValueError:
      messagebox.showerror(title='Error', message='Ooops, Value Please enter an acceptible host or network IP Address.')
      self.e1.delete(0,END)
      return 'Value'    
    except TypeError:
      messagebox.showerror(title='Error', message='Ooops, Type Please enter an acceptible host or network IP Address.')
      self.e1.delete(0,END)
      return 'Type' 

任何帮助都会很棒,

谢谢!

0 个答案:

没有答案