-
Notifications
You must be signed in to change notification settings - Fork 0
/
sgplib.py
57 lines (47 loc) · 1.38 KB
/
sgplib.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
55
56
57
import zlib
class Package:
def __init__(self):
pass
def encode(self, content, trytozip=True, zip_level=6):
if trytozip:
zipped = zlib.compress(content, zip_level)
self.raw = content
self.zipped = zipped
else:
self.raw = content
self.zipped = content
shouldzip = self.raw.__sizeof__() > self.zipped.__sizeof__()
if shouldzip:
data = self.zipped
else:
data = self.raw
self.zipped = shouldzip
self.data = bytes([int(shouldzip)])+data
return self.data
def decode(self, content):
cont = list(content)
zipped = bool(cont[0])
string = bytes(cont[1:])
self.zipped = zipped
if zipped:
inside = zlib.decompress(string)
else:
inside = string
self.raw = inside
return inside
class Socket:
def __init__(self, socket):
self.socket = socket
def recv(self, size):
data = self.socket.recv(size)
if not data:
raise IOError("Socket closed.")
temp = Package()
return temp.decode(data)
def send(self, data, ziplevel=6):
temp = Package()
zipped = temp.encode(data, ziplevel)
return self.socket.send(zipped)
@property
def fileno(self):
return self.socket.fileno