-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
177 lines (156 loc) · 7.63 KB
/
client.py
File metadata and controls
177 lines (156 loc) · 7.63 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
"""
Simple Chat Client
This script implements a command-line chat client that connects to a chat server.
It supports various commands to interact with the server and other users.
Commands:
%connect localhost 8080 - Connect to the chat server
%join - Join the server with a username
%post <message> - Send a message to all users
%users - Get the list of users connected to the server
%leave - Disconnect from the server
%exit - Exit the chat client
%message <message_id> - Retrieve a specific message by ID
%groups - List available groups
%groupjoin <group_names> - Join specified groups - e.g %groupjoin g1,g2,g3
%grouppost <group> <message> - Send a message to a group
%groupusers <group> - List users in a group
%groupleave <group> - Leave a group
%groupmessage <group> <message_id> - Retrieve a message from a group
Usage:
Run the script and enter commands as prompted.
"""
import socket # For networking operations
import threading # For handling concurrent threads
import datetime # For date and time operations
def receive_messages(sock):
# Function to continuously receive messages from the server
while True:
try:
# Receive data from the server
message = sock.recv(1024).decode('utf-8')
if not message:
break
print(message)
except:
# If an error occurs, notify and exit the loop
print("Connection closed.")
break
def main():
client_socket = None # Initialize the client socket
# Command loop to process user inputs
while True:
command = input("> ").strip() # Read command from user
# Handle %connect command to connect to the server
if command.startswith("%connect"):
# Parse the address and port from the command
_, address, port = command.split()
server_address = (address, int(port))
# Create a new socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
client_socket.connect(server_address)
print("Connected to the server.")
# Start a new thread to receive messages from the server
threading.Thread(target=receive_messages, args=(client_socket,), daemon=True).start()
except:
# If connection fails, notify user and reset client_socket
print("Connection failed.")
client_socket = None
# Handle %join command to join the server with a username
elif command.startswith("%join"):
if client_socket:
username = input("Enter your username: ")
# Send the username to the server
client_socket.send(username.encode('utf-8'))
client_socket.send("%users".encode('utf-8')) # Request the list of users
else:
print("You need to connect to the server first using %connect command.")
# Handle %post command to send a message to the server
elif command.startswith("%post"):
if client_socket:
# Extract the message content after the command
_, content = command.split(maxsplit=1)
# Get the current date and time
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
message = f"Date: {timestamp}, Content: {content}"
client_socket.send(message.encode('utf-8'))
else:
print("You need to connect to the server first using %connect command.")
# Handle %users command to request the list of users from the server
elif command.startswith("%users"):
if client_socket:
client_socket.send("%users".encode('utf-8'))
else:
print("You need to connect to the server first using %connect command.")
# Handle %leave command to disconnect from the server
elif command.startswith("%leave"):
if client_socket:
# Notify the server about leaving
client_socket.send("%leave".encode('utf-8'))
client_socket.close()
client_socket = None
print("Disconnected from the server.")
else:
print("You need to connect to the server first using %connect command.")
# Handle %exit command to exit the client application
elif command.startswith("%exit"):
if client_socket:
client_socket.send("%leave".encode('utf-8'))
client_socket.close()
print("Exiting the client.")
break # Exit the command loop and terminate the program
# Handle %message command to retrieve a specific message by ID
elif command.startswith("%message"):
if client_socket:
_, message_id = command.split(maxsplit=1)
client_socket.send(f"%message {message_id}".encode('utf-8'))
else:
print("You need to connect to the server first using %connect command.")
# Handle %groups command to list available groups on the server
elif command.startswith("%groups"):
if client_socket:
client_socket.send("%groups".encode('utf-8'))
else:
print("You need to connect to the server first using %connect command.")
# Handle %groupjoin command to join specified groups
elif command.startswith("%groupjoin"):
if client_socket:
_, groups = command.split(maxsplit=1)
client_socket.send(f"%groupjoin {groups}".encode('utf-8'))
else:
print("You need to connect to the server first using %connect command.")
# Handle %grouppost command to send a message to a group
elif command.startswith("%grouppost"):
if client_socket:
_, group, content = command.split(maxsplit=2)
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
message = f"%grouppost {group} Date: {timestamp}, Content: {content}"
client_socket.send(message.encode('utf-8'))
else:
print("You need to connect to the server first using %connect command.")
# Handle %groupusers command to list users in a specific group
elif command.startswith("%groupusers"):
if client_socket:
_, group = command.split(maxsplit=1)
client_socket.send(f"%groupusers {group}".encode('utf-8'))
else:
print("You need to connect to the server first using %connect command.")
# Handle %groupleave command to leave a specific group
elif command.startswith("%groupleave"):
if client_socket:
_, group = command.split(maxsplit=1)
client_socket.send(f"%groupleave {group}".encode('utf-8'))
else:
print("You need to connect to the server first using %connect command.")
# Handle %groupmessage command to retrieve a specific message from a group
elif command.startswith("%groupmessage"):
if client_socket:
_, group, message_id = command.split(maxsplit=2)
client_socket.send(f"%groupmessage {group} {message_id}".encode('utf-8'))
else:
print("You need to connect to the server first using %connect command.")
# If command is not recognized, notify the user
else:
print("Unknown command. Please use one of the specified commands.")
if __name__ == "__main__":
main()