-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_by_size.py
59 lines (47 loc) · 1.58 KB
/
tcp_by_size.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
58
59
__author__ = 'Yossi'
# from tcp_by_size import send_with_size ,recv_by_size
SIZE_HEADER_FORMAT = "000000000|" # n digits for data size + one delimiter
size_header_size = len(SIZE_HEADER_FORMAT)
TCP_DEBUG = False
LEN_TO_PRINT = 100
VER = 'Python3'
def str_byte(_s,direction):
if VER == 'Python3':
if direction == 'encode':
return _s.encode()
else:
return _s.decode('utf8')
else:
return _s
def recv_by_size(sock):
size_header = b''
data_len = 0
while len(size_header) < size_header_size:
_s = sock.recv(size_header_size - len(size_header))
if _s == b'':
size_header = b''
break
size_header += _s
data = b''
if size_header != b'':
data_len = int(size_header[:size_header_size - 1])
while len(data) < data_len:
_d = sock.recv(data_len - len(data))
if _d == b'':
data = b''
break
data += _d
if TCP_DEBUG and size_header != b'':
print ("\nRecv(%s)>>>" % (size_header,), end='')
print ("%s"%(data[:min(len(data),LEN_TO_PRINT)],))
if data_len != len(data):
data=b'' # Partial data is like no data !
return data
def send_with_size(sock, bdata):
len_data = len(bdata)
header_data = str(len(bdata)).zfill(size_header_size - 1) + "|"
bytea = bytearray(header_data,encoding='utf8') + bdata
sock.send(bytea)
if TCP_DEBUG and len_data > 0:
print ("\nSent(%s)>>>" % (len_data,), end='')
print ("%s"%(bytea[:min(len(bytea),LEN_TO_PRINT)],))