0

I want user enter IP subnet which is formatted x.x.x.x/y. But if user enters anything different this format, there should be warning as below. But below code is not working as I want. What is your suggestion?

def test():
    while True:
        val = raw_input("enter IP:")
        try:
            IP=ipaddress.ip_network(str(val),True)
        except:
            if val == "":
                print " Enter subnet:"
            else:
                print " IP should be formatted X.X.X.X/Y"

test()
vahdet
  • 6,357
  • 9
  • 51
  • 106
  • Possible duplicate of [Python: How to validate an IP Address with a CIDR notation (import socket)](https://stackoverflow.com/questions/45988215/python-how-to-validate-an-ip-address-with-a-cidr-notation-import-socket) – vahdet Mar 08 '19 at 07:43
  • If you are only just learning Python, you really should retarget to the currently supported and recommended version of the language, which is Python 3. By the original timetable, Python 2 was going to be end-of-lifed almost a year ago. It's got another year on life support, but it's dying. – tripleee Mar 08 '19 at 08:27
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – tripleee Mar 08 '19 at 08:27

0 Answers0