codekingpro/portable-devtools
114k
1# A command-line tool for testing.2#3# Usage:4#5# python -m email_validator test@example.org6# python -m email_validator < LIST_OF_ADDRESSES.TXT7#8# Provide email addresses to validate either as a command-line argument9# or in STDIN separated by newlines. Validation errors will be printed for10# invalid email addresses. When passing an email address on the command11# line, if the email address is valid, information about it will be printed.12# When using STDIN, no output will be given for valid email addresses.13#14# Keyword arguments to validate_email can be set in environment variables15# of the same name but upprcase (see below).16 17import json18import os19import sys20 21from .validate_email import validate_email22from .deliverability import caching_resolver23from .exceptions_types import EmailNotValidError24 25 26def main(dns_resolver=None):27 # The dns_resolver argument is for tests.28 29 # Set options from environment variables.30 options = {}31 for varname in ('ALLOW_SMTPUTF8', 'ALLOW_QUOTED_LOCAL', 'ALLOW_DOMAIN_LITERAL',32 'GLOBALLY_DELIVERABLE', 'CHECK_DELIVERABILITY', 'TEST_ENVIRONMENT'):33 if varname in os.environ:34 options[varname.lower()] = bool(os.environ[varname])35 for varname in ('DEFAULT_TIMEOUT',):36 if varname in os.environ:37 options[varname.lower()] = float(os.environ[varname])38 39 if len(sys.argv) == 1:40 # Validate the email addresses pased line-by-line on STDIN.41 dns_resolver = dns_resolver or caching_resolver()42 for line in sys.stdin:43 email = line.strip()44 try:45 validate_email(email, dns_resolver=dns_resolver, **options)46 except EmailNotValidError as e:47 print(f"{email} {e}")48 else:49 # Validate the email address passed on the command line.50 email = sys.argv[1]51 try:52 result = validate_email(email, dns_resolver=dns_resolver, **options)53 print(json.dumps(result.as_dict(), indent=2, sort_keys=True, ensure_ascii=False))54 except EmailNotValidError as e:55 print(e)56 57 58if __name__ == "__main__":59 main()60 