Python Forum

Full Version: IP Subnet is asked from user
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I am trying to force the user enter IP format with below code. However, it does not work. User should be enter x.x.x.x/y format for IP to continue.
What is problem on below code?
thanks

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()
One question, why are you using python 2.7?
Current version is 3.7.2 and 2.7 dies in less than 10 months.