On 07/26/2012 11:05 PM, gowrishankar wrote:

+        # Match IP|'':<port>
+        m = re.search('^((?:25[0-]|2[0-4][0-9]|[01]?[0-9][0-9]?\.){3}'
?:25[0-5] ??

I'd suggest that regular expressions are great for picking out parts of text, but not the right tool for doing validation of a highly standardized _numeric_ format.

I'm sure there's a module somewhere that can do this easier, but by way of example, let's just KISS - Keep it Stupid Simple and use a sieve-like validater (not tested):

try:
    ip_part, port_part = cmdresult.split(':')
    # List Comprehension to save space
    byte_list = [ int(b) for b in ip_part.split('.')[0:5] ]
    # quick check
    [ assert(b >= 0 and b <= 255) for b in byte_list ]
    assert(byte_list[3] < 255) # can't be broadcast address
    assert(byte_list[0] > 0)
except (AssertionError, list, of, other, exceptions):
    raise returned_ip_address_bad('Malformed IP:port string %s' %
                                  cmdresult)

IMHO it's a waste of time to try and account for every condition. Code can always be improved later, so for the first pass just shoot for "good enough".

--
Chris Evich, RHCA, RHCE, RHCDS, RHCSS
Quality Assurance Engineer
e-mail: cevich + `@' + redhat.com o: 1-888-RED-HAT1 x44214

_______________________________________________
Autotest-kernel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/autotest-kernel

Reply via email to