Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 8 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions json_utils.py
Original file line number Diff line number Diff line change
@@ -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)
134 changes: 0 additions & 134 deletions jsonsocket.py

This file was deleted.