-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path055-full-chat-server-w-pics-and-pm.py
141 lines (120 loc) · 5.25 KB
/
055-full-chat-server-w-pics-and-pm.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
import socket
import threading
import os
import sys
# Set up the server
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('localhost', 8888)
server.bind(server_address)
server.listen(5)
print('Server is listening on', server_address)
# Set up the clients list and messages dictionary
clients = []
messages = {}
# Define the message handler function
def handle_message(client_socket, client_address):
# Receive the client's username
client_name = client_socket.recv(1024).decode('utf-8')
clients.append(client_socket)
print('New client connected:', client_name)
# Send a welcome message to the client
welcome_message = 'Welcome to the chat room, ' + client_name + '!\n'
client_socket.sendall(welcome_message.encode('utf-8'))
while True:
try:
# Receive the message from the client
message = client_socket.recv(1024).decode('utf-8')
if message.startswith('/pm'):
# Parse the private message
message_parts = message.split(' ')
recipient = message_parts[1]
message_text = ' '.join(message_parts[2:])
# Find the recipient client
recipient_socket = None
for c in clients:
if c != client_socket and c.getpeername()[1] == client_socket.getpeername()[1]:
recipient_socket = c
break
# Send the private message to the recipient
if recipient_socket is not None:
pm_message = client_name + ' (private): ' + message_text + '\n'
recipient_socket.sendall(pm_message.encode('utf-8'))
client_socket.sendall(pm_message.encode('utf-8'))
else:
error_message = 'Recipient not found.\n'
client_socket.sendall(error_message.encode('utf-8'))
elif message.startswith('/file'):
# Parse the file name and size
message_parts = message.split(' ')
file_name = message_parts[1]
file_size = int(message_parts[2])
# Receive the file from the client
received_size = 0
file_data = b''
while received_size < file_size:
data = client_socket.recv(1024)
received_size += len(data)
file_data += data
# Save the file to disk
with open(file_name, 'wb') as f:
f.write(file_data)
# Send the file to all clients
file_message = client_name + ' sent a file: ' + file_name + '\n'
for c in clients:
if c != client_socket:
c.sendall(file_message.encode('utf-8'))
elif message.startswith('/image'):
# Parse the image name and size
message_parts = message.split(' ')
image_name = message_parts[1]
image_size = int(message_parts[2])
# Receive the image from the client
received_size = 0
image_data = b''
while received_size < image_size:
data = client_socket.recv(1024)
received_size += len(data)
image_data += data
# Save the image to disk
with open(image_name, 'wb') as f:
f.write(image_data)
# Send the image to all clients
image_message = client_name + ' sent an image: ' + image_name + '\n'
for c in clients:
if c != client_socket:
c.sendall(image_message.encode('utf-8'))
else:
# Broadcast the message to all clients
broadcast_message = client_name + ': ' + message + '\n'
for c in clients:
if c != client_socket:
c.sendall(broadcast_message.encode('utf-8'))
# Save the message to the history
if client_name not in messages:
messages[client_name] = []
messages[client_name].append(broadcast_message)
except ConnectionResetError:
# Handle the case when the client disconnects
clients.remove(client_socket)
print('Client disconnected:', client_name)
break
def handle_history(client_socket):
# Receive the client's username
client_name = client_socket.recv(1024).decode('utf-8')
# Check if the client has any messages in the history
if client_name in messages:
history = ''.join(messages[client_name])
client_socket.sendall(history.encode('utf-8'))
else:
error_message = 'No history found.\n'
client_socket.sendall(error_message.encode('utf-8'))
while True:
# Accept new connections
client_socket, client_address = server.accept()
makefile
# Create a new thread to handle the client's messages
message_thread = threading.Thread(target=handle_message, args=(client_socket, client_address))
message_thread.start()
# Create a new thread to handle the client's history requests
history_thread = threading.Thread(target=handle_history, args=(client_socket,))
history_thread.start()