-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.py
More file actions
207 lines (156 loc) · 5.63 KB
/
Server.py
File metadata and controls
207 lines (156 loc) · 5.63 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import socket
import threading
import interface
from tkinter import *
import time
"""
## init the server
socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
socket.bind(('',8558))
## init the windows
"""
all_client = []
print("1yay")
class UpdateListBox(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.totalpseudo = ""
self.is_running = True
def givepara(self, listbox):
self.listbox = listbox
def terminate(self):
self.is_running = False
def run(self):
while self.is_running:
tot = ""
for conn, client in all_client:
if client.pseudo == "":
ip,port = client.address
tot += ip
else:
tot += client.pseudo[1:-1]
if tot != self.totalpseudo:
self.totalpseudo = tot
self.listbox.delete(0,END)
for conn, client in all_client:
if client.pseudo == "":
ip, port = client.address
self.listbox.insert(END,ip)
else:
self.listbox.insert(END,client.pseudo[1:-1])
class ThreadChat(threading.Thread):
def __init__(self,conn,log,socket,address):
global all_client
threading.Thread.__init__(self)
self.socket = socket
self.address = address
self.conn = conn
self.log = log
self.pseudo = ""
self.mute = False
self.is_running = True
def terminate(self):
self.is_running = False
def run(self):
global all_client
for i in all_client:
print(i)
while self.is_running :
#print(self.mute)
if not self.mute:
#print("france")
message = self.conn.recv(4096)
message = message.decode("utf8")
#print(message[0:7])
if message[0:8] == "pseudo :":
self.pseudo = "["+message[8:]+"]"
#print(self.pseudo)
elif message[0:] == "/rquit":
for i in range (len(all_client)):
__conn, __client = all_client[i]
if __client.pseudo == self.pseudo:
__conn.close()
all_client.pop(i)
self.terminate()
else:
message = self.pseudo+ message
self.log.insert(END,message)
self.SendAllClient(message)
else :
self.conn.send("/rmuteuser".encode("utf8"))
time.sleep(30)
self.mute = False
def UnMute(self):
self.mute = False
def SendAllClient(self,message):
global all_client
if message != "" and message != "\n" :
for conn, client in all_client:
conn.send(message.encode("utf8"))
class ServThread (threading.Thread):
def __init__(self,host = "192.168.1.19", port = 855):
global all_client
threading.Thread.__init__(self)
self.ListClient = []
self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.socket.bind((host,port))
self.is_running = True
self.update = UpdateListBox()
def terminate(self):
self.update.terminate()
self.is_running = False
def GiveParameter(self,log, listbox):
self.log = log
self.update.givepara(listbox)
self.update.start()
def run(self):
global all_client
while self.is_running:
self.socket.listen()
self.conn, self.address = self.socket.accept()
self.client = ThreadChat(self.conn, self.log,self.socket,self.address)
self.ListClient.append((self.client, self.conn))
all_client.append((self.conn,self.client))
self.client.start()
def SendAllClient(self,message):
global all_client
if message != "" and message != "\n":
for conn, client in all_client:
conn.send(message.encode("utf8"))
def Mute2(self, pseudo):
#print("recherche")
for conn, client in all_client:
ip,_ = client.address
#print(ip + " " + client.pseudo)
if client.pseudo[1:-1] == pseudo or ip == pseudo :
print(pseudo + " "+ ip + " " + client.pseudo)
client.mute = True
class ChatServ():
def __init__(self,host = "", port = 8558):
self.ListClient = []
self.host = host
self.port = port
#self.log = log
def Start(self,log):
self.log = log
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.bind((self.host,self.port))
while True:
self.socket.listen()
self.conn, self.adress = self.socket.accept()
self.log.insert(END,"Un client c'est connecté{}".format(self.adress))
self.client = ThreadChat(self.conn,self.log)
self.client.start()
def Stop(self):
self.log.insert(END,"Le server est déconnecter")
#self.client.close()
#self.socket.close()
"""
while True :
socket.listen(5)
client,address = socket.accept()
print("A client is connect")
conn = ThreadChat(client)
conn.start()
client.close()
socket.close()"""