-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
45 lines (37 loc) · 1.33 KB
/
Copy pathserver.py
File metadata and controls
45 lines (37 loc) · 1.33 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
import socket
import threading
global_User = []
global_connection = []
def serverListen(connection, address, Name):
while(True):
try:
data = connection.recv(1024).decode('utf-8')
except ConnectionResetError:
print(Name + " has left!!!")
global_User.remove(address)
global_connection.remove(connection)
connection.close()
break
for each in global_connection:
if each != connection:
each.send((Name+"-->"+data).encode('utf-8'))
def serverSend(conn):
for each in conn:
each.send(('Available User: '+str(len(global_User)-1)).encode('utf-8'))
if __name__ == '__main__':
objectSocket = socket.socket()
print("Server Started !!!!")
print("Waiting for clients!!!")
objectSocket.bind(('127.0.0.1', 12000))
objectSocket.listen(3)
while True:
try:
connection, address = objectSocket.accept()
global_User.append(address)
global_connection.append(connection)
Name = connection.recv(20).decode('utf-8')
serverSend(global_connection)
print(Name + ' came in chat room!!!')
threading.Thread(target=serverListen, args=(connection,address,Name,)).start()
except ConnectionResetError as e:
pass