-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultithreadserver.py
More file actions
148 lines (121 loc) · 3.8 KB
/
multithreadserver.py
File metadata and controls
148 lines (121 loc) · 3.8 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python
"""
only socket multi server
now error:
dont close on receive thread
soulution: multiprocess
"""
from socket import *
import os
import threading
from time import ctime
HOST = 'localhost'
PORT = 8000
BUFSIZE = 1024
ADDR = (HOST, PORT)
LIMIT = 16
#shared object along threads
class shareob(object):
def __init__(self):
self.data = None
self.users = []
self.isrun = True
#this thread have only one socket client
class submessage(threading.Thread):
def __init__(self, group=None, target=None, name=None,
args=(), kwargs=None,objects=None):
threading.Thread.__init__(
self, group=group,
target=target, name=name)
self.number = args
self.kwargs = kwargs
self.sock = objects
self.__isrun = True
return
def run(self):
log = open('pid','a')
log.writelines(str(os.getpid())+ '\n')
log.close()
while self.__isrun:
self.sock.data = self.sock.users[self.number].recv(BUFSIZE)
if not self.sock.data:
break
elif self.sock.data.decode('utf-8') == 'exit':
self.__isrun = False
self.sock.users[self.number].close()
break
return
#send message all client
class mysendmessage(threading.Thread):
def __init__(self, group=None, target=None, name=None,
args=(), kwargs=None,objects=None):
threading.Thread.__init__(
self, group=group,
target=target, name=name)
self.args = args
self.kwargs = kwargs
self.sock = objects
self.__isrun = True
return
def run(self):
log = open('pid','a')
log.writelines(str(os.getpid())+ '\n')
log.close()
while self.__isrun:
if self.sock.data:
if self.sock.data == 'out':
self.sock.isrun = False
for idx in range(0, len(self.sock.users)):
self.sock.users[idx].send(bytes('[%s] %s' %(ctime(), self.sock.data.decode('utf-8')), 'utf-8'))
self.sock.data = None
return
#only listen and fork sub message thread
class mysocket(threading.Thread):
def __init__(self, group=None, target=None, name=None,
args=(), kwargs=None,objects=None):
threading.Thread.__init__(
self, group=group,
target=target, name=name)
self.args = args
self.kwargs = kwargs
self.objects = objects
self.__isrun = True
self.tcpSersock = socket(AF_INET, SOCK_STREAM)
self.tcpSersock.bind(ADDR)
self.tcpSersock.listen(LIMIT)
return
def run(self):
submes = []
while self.__isrun:
print('waiting for connections')
tcpCilsock, addr = self.tcpSersock.accept()
print('connected from ', addr)
self.objects.users.append(tcpCilsock)
sub = submessage(objects=self.objects, args=len(submes))
submes.append(sub)
sub.start()
if self.objects.data is not None:
if self.objects.data.decode('utf-8') == 'out':
self.__isrun = False
for out in submes:
out.stop()
break
self.tcpSersock.close()
return
def main():
sock = shareob()
log = open('pid','w')
log.writelines(str(os.getpid())+ '\n')
log.close()
my = mysocket(objects=sock)
sendme = mysendmessage(objects=sock)
my.setDaemon(True)
sendme.setDaemon(True)
print('start thread')
my.start()
sendme.start()
sendme.join()
my.join()
print('end thread')
if __name__=='__main__':
main()