-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_socket.py
More file actions
70 lines (58 loc) · 1.79 KB
/
server_socket.py
File metadata and controls
70 lines (58 loc) · 1.79 KB
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
#coding = utf-8
import socket
import threading
import Queue
msg_queue = Queue.Queue()
client_dict = {}
class Client(threading.Thread):
def __init__(self, conn):
self.conn = conn
threading.Thread.__init__(self)
def run(self):
global msg_queue, client_dict
while True:
data = self.conn.recv(65535)
try:
if data[0] == '/':
client_dict[data[1:]] = self.conn
else:
msg_queue.put(data)
except Exception, error:
print error
break
class Msg_Consumer(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global msg_queue, client_dict
while True:
data = msg_queue.get()
try:
try:
suid, duid, content = data.split(',')
print suid, duid, content
client_dict[duid].send(','.join([suid, content]))
except Exception, error:
suid, content = data.split(',')
self.send_all(suid, data)
except Exception, error:
print error
break
def send_all(self, suid, data):
for uid, client in client_dict.items():
if uid != suid:
client.send(data.strip())
HOST = '127.0.0.1'
PORT = 38557
if __name__ == '__main__':
my_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
my_server.bind((HOST, PORT))
my_server.listen(3)
msg_consumer = Msg_Consumer()
msg_consumer.start()
while True:
conn, client_addr = my_server.accept()
conn.send('succeed')
client = Client(conn)
client.start()
conn.close()