From 685116565dd588f613576c1d73a60ce5be39f34a Mon Sep 17 00:00:00 2001 From: chris-rose-one Date: Wed, 6 Apr 2016 20:11:27 +1000 Subject: [PATCH 1/3] Delete jsonsocket.py --- jsonsocket.py | 134 -------------------------------------------------- 1 file changed, 134 deletions(-) delete mode 100644 jsonsocket.py diff --git a/jsonsocket.py b/jsonsocket.py deleted file mode 100644 index 9dfe7a6..0000000 --- a/jsonsocket.py +++ /dev/null @@ -1,134 +0,0 @@ -import json, socket - -class Server(object): - """ - A JSON socket server used to communicate with a JSON socket client. All the - data is serialized in JSON. How to use it: - - server = Server(host, port) - while True: - server.accept() - data = server.recv() - # shortcut: data = server.accept().recv() - server.send({'status': 'ok'}) - """ - - backlog = 5 - client = None - - def __init__(self, host, port): - self.socket = socket.socket() - self.socket.bind((host, port)) - self.socket.listen(self.backlog) - - def __del__(self): - self.close() - - def accept(self): - # if a client is already connected, disconnect it - if self.client: - self.client.close() - self.client, self.client_addr = self.socket.accept() - return self - - def send(self, data): - if not self.client: - raise Exception('Cannot send data, no client is connected') - _send(self.client, data) - return self - - def recv(self): - if not self.client: - raise Exception('Cannot receive data, no client is connected') - return _recv(self.client) - - def close(self): - if self.client: - self.client.close() - self.client = None - if self.socket: - self.socket.close() - self.socket = None - - -class Client(object): - """ - A JSON socket client used to communicate with a JSON socket server. All the - data is serialized in JSON. How to use it: - - data = { - 'name': 'Patrick Jane', - 'age': 45, - 'children': ['Susie', 'Mike', 'Philip'] - } - client = Client() - client.connect(host, port) - client.send(data) - response = client.recv() - # or in one line: - response = Client().connect(host, port).send(data).recv() - """ - - socket = None - - def __del__(self): - self.close() - - def connect(self, host, port): - self.socket = socket.socket() - self.socket.connect((host, port)) - return self - - def send(self, data): - if not self.socket: - raise Exception('You have to connect first before sending data') - _send(self.socket, data) - return self - - def recv(self): - if not self.socket: - raise Exception('You have to connect first before receiving data') - return _recv(self.socket) - - def recv_and_close(self): - data = self.recv() - self.close() - return data - - def close(self): - if self.socket: - self.socket.close() - self.socket = None - - -## helper functions ## - -def _send(socket, data): - try: - serialized = json.dumps(data) - except (TypeError, ValueError), e: - raise Exception('You can only send JSON-serializable data') - # send the length of the serialized data first - socket.send('%d\n' % len(serialized)) - # send the serialized data - socket.sendall(serialized) - -def _recv(socket): - # read the length of the data, letter by letter until we reach EOL - length_str = '' - char = socket.recv(1) - while char != '\n': - length_str += char - char = socket.recv(1) - total = int(length_str) - # use a memoryview to receive the data chunk by chunk efficiently - view = memoryview(bytearray(total)) - next_offset = 0 - while total - next_offset > 0: - recv_size = socket.recv_into(view[next_offset:], total - next_offset) - next_offset += recv_size - try: - deserialized = json.loads(view.tobytes()) - except (TypeError, ValueError), e: - raise Exception('Data received was not in JSON format') - return deserialized From 2ad35d77b1fdb7c5230a4c6c9339f336e129e433 Mon Sep 17 00:00:00 2001 From: chris-rose-one Date: Wed, 6 Apr 2016 20:12:29 +1000 Subject: [PATCH 2/3] Added files via upload --- json_utils.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 json_utils.py diff --git a/json_utils.py b/json_utils.py new file mode 100644 index 0000000..cccd74a --- /dev/null +++ b/json_utils.py @@ -0,0 +1,45 @@ +import json + +def send_json(socket, data): + try: + serialized = json.dumps(data) + except (TypeError, ValueError) as e: + raise Exception('You can only send JSON-serializable data') + # send the length of the serialized data first + socket.send(('%d\n').encode() % len(serialized)) + # send the serialized data + socket.sendall(serialized.encode()) + +def receive_json(socket): + # read the length of the data, letter by letter until we reach EOL + length_str = '' + char = socket.recv(1).decode() + if char == '': + return char + while char != '\n': + length_str += char + char = socket.recv(1).decode() + total = int(length_str) + print(total) + # use a memoryview to receive the data chunk by chunk efficiently + view = memoryview(bytearray(total)) + next_offset = 0 + while total - next_offset > 0: + recv_size = socket.recv_into(view[next_offset:], total - next_offset) + next_offset += recv_size + try: + hexdump(view) + deserialized = json.loads(view.tobytes().decode()) + except (TypeError, ValueError) as e: + raise Exception('Data received was not in JSON format') + return deserialized + +def hexdump(src, length=16): + result = [] + digits = 4 if isinstance(src, unicode) else 2 + for i in xrange(0, len(src), length): + s = src[i:i+length] + hexa = b' '.join(["%0*X" % (digits, ord(x)) for x in s]) + text = b''.join([x if 0x20 <= ord(x) < 0x7F else b'.' for x in s]) + result.append( b"%04X %-*s %s" % (i, length*(digits + 1), hexa, text) ) + print b'\n'.join(result) \ No newline at end of file From 1df9f87269ff8e0b0af958009667c2a83020dac0 Mon Sep 17 00:00:00 2001 From: chris-rose-one Date: Wed, 6 Apr 2016 20:35:17 +1000 Subject: [PATCH 3/3] Update README.md --- README.md | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 3dae46a..0df83dd 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,14 @@ -jsonsocket -========== +json socket utility +=================== -This is a small Python library for sending data over sockets. +This is a simple python utility for sending json data when using sockets. -It allows sending lists, dictionaries, strings, etc. It can handle very large data (I've tested it with 10GB of data). Any JSON-serializable data is accepted. +It allows sending lists, dictionaries, strings, etc. It can handle very large data (It has been tested with 10GB of data). Any JSON-serializable data is accepted. -Examples: +these two functions don't require re-write/over-writing the socket library and are python3 compatible. -```python -from jsonsocket import Client, Server +get a taste for using sockets here: http://tutorialspoint.com/python/python_networking.htm -host = 'localhost' -port = '8000' +an example of working with json data over sockets can be found here: http://github.com/chris-rose-one/battleship -# Client code: -client = Client() -client.connect(host, port).send({'some_list': [123, 456]}) -response = client.recv() -# response now is {'data': {'some_list': [123, 456]}} -client.close() - - -# Server code: -server = Server(host, port) -server.accept() -data = server.recv() -# data now is: {'some_list': [123, 456]} -server.send({'data': data}).close() - -``` +thanks @mdebbar this code has been quite helpful