jsocket 1.9.0__py3-none-any.whl → 1.9.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- jsocket/jsocket_base.py +12 -8
- jsocket/tserver.py +17 -12
- {jsocket-1.9.0.dist-info → jsocket-1.9.2.dist-info}/METADATA +1 -4
- jsocket-1.9.2.dist-info/RECORD +8 -0
- {jsocket-1.9.0.dist-info → jsocket-1.9.2.dist-info}/WHEEL +1 -1
- jsocket-1.9.0.dist-info/RECORD +0 -8
- {jsocket-1.9.0.dist-info → jsocket-1.9.2.dist-info}/LICENSE +0 -0
- {jsocket-1.9.0.dist-info → jsocket-1.9.2.dist-info}/top_level.txt +0 -0
jsocket/jsocket_base.py
CHANGED
|
@@ -31,10 +31,10 @@ import time
|
|
|
31
31
|
logger = logging.getLogger("jsocket")
|
|
32
32
|
|
|
33
33
|
class JsonSocket(object):
|
|
34
|
-
def __init__(self, address='127.0.0.1', port=5489):
|
|
34
|
+
def __init__(self, address='127.0.0.1', port=5489, timeout=2.0):
|
|
35
35
|
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
36
36
|
self.conn = self.socket
|
|
37
|
-
self._timeout =
|
|
37
|
+
self._timeout = timeout
|
|
38
38
|
self._address = address
|
|
39
39
|
self._port = port
|
|
40
40
|
|
|
@@ -74,17 +74,21 @@ class JsonSocket(object):
|
|
|
74
74
|
return json.loads(str(msg[0],'ascii'))
|
|
75
75
|
|
|
76
76
|
def close(self):
|
|
77
|
+
logger.debug("closing all connections")
|
|
78
|
+
self._close_connection()
|
|
77
79
|
self._close_socket()
|
|
78
|
-
|
|
79
|
-
self._close_connection()
|
|
80
|
-
|
|
80
|
+
|
|
81
81
|
def _close_socket(self):
|
|
82
82
|
logger.debug("closing main socket")
|
|
83
|
-
self.socket.
|
|
83
|
+
if self.socket.fileno() != -1:
|
|
84
|
+
self.socket.shutdown(socket.SHUT_RDWR)
|
|
85
|
+
self.socket.close()
|
|
84
86
|
|
|
85
87
|
def _close_connection(self):
|
|
86
88
|
logger.debug("closing the connection socket")
|
|
87
|
-
self.conn.
|
|
89
|
+
if self.conn.fileno() != -1:
|
|
90
|
+
self.conn.shutdown(socket.SHUT_RDWR)
|
|
91
|
+
self.conn.close()
|
|
88
92
|
|
|
89
93
|
def _get_timeout(self):
|
|
90
94
|
return self._timeout
|
|
@@ -152,7 +156,7 @@ class JsonClient(JsonSocket):
|
|
|
152
156
|
return True
|
|
153
157
|
return False
|
|
154
158
|
|
|
155
|
-
|
|
159
|
+
|
|
156
160
|
if __name__ == "__main__":
|
|
157
161
|
""" basic json echo server """
|
|
158
162
|
import threading
|
jsocket/tserver.py
CHANGED
|
@@ -20,7 +20,7 @@ __copyright__= """
|
|
|
20
20
|
GNU General Public License for more details.
|
|
21
21
|
|
|
22
22
|
You should have received a copy of the GNU General Public License
|
|
23
|
-
along with tserver module.
|
|
23
|
+
along with tserver module. If not, see <http://www.gnu.org/licenses/>."""
|
|
24
24
|
__version__ = "1.0.2"
|
|
25
25
|
|
|
26
26
|
import jsocket.jsocket_base as jsocket_base
|
|
@@ -42,8 +42,8 @@ class ThreadedServer(threading.Thread, jsocket_base.JsonServer):
|
|
|
42
42
|
|
|
43
43
|
This method is called every time a JSON object is received from a client
|
|
44
44
|
|
|
45
|
-
@param obj
|
|
46
|
-
@retval
|
|
45
|
+
@param obj JSON "key: value" object received from client
|
|
46
|
+
@retval None or a response object
|
|
47
47
|
"""
|
|
48
48
|
pass
|
|
49
49
|
|
|
@@ -61,7 +61,10 @@ class ThreadedServer(threading.Thread, jsocket_base.JsonServer):
|
|
|
61
61
|
while self._isAlive:
|
|
62
62
|
try:
|
|
63
63
|
obj = self.read_obj()
|
|
64
|
-
self._process_message(obj)
|
|
64
|
+
resp_obj = self._process_message(obj)
|
|
65
|
+
if resp_obj is not None:
|
|
66
|
+
logger.debug("message has a response")
|
|
67
|
+
self.send_obj(resp_obj)
|
|
65
68
|
except socket.timeout as e:
|
|
66
69
|
logger.debug("socket.timeout: %s" % e)
|
|
67
70
|
continue
|
|
@@ -69,7 +72,7 @@ class ThreadedServer(threading.Thread, jsocket_base.JsonServer):
|
|
|
69
72
|
logger.exception(e)
|
|
70
73
|
self._close_connection()
|
|
71
74
|
break
|
|
72
|
-
self.
|
|
75
|
+
self._close_socket()
|
|
73
76
|
|
|
74
77
|
def start(self):
|
|
75
78
|
""" Starts the threaded server.
|
|
@@ -100,7 +103,7 @@ class ServerFactoryThread(threading.Thread, jsocket_base.JsonSocket):
|
|
|
100
103
|
""" Swaps the existing socket with a new one. Useful for setting socket after a new connection.
|
|
101
104
|
|
|
102
105
|
@param new_sock socket to replace the existing default jsocket.JsonSocket object
|
|
103
|
-
@retval
|
|
106
|
+
@retval None
|
|
104
107
|
"""
|
|
105
108
|
del self.socket
|
|
106
109
|
self.socket = new_sock
|
|
@@ -108,21 +111,23 @@ class ServerFactoryThread(threading.Thread, jsocket_base.JsonSocket):
|
|
|
108
111
|
|
|
109
112
|
def run(self):
|
|
110
113
|
""" Should exit when client closes socket conn.
|
|
111
|
-
|
|
114
|
+
Can force an exit with force_stop.
|
|
112
115
|
"""
|
|
113
116
|
while self._isAlive:
|
|
114
117
|
try:
|
|
115
118
|
obj = self.read_obj()
|
|
116
|
-
self._process_message(obj)
|
|
119
|
+
resp_obj = self._process_message(obj)
|
|
120
|
+
if resp_obj is not None:
|
|
121
|
+
logger.debug("message has a response")
|
|
122
|
+
self.send_obj(resp_obj)
|
|
117
123
|
except socket.timeout as e:
|
|
118
124
|
logger.debug("socket.timeout: %s" % e)
|
|
119
125
|
continue
|
|
120
126
|
except Exception as e:
|
|
121
|
-
logger.info("client connection broken,
|
|
122
|
-
self._close_connection()
|
|
127
|
+
logger.info("client connection broken, exit and close connection socket")
|
|
123
128
|
self._isAlive = False
|
|
124
129
|
break
|
|
125
|
-
self.
|
|
130
|
+
self._close_connection()
|
|
126
131
|
|
|
127
132
|
def start(self):
|
|
128
133
|
""" Starts the factory thread.
|
|
@@ -136,7 +141,7 @@ class ServerFactoryThread(threading.Thread, jsocket_base.JsonSocket):
|
|
|
136
141
|
|
|
137
142
|
def force_stop(self):
|
|
138
143
|
""" Force stops the factory thread.
|
|
139
|
-
|
|
144
|
+
Should exit when client socket is closed under normal conditions.
|
|
140
145
|
The life of the dead is in the memory of the living.
|
|
141
146
|
|
|
142
147
|
@retval None
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: jsocket
|
|
3
|
-
Version: 1.9.
|
|
3
|
+
Version: 1.9.2
|
|
4
4
|
Summary: Python JSON Server & Client
|
|
5
5
|
Home-page: https://cpiekarski.com/2012/01/25/python-json-client-server-redux/
|
|
6
6
|
Author: Christopher Piekarski
|
|
@@ -9,7 +9,6 @@ Maintainer: Christopher Piekarski
|
|
|
9
9
|
Maintainer-email: chris@cpiekarski.com
|
|
10
10
|
License: OSI Approved Apache Software License
|
|
11
11
|
Keywords: json,socket,server,client
|
|
12
|
-
Platform: UNKNOWN
|
|
13
12
|
Classifier: Intended Audience :: Developers
|
|
14
13
|
Classifier: License :: OSI Approved :: Apache Software License
|
|
15
14
|
Classifier: Programming Language :: Python :: 3.9
|
|
@@ -23,5 +22,3 @@ Provides: jsocket
|
|
|
23
22
|
Requires-Python: >=3.9
|
|
24
23
|
License-File: LICENSE
|
|
25
24
|
|
|
26
|
-
UNKNOWN
|
|
27
|
-
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
jsocket/__init__.py,sha256=wDphi2h6wD6vC-DYa4mOu9qqefhOuLAdMp47YTcm0mM,2555
|
|
2
|
+
jsocket/jsocket_base.py,sha256=yUF0OYT8PcrxCqABEB9SaIR7aqYHd9_m1GgaoqlIEn4,5470
|
|
3
|
+
jsocket/tserver.py,sha256=pr9xNWdc3UU_9F1AQKBObCoubCEexZOceHobvZmaXa4,5770
|
|
4
|
+
jsocket-1.9.2.dist-info/LICENSE,sha256=r9OGvUawVBQpodY7BwMPTYja9PZebyIOvfw9_xhtMYc,828
|
|
5
|
+
jsocket-1.9.2.dist-info/METADATA,sha256=oIohjMr9i5-g5tH6UfMtb-loH5PqtEtKWRx-9-2zz5Q,956
|
|
6
|
+
jsocket-1.9.2.dist-info/WHEEL,sha256=AtBG6SXL3KF_v0NxLf0ehyVOh0cold-JbJYXNGorC6Q,92
|
|
7
|
+
jsocket-1.9.2.dist-info/top_level.txt,sha256=QqfmeUi7avy9cdcsVVvG68CP-4mfg_P6E7OuBuNEcN4,8
|
|
8
|
+
jsocket-1.9.2.dist-info/RECORD,,
|
jsocket-1.9.0.dist-info/RECORD
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
jsocket/__init__.py,sha256=wDphi2h6wD6vC-DYa4mOu9qqefhOuLAdMp47YTcm0mM,2555
|
|
2
|
-
jsocket/jsocket_base.py,sha256=SCbgwOfMFEd5kyTQ8QV7u-0Pu9VEi3i0e_2A0-K7pG4,5304
|
|
3
|
-
jsocket/tserver.py,sha256=lD5vJdNLyQUBNjqXaYFkG-XA46Vma0Fdjopb9JumCAk,5518
|
|
4
|
-
jsocket-1.9.0.dist-info/LICENSE,sha256=r9OGvUawVBQpodY7BwMPTYja9PZebyIOvfw9_xhtMYc,828
|
|
5
|
-
jsocket-1.9.0.dist-info/METADATA,sha256=6cFHPDyy7A-FylMNm2kxy5OHcDwv2wgrox0owGm7wCk,983
|
|
6
|
-
jsocket-1.9.0.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
|
|
7
|
-
jsocket-1.9.0.dist-info/top_level.txt,sha256=QqfmeUi7avy9cdcsVVvG68CP-4mfg_P6E7OuBuNEcN4,8
|
|
8
|
-
jsocket-1.9.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|