-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
169 lines (116 loc) · 3.08 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
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
import socket
import pickle
import select
import sys
import threading
from _thread import start_new_thread
HEADER_LENGTH = 1024
LIST = "1"
SEND = "2"
RECEIVE = "3"
EXIT = "4"
OK = "OK"
OH = "NOT OK"
client_list = []
class Message:
def __init__(self, sender, message):
self.sender = sender
self.message = message
class Client:
def __init__(self,client_address,client_socket):
global client_list
self.socket=client_socket
self.address = client_address
self.messages = []
client_list.append(self)
print (f"{client_address} connected")
def set_name(self, name):
self.client_name = name
def kill_session(self):
global client_list
client_list.remove(self)
def send(self, data):
self.socket.send(data)
def recv(self):
return self.socket.recv(HEADER_LENGTH).decode()
def get_name(self):
return self.client_name
def insert_message(self, message):
self.messages.append(message)
def get_messages(self):
message_list = []
for message in self.messages:
message_str = message.sender + " : " + message.message
message_list.append(message_str)
return message_list
def run(client):
global client_list
client_name = client.socket.recv(HEADER_LENGTH).decode()
if not client_name:
print("Client closed connection")
client.kill_session()
return
print(client_name)
client.set_name(client_name)
while True:
command = client.recv()
print(command)
if not command:
client.kill_session()
print("client closed connection")
break
if command == LIST :
client_names = []
for client in client_list:
client_names.append(client.get_name())
data = pickle.dumps(client_names)
client.send(data)
elif command == SEND:
receiver = client.recv()
receiver_client = None
for cl in client_list:
if cl.get_name() == receiver:
receiver_client = cl
if receiver_client is None:
client.send(OH.encode())
continue
else:
client.send(OK.encode())
message = client.recv()
print(message)
client_message = Message(client.get_name(), message)
receiver_client.insert_message(client_message)
print("message inserted")
elif command == RECEIVE:
message_list = client.get_messages()
print(message_list)
data = pickle.dumps(message_list)
client.send(data)
elif command == EXIT:
print (str(client_name) + " disconnected.")
client.kill_session()
break
else :
print("None of the above :/ puta ")
#reserve a port on your computer, in our case :9000 . can be anyhting
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#server.TCPServer.allow_reuse_address = True
print ("Server Socket successfully created")
IP_address = 'localhost'
Port = '9000'
server.bind((IP_address, int(Port)))
#put socket into listening mode
server.listen(5)
print(f"Listening for connections on {IP_address}:{Port}...")
#forever loop until client wants to exit
while True:
try:
c, address = server.accept()
#print(client)
#client.settimeout(60)
start_new_thread(run, (Client(address, c),))
except Exception as e:
print("Exiting")
break
print("Closing server socket")
server.close()