Open 710csm opened 5 years ago
import socket, threading
class TCPServerThread(threading.Thread): def init(self, commandQueue, tcpServerThreads, connections, connection, clientAddress): threading.Thread.init(self)
self.commandQueue = commandQueue
self.tcpServerThreads = tcpServerThreads
self.connections = connections
self.connection = connection
self.clientAddress = clientAddress
def run(self):
try:
while True:
data = self.connection.recv(1024).decode()
# when break connection
if not data:
print 'tcp server :: exit :',self.connection
break
print 'tcp server :: client :', data
self.commandQueue.put(data)
except:
self.connections.remove(self.connection)
self.tcpServerThreads.remove(self)
exit(0)
self.connections.remove(self.connection)
self.tcpServerThreads.remove(self)
def send(self, message):
print 'tcp server :: ',message
try:
for i in range(len(self.connections)):
self.connections[i].sendall(message.encode())
except:
pass
import socket, threading
class TCPServerThread(threading.Thread): def init(self, commandQueue, tcpServerThreads, connections, connection, clientAddress): threading.Thread.init(self)
출처: http://mititch.tistory.com/36 [미티치의 삽질 블로그]