-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
60 lines (47 loc) · 1.21 KB
/
server.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
import socket
import thread
class HbcServer(object):
port = 9531
def __init__ (self, size):
self.size = size
self.clilist = []
self.srvsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def __del__(self):
for element in self.clilist:
element[1].close()
self.srvsock.close()
def wait(self):
self.srvsock.bind(('', self.port))
self.srvsock.listen(self.size)
index = 0
while (index < self.size):
clisock, (clientIP, clientPort) = self.srvsock.accept()
self.clilist.append((index, clisock))
self.sendmsg(index, "index", "")
index = index + 1
def procforward(self, index, clisock):
while True:
while True:
clisock.recv(1024)
def sendmsg(self, index, msgtype, msg):
if (msgtype == "index"):
data = "index:" + str(index)
self.clilist[index][1].send(data)
def run(self):
while True:
data = self.srvsock.recv(1024)
self.handle(data)
def handle(self, data):
datalist = data.split(':', 1)
msgheader = datalist[0]
msgdata = datalist[1]
if (msgheader == "all"):
return False
if (msgheader == "die"):
return True
index = int(msgheader)
print str(index + 1)
if __name__ == "__main__":
server = HbcServer(2)
server.wait()
server.run()