From 3a1afb94344cacb024ad2eb8fb7c006d64b1e854 Mon Sep 17 00:00:00 2001
From: Toni Uhlig <matzeton@googlemail.com>
Date: Wed, 20 Jan 2021 16:10:16 +0100
Subject: Added simple python json out example used for comparing JSON output.

Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
---
 examples/py-json-stdout/json-stdout.py | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
 create mode 100755 examples/py-json-stdout/json-stdout.py

(limited to 'examples/py-json-stdout')

diff --git a/examples/py-json-stdout/json-stdout.py b/examples/py-json-stdout/json-stdout.py
new file mode 100755
index 000000000..ac94d707f
--- /dev/null
+++ b/examples/py-json-stdout/json-stdout.py
@@ -0,0 +1,34 @@
+#!/usr/bin/env python3
+
+import os
+import sys
+
+sys.path.append(os.path.dirname(sys.argv[0]) + '/../../dependencies')
+import nDPIsrvd
+from nDPIsrvd import nDPIsrvdSocket, TermColor
+
+
+def parse_json_str(json_str):
+
+    j = nDPIsrvd.JsonParseBytes(json_str[0])
+    nDPIdEvent = nDPIsrvd.nDPIdEvent.validateJsonEventTypes(j)
+    if nDPIdEvent.isValid is False:
+        raise RuntimeError('Missing event id or event name invalid in the JSON string: {}'.format(j))
+    print(j)
+
+if __name__ == '__main__':
+    argparser = nDPIsrvd.defaultArgumentParser()
+    args = argparser.parse_args()
+    address = nDPIsrvd.validateAddress(args)
+
+    sys.stderr.write('Recv buffer size: {}\n'.format(nDPIsrvd.NETWORK_BUFFER_MAX_SIZE))
+    sys.stderr.write('Connecting to {} ..\n'.format(address[0]+':'+str(address[1]) if type(address) is tuple else address))
+
+    nsock = nDPIsrvdSocket()
+    nsock.connect(address)
+
+    while True:
+        received = nsock.receive()
+        for received_json_pkt in received:
+            parse_json_str(received_json_pkt)
+
-- 
cgit v1.2.3