0

Please, friends, I am stuck once again. I want to use python to check the MX records of and email list and check if the MX record appears on another list and if yes will print the email and the matching MX record below is the code I managed to come up with.

import dns
import os
import re
import sys


with open('domain.txt') as f:
    for line in f:
        #do not forget to strip the trailing new line
        email = line.rstrip("\n")
        # print (email)
        j = open("saved_mxRecord.txt")
        list = j.readlines()
        domain = email.split("@")[-1]
        try:
            records = dns.resolver.query(domain, 'MX')
            mxRecord = records[0].exchange
            new_mxRecord = str(mxRecord)
            if new_mxRecord in range(list):
                print(new_mxRecord)
                print email

        except Exception:
            pass

when I remove this line of code if new_mxRecord in range(list): I am able to have it spring but the major function is removed. Please how do I get to check if the new_mxRecord exists on the saved_mxRecord and have it printed out.

  • so 'list' is a list of strings (never good to use the name 'list' as it is a built-in class in python). The range function expects an integer, not a python list. Perhaps you meant "if new_mxRecord in list: – electrogas Jul 30 '20 at 02:59
  • @electrogas, I have it as if "new_mxRecord in list:" but it did not print as well. – Mbah Augustine Jul 30 '20 at 07:04
  • OK, import pdb at the top, put pdb.set_trace() just before your if statement, then type in "print new_mxrecord" and "print list". Maybe you'll see that your list is a list of byte type, but new_mxrecord is not. – electrogas Jul 31 '20 at 15:26

1 Answers1

0

Finally, I am able to fix it, below is the working code:

import dns
import os
import re
import sys
#from multiprocessing.dummy import Pool as ThreadPool

with open('domain.txt') as f:
    for line in f:
        #do not forget to strip the trailing new line
        email = line.rstrip("\n")
        # print (email)
        
        domain = email.split("@")[-1]
        try:
            records = dns.resolver.query(domain, 'MX')
            mxRecord = records[0].exchange
            new_mxRecord = str(mxRecord)
            print new_mxRecord
            list = ['mx1',  'mx2', 'mx3']
            if new_mxRecord in list:
                print email



        except Exception:
            pass  

Thanks, @electrogas for guiding me