|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a JSON based writer class. |
|
8 """ |
|
9 |
|
10 import json |
|
11 import socket |
|
12 |
|
13 |
|
14 class EricJsonWriter: |
|
15 """ |
|
16 Class implementing a JSON based writer class. |
|
17 """ |
|
18 def __init__(self, host, port): |
|
19 """ |
|
20 Constructor |
|
21 |
|
22 @param host IP address the reader is listening on |
|
23 @type str |
|
24 @param port port the reader is listening on |
|
25 @type int |
|
26 """ |
|
27 self.__connection = socket.create_connection((host, port)) |
|
28 |
|
29 def write(self, data): |
|
30 """ |
|
31 Public method to send JSON serializable data. |
|
32 |
|
33 @param data JSON serializable object to be sent |
|
34 @type object |
|
35 """ |
|
36 dataStr = json.dumps(data) + '\n' |
|
37 self.__connection.sendall(dataStr.encode('utf8', 'backslashreplace')) |
|
38 |
|
39 def close(self): |
|
40 """ |
|
41 Public method to close the stream. |
|
42 """ |
|
43 self.__connection.close() |