|
| 1 | + |
| 2 | +import socket |
| 3 | +import pickle |
| 4 | +import select |
| 5 | +import sys |
| 6 | +import threading |
| 7 | +from _thread import start_new_thread |
| 8 | + |
| 9 | + |
| 10 | +HEADER_LENGTH = 1024 |
| 11 | + |
| 12 | +LIST = "1" |
| 13 | +SEND = "2" |
| 14 | +RECEIVE = "3" |
| 15 | +EXIT = "4" |
| 16 | +OK = "OK" |
| 17 | +OH = "NOT OK" |
| 18 | +client_list = [] |
| 19 | + |
| 20 | +class Message: |
| 21 | + |
| 22 | + def __init__(self, sender, message): |
| 23 | + self.sender = sender |
| 24 | + self.message = message |
| 25 | + |
| 26 | +class Client: |
| 27 | + |
| 28 | + def __init__(self,client_address,client_socket): |
| 29 | + global client_list |
| 30 | + self.socket=client_socket |
| 31 | + self.address = client_address |
| 32 | + self.messages = [] |
| 33 | + client_list.append(self) |
| 34 | + print (f"{client_address} connected") |
| 35 | + |
| 36 | + def set_name(self, name): |
| 37 | + self.client_name = name |
| 38 | + |
| 39 | + def kill_session(self): |
| 40 | + global client_list |
| 41 | + client_list.remove(self) |
| 42 | + |
| 43 | + def send(self, data): |
| 44 | + self.socket.send(data) |
| 45 | + |
| 46 | + def recv(self): |
| 47 | + return self.socket.recv(HEADER_LENGTH).decode() |
| 48 | + |
| 49 | + def get_name(self): |
| 50 | + return self.client_name |
| 51 | + |
| 52 | + def insert_message(self, message): |
| 53 | + self.messages.append(message) |
| 54 | + |
| 55 | + def get_messages(self): |
| 56 | + message_list = [] |
| 57 | + for message in self.messages: |
| 58 | + message_str = message.sender + " : " + message.message |
| 59 | + message_list.append(message_str) |
| 60 | + return message_list |
| 61 | + |
| 62 | + |
| 63 | +def run(client): |
| 64 | + |
| 65 | + global client_list |
| 66 | + |
| 67 | + client_name = client.socket.recv(HEADER_LENGTH).decode() |
| 68 | + if not client_name: |
| 69 | + print("Client closed connection") |
| 70 | + client.kill_session() |
| 71 | + return |
| 72 | + |
| 73 | + print(client_name) |
| 74 | + client.set_name(client_name) |
| 75 | + |
| 76 | + |
| 77 | + while True: |
| 78 | + |
| 79 | + command = client.recv() |
| 80 | + print(command) |
| 81 | + if not command: |
| 82 | + client.kill_session() |
| 83 | + print("client closed connection") |
| 84 | + break |
| 85 | + |
| 86 | + if command == LIST : |
| 87 | + |
| 88 | + client_names = [] |
| 89 | + for client in client_list: |
| 90 | + client_names.append(client.get_name()) |
| 91 | + data = pickle.dumps(client_names) |
| 92 | + client.send(data) |
| 93 | + |
| 94 | + elif command == SEND: |
| 95 | + |
| 96 | + receiver = client.recv() |
| 97 | + receiver_client = None |
| 98 | + |
| 99 | + for cl in client_list: |
| 100 | + if cl.get_name() == receiver: |
| 101 | + receiver_client = cl |
| 102 | + if receiver_client is None: |
| 103 | + client.send(OH.encode()) |
| 104 | + continue |
| 105 | + else: |
| 106 | + client.send(OK.encode()) |
| 107 | + |
| 108 | + message = client.recv() |
| 109 | + print(message) |
| 110 | + client_message = Message(client.get_name(), message) |
| 111 | + receiver_client.insert_message(client_message) |
| 112 | + |
| 113 | + print("message inserted") |
| 114 | + |
| 115 | + elif command == RECEIVE: |
| 116 | + message_list = client.get_messages() |
| 117 | + print(message_list) |
| 118 | + data = pickle.dumps(message_list) |
| 119 | + client.send(data) |
| 120 | + |
| 121 | + |
| 122 | + elif command == EXIT: |
| 123 | + print (str(client_name) + " disconnected.") |
| 124 | + client.kill_session() |
| 125 | + break |
| 126 | + else : |
| 127 | + print("None of the above :/ puta ") |
| 128 | + |
| 129 | + |
| 130 | +#reserve a port on your computer, in our case :9000 . can be anyhting |
| 131 | + |
| 132 | +server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 133 | +#server.TCPServer.allow_reuse_address = True |
| 134 | +print ("Server Socket successfully created") |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | +IP_address = 'localhost' |
| 139 | +Port = '9000' |
| 140 | + |
| 141 | + |
| 142 | +server.bind((IP_address, int(Port))) |
| 143 | + |
| 144 | +#put socket into listening mode |
| 145 | + |
| 146 | +server.listen(5) |
| 147 | + |
| 148 | +print(f"Listening for connections on {IP_address}:{Port}...") |
| 149 | + |
| 150 | +#forever loop until client wants to exit |
| 151 | + |
| 152 | +while True: |
| 153 | + try: |
| 154 | + c, address = server.accept() |
| 155 | + #print(client) |
| 156 | + #client.settimeout(60) |
| 157 | + |
| 158 | + start_new_thread(run, (Client(address, c),)) |
| 159 | + |
| 160 | + |
| 161 | + except Exception as e: |
| 162 | + print("Exiting") |
| 163 | + break |
| 164 | + |
| 165 | +print("Closing server socket") |
| 166 | +server.close() |
| 167 | + |
| 168 | + |
| 169 | + |
0 commit comments