-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket_utils.py
54 lines (43 loc) · 1.5 KB
/
socket_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Documentation: https://docs.python.org/3/library/struct.html
# IMPORTANT NOTE: THIS FILE IS USED TO TEST FUNCTIONALITY AND WAS PROVIDED
# BY MATHEW BOLGER 'TUTE 8'
import socket
import json
import struct
class SocketUtils:
"""
Utility class for sending data via sockets.
"""
@staticmethod
def sendJson(socket, object):
"""
Sends a dictionary in JSON format through a given socket.
Args:
socket (socket): The socket to send data through.
object (dict): Dictionary object to JSONify and send.
"""
jsonString = json.dumps(object)
data = jsonString.encode("utf-8")
jsonLength = struct.pack("!i", len(data))
socket.sendall(jsonLength)
socket.sendall(data)
@staticmethod
def recvJson(socket):
"""
Receives a JSON object via socket and returns it as a dictionary.
Args:
socket (socket): The socket to send data through.
Returns:
Dictionary object generated from received JSON data.
"""
buffer = socket.recv(4)
jsonLength = struct.unpack("!i", buffer)[0]
# Reference: https://stackoverflow.com/a/15964489/9798310
buffer = bytearray(jsonLength)
view = memoryview(buffer)
while jsonLength:
nbytes = socket.recv_into(view, jsonLength)
view = view[nbytes:]
jsonLength -= nbytes
jsonString = buffer.decode("utf-8")
return json.loads(jsonString)