Add a few examples that show how to use the python module
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 8 Jan 2018 17:39:55 +0000 (17:39 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 8 Jan 2018 17:39:55 +0000 (17:39 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
examples/python/create-database.py [new file with mode: 0644]
examples/python/read-database.py [new file with mode: 0644]

index 5377410..c97f76b 100644 (file)
@@ -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 (file)
index 0000000..284c85f
--- /dev/null
@@ -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 (file)
index 0000000..2f59dec
--- /dev/null
@@ -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)