Add functions to get and parse SPF records

This commit is contained in:
Sokratis Alichanidis 2025-05-03 23:36:14 +02:00
parent 013c0ea4d4
commit 369137ae99
Signed by: salichani
SSH key fingerprint: SHA256:DRfo4u5CWxYiHCJUUUPaBRqlOoTwT+AUm+i9y+z1uVw
2 changed files with 37 additions and 0 deletions

1
.gitignore vendored
View file

@ -162,3 +162,4 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/ #.idea/
/.idea/

36
pymsgcheck.py Normal file
View file

@ -0,0 +1,36 @@
from os.path import split
import dns.resolver
import ipaddress
def parse_spf_parts(part):
if part.startswith('ip4:'):
return [part[4:]]
elif part.startswith('ip6:'):
return [part[4:]]
elif part.startswith('include'):
return get_spf_record(part[8:])
elif part.startswith('a'):
addresses = []
for address in dns.resolver.resolve_name(part[2:]).addresses():
addresses.append(address)
return addresses
else:
return []
def get_spf_record(domain_name):
spf_record = ''
servers = []
txt_records = dns.resolver.resolve(domain_name, 'TXT')
for record in txt_records:
if record.to_text().find('v=spf1') > 0:
spf_record = record.to_text()
parts = spf_record.split()
for part in parts:
servers += (parse_spf_parts(part))
return servers
spf_servers = get_spf_record('trivago.com')
print(spf_servers)