Skip to content

Instantly share code, notes, and snippets.

@stuartcw
Last active August 29, 2015 14:13
Show Gist options
  • Save stuartcw/92e8ed933f6178f34c66 to your computer and use it in GitHub Desktop.
Save stuartcw/92e8ed933f6178f34c66 to your computer and use it in GitHub Desktop.
Validating Email Addresses by checking their domain.
#!/usr/bin/python
import sys
import socket
import os
import re
DEBUG_MODE=False
OUPUT_FILENAME="bad_domains.txt"
__query = 'nslookup -q=mx {0}'
FILENAME=sys.argv[1]
def isGarbage(line):
if line.startswith("---") or line.endswith("rows)") or line.startswith("domain") or len(line)==0:
return True
else:
return False
def canResolveDomain(domain):
try:
ipAddress=socket.gethostbyname(domain)
return True
except socket.gaierror:
return False
def check_for_mx_record(domain):
command = __query.format(domain)
with os.popen(command) as response:
result = response.readlines()
for line in result:
if line.find("mail exchanger")>-1:
print domain,line
return True
return False
with open(FILENAME) as inputFile:
with open(OUPUT_FILENAME,"w") as outputFile:
domains=set(inputFile)
for line in domains:
line=line.strip()
if isGarbage(line):
continue
domain=line
hasMXHosts=check_for_mx_record(domain)
print domain, hasMXHosts
if not hasMXHosts:
print >>outputFile,domain
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment