-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
88 lines (76 loc) · 2.14 KB
/
server.py
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
import socket, os, sys
from threading import Thread
try:
HOST, PORT = sys.argv[1], int(sys.argv[2])
except:
print("Invalid arguments given:")
HOST = input("Enter host: ")
PORT = input("Port: ")
BUFSIZ = 1024
ADDR = (HOST, int(PORT))
RESET = "\033[0;0m"
BOLD = "\033[;1m"
RED = "\033[91m"
GREEN = "\033[92m"
SERVER = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
SERVER.bind(ADDR)
SERVER.listen(1)
#functon that converts a list into a string with separator: ';'
#['hi', 'my', 'name', 'is'] becomes hi;my;name;is
def convert_list(lista):
string = ""
for item in lista:
string += item+";"
string += "\n"
l = list(string)
del l[-2]
string = ""
for item in l:
string += item
return string
def from_client(client, adress):
while True:
try:
msg = client.recv(BUFSIZ).decode("utf-8")
if msg == "LEFT":
leftname = people[adress[1]]
sys.stdout.write(BOLD+RED+people[adress[1]]+" left"+RESET+"\n")
names.remove(people[adress[1]])
del people[adress[1]]
sks.remove(client)
for sock in sks:
if sock == client:
continue
sock.send(bytes("INFO: "+BOLD+RED+leftname+" left"+RESET+"\n", "utf-8"))
client.send(bytes("left", "utf-8"))
return
if msg == "LIST":
client.send(bytes("INFO: "+convert_list(names), "utf-8"))
continue
sys.stdout.write(people[adress[1]]+"> "+msg)
for sock in sks:
if sock == client:
continue
sock.send(bytes(people[adress[1]]+": "+msg, "utf-8"))
except Exception as e:
print("While loop exit because of {}".format(e)) #To remove
client.close()
break
people = {} #dictionary of PIDs and names
names = [] #list of names
sks = [] #list of sockets
print("#"+"-"*30+"#") #separator
print("Waiting for connections...")
while True:
c, a = SERVER.accept()
sks.append(c)
name = c.recv(BUFSIZ).decode("utf-8")
sys.stdout.write(BOLD+GREEN+a[0]+":"+str(a[1])+" connected"+RESET+"\n")
people[a[1]] = name
names.append(name)
for sock in sks:
if sock == c:
continue
sock.send(bytes("INFO: "+BOLD+GREEN+a[0]+":"+str(a[1])+" ("+"%s" % (name) +")"+" joined"+RESET+"\n", "utf-8"))
Thread(target=from_client, args=(c,a)).start()
#print(names, people)