Commit 05a7af5d authored by Alexandre's avatar Alexandre
Browse files

Move print_result function to global scope

parent 0e447fe0
......@@ -302,39 +302,6 @@ class Connection:
# proper response).
pass
def print_result(self, rcode, msg, size, prefix=None):
ok = True
if (self.dot and rcode) or (not self.dot and rcode == 200):
if not monitoring:
if not check or verbose:
print(msg)
else:
if size is not None and size > 0:
print("%s OK - %s" % (self.server, "No error for %s/%s, %i bytes received" % (name, rtype, size)))
else:
print("%s OK - %s" % (self.server, "No error"))
sys.exit(STATE_OK)
else:
if not monitoring:
if prefix:
print(prefix, end=': ', file=sys.stderr)
if dot:
print("Error: %s" % msg, file=sys.stderr)
else:
try:
msg = msg.decode()
except (UnicodeDecodeError, AttributeError):
pass # Sometimes, msg can be binary, or Latin-1
print("HTTP error %i: %s" % (rcode, msg), file=sys.stderr)
else:
if not dot:
print("%s HTTP error - %i: %s" % (self.server, rcode, msg))
else:
print("%s Error - %i: %s" % (self.server, rcode, msg))
sys.exit(STATE_CRITICAL)
ok = False
return ok
class ConnectionDoT(Connection):
def __init__(self, server, servername=None, connect=None, forceIPv4=False, forceIPv6=False,
......@@ -410,7 +377,7 @@ class ConnectionDoT(Connection):
request = RequestDoT(qname, qtype, want_dnssec=dnssec, use_edns=edns)
self.send_and_receive(request)
request.check_response()
return (request.rcode, request.response, request.response_size)
return request
class ConnectionDoH(Connection):
......@@ -514,7 +481,7 @@ class ConnectionDoH(Connection):
request = RequestDoH(qname, qtype, want_dnssec=dnssec, use_edns=edns)
self.send_and_receive(request)
request.check_response()
return (request.rcode, request.response, request.response_size)
return request
def get_next_domain(input_file):
......@@ -529,6 +496,45 @@ def get_next_domain(input_file):
(name, rtype) = line.split()
return name, rtype
def print_result(connection, request, prefix=None):
ok = True
dot = connection.dot
server = connection.server
rcode = request.rcode
msg = request.response
size = request.response_size
if (dot and rcode) or (not dot and rcode == 200):
if not monitoring:
if not check or verbose:
print(msg)
else:
if size is not None and size > 0:
print("%s OK - %s" % (server, "No error for %s/%s, %i bytes received" % (name, rtype, size)))
else:
print("%s OK - %s" % (server, "No error"))
sys.exit(STATE_OK)
else:
if not monitoring:
if prefix:
print(prefix, end=': ', file=sys.stderr)
if dot:
print("Error: %s" % msg, file=sys.stderr)
else:
try:
msg = msg.decode()
except (UnicodeDecodeError, AttributeError):
pass # Sometimes, msg can be binary, or Latin-1
print("HTTP error %i: %s" % (rcode, msg), file=sys.stderr)
else:
if not dot:
print("%s HTTP error - %i: %s" % (server, rcode, msg))
else:
print("%s Error - %i: %s" % (server, rcode, msg))
sys.exit(STATE_CRITICAL)
ok = False
return ok
def run_check(connection):
req_args = { 'qname': name, 'qtype': rtype, 'use_edns': edns, 'want_dnssec': dnssec }
requests = create_requests_list(dot=dot, **req_args)
......@@ -551,7 +557,7 @@ def run_check(connection):
error(e)
break
request.check_response()
if not connection.print_result(request.rcode, request.response, request.response_size, prefix=test_name):
if not print_result(connection, request, prefix=test_name):
ok = False
break
......@@ -719,12 +725,12 @@ if not check:
if ifile is not None:
name, rtype = get_next_domain(input)
try:
(rcode, msg, size) = conn.do_test(name, rtype)
request = conn.do_test(name, rtype)
except (OpenSSL.SSL.Error, CustomException) as e:
ok = False
error(e)
break
if not conn.print_result(rcode, msg, size):
if not print_result(conn, request):
ok = False
if tests > 1 and i == 0:
start2 = time.time()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment