[Tutor] Review my code
Chanakya Mitra
chanakya at optirisk-systems.com
Mon Sep 26 13:55:06 CEST 2011
I wrote a short script that tries to check if an email address exists or
not.
It takes the domain of the inputted email and looks up the registered
mail exchange.
It then connects via smtp and attempts the "RCPT TO:<entered email>"
command and based on the response decides if the email exists or not.
I was trying to be more OOP in my approach but not sure I've been
entirely successful.
Thanks for your time.
Chan
Code:
import DNS, socket, string, re, sys
servers = []
myserver = 'mailgate.xxxxxxxx.com'
mymail = 'maywedtuza at xxxxxxxx.com'
CRLF="\r\n"
def mailxch(hname):
mxhosts = DNS.mxlookup(hname)
return mxhosts
def prompt(prompt):
return raw_input(prompt).strip()
class smtsock(object):
response = None
def getmsg(self):
self.response = self.msock.makefile('rb')
while 1:
try:
line = self.response.readline()
return line[:3], line[4:]
except socket.error:
self.msock.close()
return "socket Error"
def put(self,cmd,args=""):
if args == "":
str = '%s%s' % (cmd, CRLF)
else:
str = '%s %s%s' % (cmd, args, CRLF)
try:
self.msock.send(str)
except socket.error:
self.msock.close()
return "socket Error"
def pr(self,cmd,args=""):
self.put(cmd, args)
return self.getmsg()
def conn(self,server):
try:
self.msock = socket.create_connection((server,25))
(hand, msg) = self.getmsg()
return hand, msg
except:
err = sys.exc_info()[1]
return err
def helo(self, server):
(hand, msg) = self.pr("HELO",server)
return hand, msg
def vrfy(self, hname):
cstr = "<%s>"% hname
(hand, msg) = self.pr("VRFY", cstr)
return hand, msg
def mailfrm(self, emf):
cstr = "<%s>"% emf
(hand, msg) = self.pr("MAIL FROM:", cstr)
return hand, msg
def rcptto(self,hname):
cstr = "<%s>"% hname
(hand, msg) = self.pr("rcpt to:", cstr)
return hand, msg
def cls(self):
self.msock.close()
rcpt_handlers = {
"250":" \n#------------------------------------------# \n\tE-mail
Exists\n#------------------------------------------# \n",
"251":" User not local; will forward to <forward-path>",
"550":" \n#------------------------------------------# \n\tNo Such
Address\n#------------------------------------------# \n",
"551":" User not local; please try <forward-path>",
"552":" Requested mail action aborted: exceeded storage allocation",
"553":" Requested action not taken: mailbox name not allowed",
"450":" Requested mail action not taken: mailbox unavailable",
"451":" Requested action aborted: local error in processing",
"452":" Requested action not taken: insufficient system storage",
"500":" Syntax error, command unrecognised",
"501":" Syntax error in parameters or arguments",
"503":" Bad sequence of commands",
"521":" <domain> does not accept mail [rfc1846]",
"socket Error":" Socket Error: Connection closed.",
"421":" <domain> Service not available, closing transmission channel"
}
em =
re.compile(r"(?:^|\s)[-a-z0-9_.]+@(?:[-a-z0-9]+\.)+[a-z]{2,6}(?:\s|$)",r
e.IGNORECASE)
while True:
hname = prompt("E-Mail Address(or XX to quit): ")
if hname == "XX":
break
if em.match(hname) == None:
print "Email incorrect format"
else:
s = smtsock()
servers = mailxch(hname.split("@")[1])
print servers
i = 0
for i in range(len(servers)):
print "Trying mailserver: %s"% servers[i][1]
(reply, msg)= s.conn(servers[i][1])
if reply != '220':
break
(reply, msg) = s.helo(myserver)
if reply != '250':
break
(reply, msg) = s.mailfrm(mymail)
if reply != '250':
break
(reply, msg) = s.rcptto(hname)
print rcpt_handlers.get(reply)
if reply == '250':
break
s.cls()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110926/b4ea60f1/attachment-0001.html>
More information about the Tutor
mailing list