-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
48 lines (41 loc) · 1.39 KB
/
server.py
File metadata and controls
48 lines (41 loc) · 1.39 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
#server.py
#December 2014
import socket
import datetime
#Server's connection Information
HOST = '127.0.0.1'
PORT = 50001
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((HOST, PORT))
clientList = {}
#Used to add timestamps when sending to clients
def addTime(fType, src, msg):
time = datetime.datetime.strftime(datetime.datetime.now(), "%H:%M:%S")
frame = fType + "!#" + src + "!#" + time + "!#" + msg
return frame
#Broadcast Messages to all clients, that aren't current user
def broadcast(fType, src, msg):
for i in clientList:
if clientList[i] != clientList[src]:
s.sendto(addTime(fType, src, msg).encode(), tuple(clientList[i]))
print ("Broadcasting: " + frame)
while 1:
data, addr = s.recvfrom(100)
frame = data.decode()
fType = frame.split('!#')[0]
src = frame.split("!#")[1]
msg = frame.split("!#")[2]
if fType == "MSG":
broadcast(fType, src, msg)
elif fType == "QUIT":
print "User: " + src + " has left chat."
broadcast(fType, src, "has left chat")
#Add Clients to list
elif fType == "INITIAL":
if src in clientList.keys():
print (">"+src + ": updated " + str(clientList[src]))
elif src not in clientList.keys():
clientList[src] = addr
print (">Client " + src + ": connected")
msg = "Connected"
broadcast(fType, src, msg)