-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
112 lines (82 loc) · 1.85 KB
/
client.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import socket
class HbcClient(object):
port = 9531
def __init__(self):
self.index = -1
self.clisocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.step = 0
def __del__(self):
self.clisocket.close()
def getServerIP(self):
return "127.0.0.1"
def connect(self, ip):
self.clisocket.connect((ip, self.port))
def read(self, filename):
fp = open("test.txt", r)
data = ""
while True:
tmp = fp.read(1024)
if (tmp != ""):
data = data + tmp
else:
break
def parsedata(self, data):
pass
def run(self):
while (self.step <= 7):
print "step ", self.step
if (self.step == 0):
data = self.getindex()
if (self.step == 1):
data = self.step1()
if (self.step == 2):
data = self.step2()
if (self.step == 3):
data = self.step3()
if (self.step == 4):
data = self.step4()
if (self.step == 5):
data = self.step5()
if (self.step == 6):
data = self.step6()
if (self.step == 7):
data = self.step7()
self.step = self.step + 1
def sendmsg(self, index, msgheader, msgdata):
#first packet
header = str(index) + ":" + msgheader
data = header + msgdata[0:1024]
i = 1024
#more than one
while (i < len(msgdata)):
data = str(index)+ ":" + msgdata[i:i + 1024]
self.clisock.send(data)
i = i + 1024
def recvmsg(self):
while True:
data = self.clisocket.recv(1024)
datalist = data.split(':', 1)
msgheader = datalist[0]
msgdata = datalist[1]
return (msgheader, msgdata)
def getindex():
(msgheader, msgdata) = self.recvmsg()
if (msgheader == "index"):
self.index = int(msgdata)
def step1(self):
pass
def step2(self):
pass
def step3(self):
pass
def step4(self):
pass
def step5(self):
pass
def step6(self):
pass
def step7(self):
pass
#if __name__ == "__main__":
# client = HbcClient()
# client.connect(client.getServerIP())