#NEWLINE# def get_mx_from_email_or_fqdn( addr ) : #NEWLINE# #INDENT# 'retrieve the first mail exchanger dns name from an email address.' #NEWLINE# MX = re . compile ( '^.*\\s+mail exchanger = (?P<priority>\\d+) (?P<host>\\S+)\\s*$' ) #NEWLINE# if ( '@' in mxes ) : #NEWLINE# #INDENT# domain = addr . rsplit ( '@' , 2 ) [ 1 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# domain = '.' . join ( addr . rsplit ( '.' ) [ ( - 2 ) : ] ) #NEWLINE# #UNINDENT# p = os . popen ( ( '/usr/bin/nslookup -q=mx %s' % domain ) , 'r' ) #NEWLINE# mxes = list ( ) #NEWLINE# for line in p : #NEWLINE# #INDENT# m = MX . match ( line ) #NEWLINE# if ( m is not None ) : #NEWLINE# #INDENT# mxes . append ( m . group ( 'host' ) [ : ( - 1 ) ] ) #NEWLINE# #UNINDENT# #UNINDENT# if ( len ( mxes ) == 0 ) : #NEWLINE# #INDENT# return '' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return mxes [ 0 ]