def check_rbl_blacklist(addr): """ Check DNS BL for a given ip. """ if not utils.is_valid_ipv4_address(addr): return {"error": "Expecting a non-shortened IPv4."}, 400 result = blacklist_service.get_blacklist_report(addr) if not result: return {"error": "RBL queries failed."}, 500 return result, 200
def parse_html(content): """ Parse Spamhaus HTML webpage to extract opened tickets. :param str content: Raw html sources :raises UnrecognizedFormatException: If sources cannot be parsed :rtype: array :return: Array of dictionnaries containing all opened SBL. """ soup = bs4.BeautifulSoup(content, 'html.parser') search = soup.find_all(*TAG_TO_FIND) if not search: raise UnrecognizedFormatException("Cannot parse file.") result = [] # Iterate through the found rows for row in search: # sbl_number and CIDR are surrounded by a bold tag. sbl_number = cidr = None infos = [elem.text for elem in row.find_all('b')] for info in infos: if info.startswith(SBL_STRING): sbl_number = int(info.replace(SBL_STRING, '')) elif '/' in info: addr = info.split('/')[0] if utils.is_valid_ipv4_address(addr): cidr = info first_seen = row.find_all('td')[4].find_all('span')[0].text first_seen = datetime.strptime(first_seen, '%d-%b-%Y %H:%M GMT') cause = row.find_all('td')[5].find_all('span')[0].text.strip() if all((sbl_number, first_seen, cidr)): result.append({ 'sbl_number': sbl_number, 'first_seen': first_seen, 'cidr': cidr, 'cause': cause }) return result
def parse_html(content): """ Parse Spamhaus HTML webpage to extract opened tickets. :param str content: Raw html sources :raises Exception: If sources cannot be parsed :rtype: array :return: Array of dictionnaries containing all opened SBL. """ soup = bs4.BeautifulSoup(content) search = soup.find_all(*TAG_TO_FIND) if not search: raise Exception("Cannot parse file.") result = [] # Iterate through the found rows for row in search: # sbl_number and CIDR are surrounded by a bold tag. sbl_number = cidr = None infos = [elem.text for elem in row.find_all('b')] for info in infos: if info.startswith(SBL_STRING): sbl_number = int(info.replace(SBL_STRING, '')) elif '/' in info: addr = info.split('/')[0] if utils.is_valid_ipv4_address(addr): cidr = info first_seen = row.find_all('td')[4].find_all('span')[0].text first_seen = datetime.strptime(first_seen, '%d-%b-%Y %H:%M GMT') if all((sbl_number, first_seen, cidr)): result.append({ 'sbl_number': sbl_number, 'first_seen': first_seen, 'cidr': cidr }) return result
def test_is_valid_ipv4(self): self.assertTrue(utils.is_valid_ipv4_address('5.5.5.5')) self.assertFalse(utils.is_valid_ipv4_address('5.5.5.5.5')) self.assertFalse(utils.is_valid_ipv4_address('127.1'))