def test_asn_bootstrap_cache(tmpdir, asn): config = dict( self_bootstrap=True, bootstrap_dir=tmpdir, ) asn_db_file = os.path.join(tmpdir, "asn.json") # check download and cache rdapc = rdap.RdapClient(config) rdapc.get_asn(asn) assert os.path.isfile(asn_db_file) assert rdapc.history[0][0] == "https://data.iana.org/rdap/asn.json" # delete history and get again to test lookup caching rdapc._history = [] rdapc.get_asn(asn) print(rdapc.history) assert rdapc.history[0][0] != "https://data.iana.org/rdap/asn.json" assert 1 == len(rdapc.history) # delete cache and manually write file os.remove(asn_db_file) assert not os.path.isfile(asn_db_file) rdapc.write_bootstrap_data("asn") assert os.path.isfile(asn_db_file) rdapc._history = [] rdapc.get_asn(asn) print(rdapc.history) assert rdapc.history[0][0] != "https://data.iana.org/rdap/asn.json" assert 1 == len(rdapc.history)
def test_asn_lookup(this_dir, asn, tmpdir): config_dir = os.path.join(this_dir, "data", "iana") shutil.copy(os.path.join(config_dir, "config.yml"), tmpdir) # check download and cache rdapc = rdap.RdapClient(config_dir=tmpdir) rdapc.get_asn(asn) assert rdapc.history[0][0] == "https://data.iana.org/rdap/asn.json" # delete history and get again to test lookup caching rdapc._history = [] rdapc.get_asn(asn) print(rdapc.history) assert rdapc.history[0][0] != "https://data.iana.org/rdap/asn.json" assert 1 == len(rdapc.history) asn_db_file = os.path.join(tmpdir, "bootstrap", "asn.json") assert os.path.isfile(asn_db_file) # delete cache and manually write file os.remove(os.path.join(tmpdir, "bootstrap", "asn.json")) assert not os.path.isfile(asn_db_file) rdapc.write_bootstrap_data("asn") assert os.path.isfile(asn_db_file) rdapc._history = [] rdapc.get_asn(asn) print(rdapc.history) assert rdapc.history[0][0] != "https://data.iana.org/rdap/asn.json" assert 1 == len(rdapc.history)
def test_asn_lookup_notfound(this_dir, asn): config_dir = os.path.join(this_dir, "data", "iana") rdapc = rdap.RdapClient(config_dir=config_dir) with pytest.raises(RdapNotFoundError) as excinfo: rdapc.get_asn(asn) assert "No service found for AS" in str(excinfo.value)
def test_asn_bootstrap_no_cache(asn): config = dict( self_bootstrap=True, ) # check download and cache rdapc = rdap.RdapClient(config) rdapc.get_asn(asn) assert rdapc.history[0][0] == "https://data.iana.org/rdap/asn.json"
def main(argv=None): if argv is None: argv = sys.argv[1:] ctx = Context() parser = argparse.ArgumentParser(description="rdap") add_options(parser, Context.option_list()) parser.add_argument( "--version", action="version", version="{}s version {}".format("%(prog)", rdap.__version__), ) parser.add_argument("--output-format", help="output format (yaml, json, text)") parser.add_argument("--show-requests", action="store_true", help="show all requests") parser.add_argument("--parse", action="store_true", help="parse data into object before display") parser.add_argument("query", nargs="+") args = parser.parse_args(argv) # get dict of options and update config argd = vars(args) ctx.update_options(argd) client = rdap.RdapClient(ctx.config) output_format = argd.get("output_format") if not output_format: output_format = ctx.config.get_nested("rdap", "output_format") codec = munge.get_codec(output_format)() for each in argd["query"]: obj = client.get(each) if argd.get("parse", False): print(codec.dumps(obj.parsed())) else: print(codec.dumps(obj.data)) if argd.get("show_requests", False): print("# Requests") for each in client.history: print("{} {}".format(*each))
def test_init(): rdap.RdapClient()
def rdapc(): return rdap.RdapClient({"timeout": 10})