-
Notifications
You must be signed in to change notification settings - Fork 31
/
inc_mxlookup.py
60 lines (51 loc) · 1.64 KB
/
inc_mxlookup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/local/opt/[email protected]/bin/python3
# -*- coding: utf-8 -*-
__author__ = 'DrPython3'
__date__ = '2021-12-04'
__version__ = '1.1'
__contact__ = 'https://github.com/DrPython3'
'''
--------------------------------------------
Functions for Reading MX Records of a Domain
--------------------------------------------
Part of << Mail.Rip V3: https://github.com/DrPython3/MailRipV3 >>
'''
# [IMPORTS]
# ---------
import sys
import socket
import dns.resolver
from inc_etc import domain_verification
# [FUNCTIONS]
# -----------
def get_host(default_timeout, email):
'''
Checks the DNS records of an email-domain for MX infos and returns any found SMTP URI.
:param float default_timeout: connection timeout
:param str email: email with domain to check
:return: found (True, False), smtp_host (SMTP URI)
'''
# set variables and stuff:
socket.setdefaulttimeout(default_timeout)
found = False
smtp_host = str('none')
smtp_domain = str(email.split('@')[1])
get_records = dns.resolver.Resolver(configure=False)
# using Google DNS, replace on purpose:
get_records.nameservers = ['8.8.8.8']
records = get_records.resolve(smtp_domain, 'MX')
counter = 0
# extract host from records:
while found == False:
try:
possible_host = str(records[counter]).split(' ')[1].rstrip('.')
verify_domain = domain_verification(possible_host)
if verify_domain == True:
smtp_host = possible_host
found = True
else:
counter += 1
except:
break
return found, smtp_host
# DrPython3 (C) 2021 @ GitHub.com