-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverFinal.py
More file actions
66 lines (54 loc) · 1.47 KB
/
serverFinal.py
File metadata and controls
66 lines (54 loc) · 1.47 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
#!/usr/bin/python3
import socket, sys, threading, queue,time
'''
kody wiadomosci:
0 logowanie
'''
PORT = 5089
HOST = socket.gethostname()
newUserComes = queue.Queue()
newMesages = queue.Queue()
activeUsers = []
#If someone logs in, they have a login at the beginning of the message
#if he sends it he has a message for you
class Server:
users = []
def __init__(self):
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
address = HOST, PORT
self.server.bind(address)
except socket.error:
print("Binding failed")
return
self.server.listen()
def exit(self):
self.server.close()
def run(self):
print("Waiting for connection")
while True:
conn, addr = self.server.accept()
threading.Thread(target=self.run_thread, args=(conn, addr)).start()
def handleMessage(self):
while not newMesages.empty():
newMessage = newMesages.get()
if newMessage[0] == "0":
newMessage = newMessage.split(" ", 1)
print ("I sent information that has arrived",newMessage[1])
newUserComes.put(newMessage[1])
while not newUserComes.empty():
newUser = newUserComes.get()
activeUsers.append(newUser)
print("He came", newUser)
def run_thread(self, conn, addr):
while True:
message = conn.recv(2048)
if(message.decode() != ""):
newMesages.put(message.decode() )
if(not newMesages.empty()):
print("message came to me")
self.handleMessage()
time.sleep(1)
newServer = Server()
newServer.run()
Server.exit()