slimta.util.dnsbl

This module provides classes to check DNSBLs for IP addresses or hosts. These lists can be hugely helpful and inexpensive ways of filtering spam.

class slimta.util.dnsbl.DnsBlocklist(address)

Bases: object

Class to check one DNSBL. This object provides __contains__ and __getitem__ protocols, so it supports simpler usage:

if '123.4.56.7' in dnsbl:
    reason = dnsbl['123.4.56.7']

Note

Because __getitem__ simply calls get_reason(), it will never raise a KeyError and should only be used after using get() or __contains__.

Parameters:address – The DNSBL domain name.
get(ip[, timeout=None[, strict=False]])

Checks this DNSBL for the given IP address. This method does not check the answer, only that the response was not NXDOMAIN.

Parameters:
  • ip – The IP address string to check.
  • timeout – A timeout in seconds before False is returned.
  • strict – If True, DNS exceptions that are not NXDOMAIN (including timeouts) will also result in a True return value.
Returns:

True if the DNSBL had an entry for the given IP address, False otherwise.

get_reason(ip[, timeout=None])

Gets the TXT record for the IP address on this DNSBL. This is usually a reason for why the IP address matched. As such, this function should only be called after get() returns True.

Parameters:
  • ip – The IP address to get a match reason for.
  • timeout – A timeout in seconds before giving up.
Returns:

A string with the reason, or None.

class slimta.util.dnsbl.DnsBlocklistGroup([pool=None])

Bases: object

Allows a group of DNSBLs to be queried simultaneously.

add_dnsbl(address)

Adds a DNSBL domain name to the list of DNSBLs to check.

Parameters:address – The DNSBL domain name.
get(ip[, timeout=None])

Queries all DNSBLs in the group for matches.

Parameters:
  • ip – The IP address to check for.
  • timeout – Timeout in seconds before canceling remaining queries.
Returns:

A set() containing the DNSBL domain names that matched a record for the IP address.

get_reasons(matches, ip[, timeout=None])

Gets the reasons for each matching DNSBL for the IP address.

Parameters:
  • matches – The DNSBL matches, as returned by get().
  • ip – The IP address to get reasons for.
  • timeout – Timeout in seconds before canceling remaining queries.
Returns:

A dict() keyed by the DNSBL domain names from the matches argument with the values being the reasons each DNSBL matched or None.

slimta.util.dnsbl.check_dnsbl(address[, match_code='550'[, match_message='5.7.1 Access denied'[, timeout=10.0]]])

Decorator for SmtpValidators methods that are given a Reply object. It will check the current SMTP session’s connecting IP address against the DNSBL provided at domain name address. If the IP matches, set the reply and do not call the validator method.

Parameters:
  • addressDnsBlocklist object, DnsBlocklistGroup object, or DNSBL domain name string.
  • match_code – When the connecting IP address matches, set the Reply code to this string.
  • match_message – When the connecting IP address matches, set the Reply message to this string.
  • timeout – Timeout in seconds before giving up the check.