eric7/EricNetwork/EricJsonStreamWriter.py

branch
eric7-maintenance
changeset 9131
bc0c1b6d6adb
diff -r 9967ae9f0906 -r bc0c1b6d6adb eric7/EricNetwork/EricJsonStreamWriter.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/eric7/EricNetwork/EricJsonStreamWriter.py	Sun Jun 05 17:17:44 2022 +0200
@@ -0,0 +1,43 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2022 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing a JSON based writer class.
+"""
+
+import json
+import socket
+
+
+class EricJsonWriter:
+    """
+    Class implementing a JSON based writer class.
+    """
+    def __init__(self, host, port):
+        """
+        Constructor
+        
+        @param host IP address the reader is listening on
+        @type str
+        @param port port the reader is listening on
+        @type int
+        """
+        self.__connection = socket.create_connection((host, port))
+    
+    def write(self, data):
+        """
+        Public method to send JSON serializable data.
+        
+        @param data JSON serializable object to be sent
+        @type object
+        """
+        dataStr = json.dumps(data) + '\n'
+        self.__connection.sendall(dataStr.encode('utf8', 'backslashreplace'))
+    
+    def close(self):
+        """
+        Public method to close the stream.
+        """
+        self.__connection.close()

eric ide

mercurial