-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlibdeka.py
50 lines (39 loc) · 1015 Bytes
/
libdeka.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
# class generator like namedtuple, but not immutable
# https://stackoverflow.com/questions/3648442/python-how-to-define-a-structure-like-in-c/3648450#3648450
def Struct(name, fields):
fields = fields.split()
def init(self, *values):
for field, value in zip(fields, values):
self.__dict__[field] = value
cls = type(name, (object,), {'__init__': init})
return cls
def toascii(s):
return s.decode('ascii', 'ignore')
def fromascii(s):
return s.encode('ascii', 'ignore')
def sendascii(req, msg):
req.sendall(fromascii(msg))
def sendblob(req, blob):
req.sendall(blob)
def getline(req):
a = ''
i = 0
while 1:
if len(a) > 0:
if a[-1] == '\n':
break
b = req.recv(1)
if not b:
return None
a += toascii(b)
return a
def getdata(req, remaining):
d = b''
while remaining > 0:
r = req.recv(max(remaining, 4096))
remaining -= len(r)
d += r
return d
def encodemsg(header, data):
s = int(len(data)) + " " + header
return s + data