-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient_protocol.py
52 lines (44 loc) · 1.32 KB
/
client_protocol.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
import json, socket, time
from constants import *
from bz2 import compress, decompress
sock = socket.socket()
old_data = ""
old_json = []
def registerMe(name):
global sock, tcp_ip, tcp_port
inf = open('config.txt', 'r')
config = inf.readline()
tcp_ip, tcp_port = config.split(' ')
tcp_port = int(tcp_port)
sock.connect((tcp_ip, tcp_port))
data = sock.recv(MAX_LENGTH)
# id = json.loads(str(decompress(data), 'utf-8'))['id']
id = json.loads(str(data, 'utf-8'))['id']
jdata = dict()
jdata['name'] = name
s = json.dumps(jdata)
# sock.send(compress(bytes(s + '\n', 'utf-8')))
sock.send(bytes(s + '\n', 'utf-8'))
return id
def getField():
global sock, old_data, old_json
# data = decompress(sock.recv(MAX_LENGTH))
data = sock.recv(MAX_LENGTH)
data = str(data, 'utf-8')
s = old_data+data
l = s.split('\n')
old_data = l[-1]
if len(l) > 1:
old_json = json.loads(l[-2])
if DEBUG_PROTOCOL_PRINT:
print(old_json)
return old_json
def sendMe(p):
global sock
data = json.dumps(p)
# sock.send(compress(bytes(data + '\n', 'utf-8')))
sock.send(bytes(data + '\n', 'utf-8'))
def killMe():
global sock
#sock.send(bytes('killme', 'utf-8'))
sock.close()