Wednesday, February 21, 2024
HomeSoftware EngineeringThe way to create a Area Identify Validator in Python

The way to create a Area Identify Validator in Python


The problem

Create a site identify validator largely compliant with RFC 1035, RFC 1123, and RFC 2181

The next guidelines apply:

  • Area identify might comprise subdomains (ranges), hierarchically separated by . (interval) character
  • Area identify should not comprise greater than 127 ranges, together with high degree (TLD)
  • Area identify should not be longer than 253 characters (RFC specifies 255, however 2 characters are reserved for trailing dot and null character for root degree)
  • Degree names have to be composed out of lowercase and uppercase ASCII letters, digits and – (minus signal) character
  • Degree names should not begin or finish with – (minus signal) character
  • Degree names should not be longer than 63 characters
  • Prime degree (TLD) should not be absolutely numerical

Moreover:

  • Area identify should comprise at the very least one subdomain (degree) aside from TLD
  • Prime degree validation have to be naive – ie. TLDs nonexistent in IANA register are nonetheless thought of legitimate so long as they adhere to the principles given above.

The validation perform accepts a string with the total area identify and returns a boolean worth indicating whether or not the area identify is legitimate or not.

Examples:

validate('aoms') == False
validate('andrewodendaal.com') == True
validate('amazon.com') == True
validate('AMAZON.COM') == True
validate('sub.amazon.com') == True
validate('amazon.com-') == False
validate('.amazon.com') == False
validate('[email protected]') == False
validate('127.0.0.1') == False

The answer in Python

Possibility 1:

import re

def validate(area):
    return re.match('''
        (?=^.{,253}$)          # max. size 253 chars
        (?!^.+.d+$)          # TLD isn't absolutely numerical
        (?=^[^-.].+[^-.]$)     # does not begin/finish with '-' or '.'
        (?!^.+(.-|-.).+$)    # ranges do not begin/finish with '-'
        (?:[a-zd-]            # makes use of solely allowed chars
        {1,63}(.|$))          # max. degree size 63 chars
        {2,127}                # max. 127 ranges
        ''', area, re.X | re.I)

Possibility 2:

def validate(area):
    print(area)
    if len(area) > 253 or len(area) == 0:
        print(1)
        return False
    
    els = area.cut up('.')
    if len(els) > 127 or len(els) < 2:
        print(2)
        return False
    
    for x in els:
        if len(x) > 63 or len(x) == 0:
            print(3)
            return False

        if not x[0].isalnum() or not x[-1].isalnum():
            print(4)
            return False

        for l in x:
            if (not all(ord(c) < 128 for c in l) or not l.isalnum()) and l != '-':
                print(5)
                return False

    if els[-1].isnumeric():
        return False
    
    print(True)
    return True

Possibility 3:

import re

def validLevel(lvl):
    return not bool(re.search(r'^-|-$', lvl)) and bool(re.match(r'[a-zA-Z0-9-]{1,63}$', lvl))

def validate(area):
    lst = area.cut up('.')
    return len(area) <= 253 
           and a couple of <= len(lst) <= 127 
           and never lst[-1].isdigit() 
           and all( validLevel(lvl) for lvl in lst )

Check instances to validate our answer

check.describe('Area identify validator checks')
check.anticipate(not validate('aoms')) 
check.anticipate(validate('andrewodendaal.com'))
check.anticipate(validate('amazon.com'))
check.anticipate(validate('AMAZON.COM'))
check.anticipate(validate('sub.amazon.com'))
check.anticipate(not validate('amazon.com-'))
check.anticipate(not validate('.amazon.com'))
check.anticipate(not validate('[email protected]'))
check.anticipate(not validate('127.0.0.1'))
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments