-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1763650800.py
More file actions
270 lines (235 loc) · 9.42 KB
/
Copy path1763650800.py
File metadata and controls
270 lines (235 loc) · 9.42 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import asyncio
DEFAULT_HOST = "127.0.0.1"
DEFAULT_PORT = 49152
class ChatServerDatagramProtocol(asyncio.DatagramProtocol):
def __init__(self, server_manager):
self.server_manager = server_manager
self.transport = None
def connection_made(self, transport):
self.transport = transport
self.server_manager.set_transport(transport)
print("UDP Server transport ready.")
def datagram_received(self, data, addr):
self.server_manager.handle_datagram(data, addr)
def error_received(self, exc):
print(f"UDP server error received: {exc}")
class ChatServer:
def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT):
self.clients = {}
self.transport = None
self.host = host
self.port = port
def set_transport(self, transport):
self.transport = transport
def register_client(self, addr, username):
if addr in self.clients:
return False
self.clients[addr] = username
return True
def unregister_client(self, addr):
if addr in self.clients:
username = self.clients.pop(addr)
return username
return None
def broadcast(self, message):
if not self.transport:
return
broadcast_data = (message + "\n").encode("utf-8")
for addr in list(self.clients.keys()):
try:
self.transport.sendto(broadcast_data, addr)
except Exception as e:
print(f"Warning: Failed to send data to client {addr}. {e}")
def handle_datagram(self, data, addr):
message = data.decode("utf-8").strip()
if not message:
return
conn_info = f"{addr[0]}:{addr[1]}"
username = self.clients.get(addr)
if username is None:
if message.startswith("__USERNAME__:") and len(message) > len("__USERNAME__:") + 1:
new_username = message[len("__USERNAME__:") :].strip()
if 1 <= len(new_username) <= 15:
if self.register_client(addr, new_username):
print(f"Client registered: {new_username} ({conn_info})")
welcome_message = f"*** {new_username} joined the chat. ***"
self.broadcast(welcome_message)
else:
print(f"Warning: Invalid username received from {conn_info}. Ignoring.")
return
if message.lower() in ("/quit", "/exit"):
username_left = self.unregister_client(addr)
if username_left:
print(f"Client quit: {username_left} ({conn_info})")
self.broadcast(f"*** {username_left} left the chat. ***")
return
display_name = username
display_message = f"[{display_name} {conn_info}] {message}"
print(display_message)
self.broadcast(display_message)
async def run(self):
loop = asyncio.get_running_loop()
host, port = self.host, self.port
print(f"Starting Chat Server UDP on {host}:{port}")
def protocol_factory():
return ChatServerDatagramProtocol(self)
self.transport, protocol = await loop.create_datagram_endpoint(protocol_factory, local_addr=(host, port))
addr = self.transport.get_extra_info("sockname")
print(f"Chat Server serving on {addr[0]}:{addr[1]}")
try:
await asyncio.Future()
finally:
if self.transport:
self.transport.close()
class ChatClientDatagramProtocol(asyncio.DatagramProtocol):
def __init__(self, username, server_addr, on_con_lost):
self.username = username
self.transport = None
self.server_addr = server_addr
self.on_con_lost = on_con_lost
def connection_made(self, transport):
self.transport = transport
self.transport.sendto(f"__USERNAME__:{self.username}\n".encode("utf-8"), self.server_addr)
print("--- Chat Client connected (UDP). Type /quit or /exit to leave. ---")
def datagram_received(self, data, addr):
messages = data.decode("utf-8").splitlines()
for message in messages:
if message:
print(f"\r{' ' * 80}\r{message}\n> ", end="", flush=True)
def error_received(self, exc):
print(f"Error received: {exc}")
def connection_lost(self, exc):
print("Client transport closed.")
if self.on_con_lost and not self.on_con_lost.done():
self.on_con_lost.set_result(True)
def send_message(self, message):
if self.transport:
self.transport.sendto((message + "\n").encode("utf-8"), self.server_addr)
class ChatClient:
def __init__(self, host, port, username):
self.host = host
self.port = port
self.username = username
self.protocol = None
self.transport = None
self.on_con_lost = None
async def _input_handler(self):
loop = asyncio.get_running_loop()
def get_input():
try:
return input("> ")
except EOFError:
return None
while True:
try:
message = await loop.run_in_executor(None, get_input)
if message is None:
break
message = message.strip()
if message.lower() in ("/quit", "/exit"):
print("Exiting chat client.")
break
if message and self.protocol:
self.protocol.send_message(message)
except KeyboardInterrupt:
print("\nInterrupted.")
break
async def run(self):
loop = asyncio.get_running_loop()
self.on_con_lost = loop.create_future()
server_addr = (self.host, self.port)
def client_protocol_factory():
return ChatClientDatagramProtocol(self.username, server_addr, self.on_con_lost)
try:
self.transport, self.protocol = await loop.create_datagram_endpoint(
client_protocol_factory, remote_addr=server_addr
)
except OSError as e:
print(f"Error creating UDP socket: {e}")
return
input_task = loop.create_task(self._input_handler())
done, pending = await asyncio.wait(
[input_task, self.on_con_lost],
return_when=asyncio.FIRST_COMPLETED,
)
if self.transport:
self.transport.close()
for task in pending:
if not task.done():
task.cancel()
if pending:
await asyncio.gather(*pending, return_exceptions=True)
async def main():
print("Socket Chat")
mode = ""
while mode not in ("c", "s"):
try:
mode = input("Select mode (C=Client / S=Server): ").strip().lower()
except (EOFError, KeyboardInterrupt):
print("Configuration interrupted. Exiting.")
return
is_server = mode == "s"
username = ""
if not is_server:
while not (1 <= len(username) <= 15):
try:
username = input("Enter your name (1-15 characters): ").strip()
if not (1 <= len(username) <= 15):
print("Error: Name must be between 1 and 15 characters long.")
except (EOFError, KeyboardInterrupt):
print("Configuration interrupted. Exiting.")
return
host = DEFAULT_HOST
port = DEFAULT_PORT
while True:
try:
prompt_role = "server listen" if is_server else "server target"
default_addr = f"{DEFAULT_HOST}:{DEFAULT_PORT}"
prompt = f"Enter {prompt_role} address (e.g., 127.0.0.1:{DEFAULT_PORT}) [{default_addr}]: "
user_input = input(prompt).strip()
except (EOFError, KeyboardInterrupt):
print("Configuration interrupted. Exiting.")
return
current_host = DEFAULT_HOST
current_port = DEFAULT_PORT
if user_input:
parts = user_input.rsplit(":", 1)
if len(parts) == 2:
raw_host, raw_port = parts
if raw_host.startswith("[") and raw_host.endswith("]"):
current_host = raw_host.strip("[]")
else:
current_host = raw_host
if not current_host:
current_host = DEFAULT_HOST
try:
current_port = int(raw_port)
if not (DEFAULT_PORT <= current_port <= 65535):
print(f"Error: Port number is out of the valid range ({DEFAULT_PORT}-65535).")
continue
except ValueError:
print("Error: Invalid port number.")
continue
elif len(parts) == 1:
current_host = parts[0]
if not is_server and current_host == "0.0.0.0":
print("Error: 0.0.0.0 is not allowed as a client target IP.")
continue
host = current_host
port = current_port
break
if is_server:
print(f"Starting server on {host}:{port}")
server = ChatServer(host, port)
await server.run()
else:
print(f"Connecting to server at {host}:{port} as user {username}")
client = ChatClient(host, port, username)
await client.run()
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
pass
except Exception:
pass