Python line check



  • I want to check the line of the user. I need a line of this kind.

    192.168.4.35 ресепшен
    

    I mean, you need to get a type of XXX.XXX.XXX.XXX.XXX. ip address and through a gap, anything. How do I do that?



  • import re
    p = re.compile('^\d{1,3}(\.\d{1,3}){3}\s.*')
    if (p.match(input_string))
        # можно проверить IP адрес на корректность
    

    Additional verification may be required as the line passes 888.999.444.777 some textwhere there is no correct IP address

    Better.

    from ipaddress import ip_address
    

    try:
    ip = ip_address(input_string.split()[0])
    except ValueError:
    print('Incorrect IP address')
    except IndexError:
    print('Bad input string')


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2