def main(**subservers): if len(sys.argv) != 2 and len(sys.argv) != 3: print(sys.argv) print("Usage: %s interface hostname" % sys.argv[0]) print(" %s config_file.ini" % sys.argv[0]) sys.exit(-1) server = None if len(sys.argv) == 3: ip = get_ip_from_hostname(sys.argv[2]) if ip is None: print( "Couldn't resolve IP from hostname, consider using a config file" ) sys.exit(-1) server = Server(sys.argv[1], sys.argv[2], ip) elif len(sys.argv) == 2: server = Server.from_file(sys.argv[1]) if server is not None: server.register(**subservers) server.run()
return None return answer[DNS] if answer is not None else None def recv(self, pkt: DNS): if pkt is not None: packet = Packet(pkt, self.domain) for i, (rrname, rdata) in enumerate(packet.answers): logger.info("Message %i (%s): %s", i, rrname, rdata) try: logger.info("Decoded: %s", Content.decode(rdata)) except Exception: logger.warning("Couldn't decode message") logger.debug(packet.dns.summary()) else: logger.warning("Packet was none, most likely timeout") if __name__ == "__main__": logger = init_logger() if len(sys.argv) < 2: logger.error("Usage: %s hostname [message]", sys.argv[0]) sys.exit(-1) ip = get_ip_from_hostname(sys.argv[1]) if ip is None: sys.exit(-1) client = Client(sys.argv[1], ip) pkt = client.send("hello world" if len(sys.argv) == 2 else sys.argv[2]) client.recv(pkt)