-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.py
46 lines (36 loc) · 1021 Bytes
/
common.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
import json
import socket
import logging
def encode(msg):
'''Converts dictionary to binary coded json-string which can be send over the network'''
json_str = json.dumps(msg)
return bytes(json_str, encoding='utf-8')
def decode(msg):
'''Decodes binary data to dictionary'''
result = None
try:
result = json.loads(msg.decode("utf-8"))
except Exception:
pass
return result
def load_config(fname):
with open(fname, 'r') as fd:
cfg = json.load(fd)
icfg = dict()
for key, item in cfg.items():
icfg[int(key)] = item
return icfg
def send_msg(msg, host, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((host, port))
sock.sendall(msg)
except ConnectionRefusedError:
logging.debug('ConnectionRefusedError')
finally:
sock.close()
def there_is_a_none(d):
for item in d.items():
if item is None:
return True
return False