From: Michael Tremer Date: Mon, 8 Jan 2018 17:39:55 +0000 (+0000) Subject: Add a few examples that show how to use the python module X-Git-Tag: 0.9.0~107 X-Git-Url: http://git.ipfire.org/?p=people%2Fms%2Flibloc.git;a=commitdiff_plain;h=e51e1dec98ea88343941dd306b0cf4ace4039d71 Add a few examples that show how to use the python module Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 5377410..c97f76b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -34,6 +34,10 @@ pkgconfigdir = $(libdir)/pkgconfig %.pc: %.pc.in Makefile $(SED_PROCESS) +EXTRA_DIST += \ + examples/python/create-database.py \ + examples/python/read-database.py + pkginclude_HEADERS = \ src/loc/libloc.h \ src/loc/as.h \ diff --git a/examples/python/create-database.py b/examples/python/create-database.py new file mode 100644 index 0000000..284c85f --- /dev/null +++ b/examples/python/create-database.py @@ -0,0 +1,27 @@ +#!/usr/bin/python3 + +import location + +w = location.Writer() + +# Set the vendor +w.vendor = "IPFire Project" + +# Set a description +w.description = "This is a geo location database" + +# Add an AS +a = w.add_as(204867) +a.name = "Lightning Wire Labs GmbH" + +print(a) + +# Add a network +n = w.add_network("2a07:1c44:5800::/40") +n.country_code = "DE" +n.asn = a.number + +print(n) + +# Write the database to disk +w.write("test.db") diff --git a/examples/python/read-database.py b/examples/python/read-database.py new file mode 100644 index 0000000..2f59dec --- /dev/null +++ b/examples/python/read-database.py @@ -0,0 +1,19 @@ +#!/usr/bin/python3 + +import location + +# Open the database +d = location.Database("test.db") +print(d) + +# Try to get information about AS123 +a = d.get_as(123) +print(a) + +# Try to get information about AS204867 +a = d.get_as(204867) +print(a) + +# Search for an IP address in the database +n = d.lookup("8.8.8.8") +print(n)