<commit_before>import dns.resolver
import ConfigParser
import os

from centinel.experiment_py import Experiment

class ConfigurableDNSExperiment(Experiment):
    name = "config_dns"

    def __init__(self, input_file):
        self.input_file  = input_file
        self.results = []
	self.args = dict()

    def run(self):
	parser = ConfigParser.ConfigParser()
	parser.read([self.input_file,])
	if not parser.has_section('DNS'):
	    return


	self.args.update(parser.items('DNS'))

	if 'resolver' in self.args.keys():
	    self.resolver = self.args['resolver']
	else:
            self.resolver = "8.8.8.8"

        if 'record' in self.args.keys():
            self.record = self.args['record']
        else:
            self.record = 'A'

	url_list = parser.items('URLS')
	for url in url_list[0][1].split():
	    self.host = url
            self.dns_test()

    def dns_test(self):
	result = {
            "host" : self.host,
	    "resolver" : self.resolver
        }
	ans = ""
	if self.record == 'A':
            res = dns.resolver.query(self.host, self.record)
            for i in res.response.answer:
                if ans == "":
                    ans = i.to_text()
                else:
                    ans = ans + ", " + i.to_text()
        else:
            try:
                query = dns.message.make_query(self.host, self.record)
                response = dns.query.udp(query, self.resolver, timeout=3)
                for answer in response.answer:
                    if ans == "":
                        ans = answer.to_text()
                    else:
                        ans += ", " + answer.to_text()
            except Exception:
                print("Error Querying " + self.record + " record for " + self.host)
        if ans == "":
            ans = "Unavailable"
	print(ans)
        result["record_type"] = self.record
        result['record'] = ans
        

        self.results.append(result)<commit_msg>Added timeout field for config and better error handling for timeouts<commit_after>import dns.resolver
import ConfigParser
import os

from centinel.experiment_py import Experiment

class ConfigurableDNSExperiment(Experiment):
    name = "config_dns"

    def __init__(self, input_file):
        self.input_file  = input_file
        self.results = []
	self.args = dict()

    def run(self):
	parser = ConfigParser.ConfigParser()
	parser.read([self.input_file,])
	if not parser.has_section('DNS'):
	    return


	self.args.update(parser.items('DNS'))

	if 'resolver' in self.args.keys():
	    self.resolver = self.args['resolver']
	else:
            self.resolver = "8.8.8.8"

        if 'record' in self.args.keys():
            self.record = self.args['record']
        else:
            self.record = 'A'

        if 'timeout' in self.args.keys():
            self.timeout = int(self.args['timeout'])
        else:
            self.timeout = 3

	url_list = parser.items('URLS')
	for url in url_list[0][1].split():
	    self.host = url
            self.dns_test()

    def dns_test(self):
	result = {
            "host" : self.host,
	    "resolver" : self.resolver
        }
	ans = ""
	if self.record == 'A':
            res = dns.resolver.query(self.host, self.record)
            for i in res.response.answer:
                if ans == "":
                    ans = i.to_text()
                else:
                    ans = ans + ", " + i.to_text()
        else:
            try:
                query = dns.message.make_query(self.host, self.record)
                response = dns.query.udp(query, self.resolver, timeout=self.timeout)
                for answer in response.answer:
                    if ans == "":
                        ans = answer.to_text()
                    else:
                        ans += ", " + answer.to_text()
            except dns.exception.Timeout:
                print("Query Timed out for " + self.host)
                ans = "Timeout"
            except Exception:
                print("Error Querying " + self.record + " record for " + self.host)
                ans = "Error"
        if ans == "":
            ans = "Unavailable"
	print(ans)

        result["record_type"] = self.record
        result['record'] = ans
        

        self.results.append(result)
{'fraction_non_alphanumeric': 0.12332628611698379, 'fraction_numerical': 0.003288700963119568, 'mean_word_length': 1.681360201511335, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '103924', 'n_tokens_mistral': 1315, 'n_tokens_neox': 1253, 'n_words': 275}