Exemple #1
0
def validate_address(addr_spec, metrics=False):
    """
    Given an addr-spec, runs the pre-parser, the parser, DNS MX checks,
    MX existence checks, and if available, ESP specific grammar for the
    local part.

    In the case of a valid address returns an EmailAddress object, otherwise
    returns None. If requested, will also return the parsing time metrics.

    Examples:
        >>> address.validate_address('*****@*****.**')
        None

        >>> address.validate_address('*****@*****.**')
        None

        >>> address.validate_address('*****@*****.**')
        [email protected]
    """
    mtimes = {
        'parsing': 0,
        'mx_lookup': 0,
        'dns_lookup': 0,
        'mx_conn': 0,
        'custom_grammar': 0
    }

    # sanity check
    if addr_spec is None:
        return None, mtimes

    # preparse address into its parts and perform any ESP specific pre-parsing
    addr_parts = preparse_address(addr_spec)
    if addr_parts is None:
        _log.warning('failed preparse check for %s', addr_spec)
        return None, mtimes

    # run parser against address
    bstart = time()
    paddr = parse('@'.join(addr_parts), addr_spec_only=True, strict=True)
    mtimes['parsing'] = time() - bstart
    if paddr is None:
        _log.warning('failed parse check for %s', addr_spec)
        return None, mtimes

    # lookup if this domain has a mail exchanger
    exchanger, mx_metrics = mail_exchanger_lookup(addr_parts[-1], metrics=True)
    mtimes['mx_lookup'] = mx_metrics['mx_lookup']
    mtimes['dns_lookup'] = mx_metrics['dns_lookup']
    mtimes['mx_conn'] = mx_metrics['mx_conn']
    if exchanger is None:
        _log.warning('failed mx check for %s', addr_spec)
        return None, mtimes

    # lookup custom local-part grammar if it exists
    bstart = time()
    plugin = plugin_for_esp(exchanger)
    mtimes['custom_grammar'] = time() - bstart
    if plugin and plugin.validate(addr_parts[0]) is False:
        _log.warning('failed custom grammer check for %s/%s', addr_spec,
                     plugin.__name__)
        return None, mtimes

    return paddr, mtimes
Exemple #2
0
def validate_address(addr_spec, metrics=False):
    """
    Given an addr-spec, runs the pre-parser, the parser, DNS MX checks,
    MX existence checks, and if available, ESP specific grammar for the
    local part.

    In the case of a valid address returns an EmailAddress object, otherwise
    returns None. If requested, will also return the parsing time metrics.

    Examples:
        >>> address.validate_address('*****@*****.**')
        None

        >>> address.validate_address('*****@*****.**')
        None

        >>> address.validate_address('*****@*****.**')
        [email protected]
    """
    mtimes = {'parsing': 0,
              'mx_lookup': 0,
              'dns_lookup': 0,
              'mx_conn':0 ,
              'custom_grammar':0}

    # sanity check
    if addr_spec is None:
        return None, mtimes

    # preparse address into its parts and perform any ESP specific pre-parsing
    addr_parts = preparse_address(addr_spec)
    if addr_parts is None:
        _log.warning('failed preparse check for %s', addr_spec)
        return None, mtimes

    # run parser against address
    bstart = time()
    paddr = parse('@'.join(addr_parts), addr_spec_only=True, strict=True)
    mtimes['parsing'] = time() - bstart
    if paddr is None:
        _log.warning('failed parse check for %s', addr_spec)
        return None, mtimes

    # lookup if this domain has a mail exchanger
    exchanger, mx_metrics = mail_exchanger_lookup(addr_parts[-1], metrics=True)
    mtimes['mx_lookup'] = mx_metrics['mx_lookup']
    mtimes['dns_lookup'] = mx_metrics['dns_lookup']
    mtimes['mx_conn'] = mx_metrics['mx_conn']
    if exchanger is None:
        _log.warning('failed mx check for %s', addr_spec)
        return None, mtimes

    # lookup custom local-part grammar if it exists
    bstart = time()
    plugin = plugin_for_esp(exchanger)
    mtimes['custom_grammar'] = time() - bstart
    if plugin and plugin.validate(addr_parts[0]) is False:
        _log.warning('failed custom grammer check for %s/%s', addr_spec, plugin.__name__)
        return None, mtimes

    return paddr, mtimes