-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatclient3.py
248 lines (223 loc) · 6.92 KB
/
chatclient3.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
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
# #!/usr/bin/python
# # USAGE: echo_client_sockets.py <HOST> <PORT> <MESSAGE>
# #
# # EXAMPLE: echo_client_sockets.py localhost 8000 Hello
# import socket
# import sys
# import json
#
# length_header = 100
#
# if len(sys.argv) < 4:
# print("USAGE: echo_client_sockets.py <HOST> <PORT> <MESSAGE>")
# sys.exit(0)
#
# s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#
# host = sys.argv[1]
# port = int(sys.argv[2])
# s.connect((host,port))
# msg = json.dumps({"message":sys.argv[3]})
# pack = f"{len(msg):<{length_header}}" + msg
# s.send(pack.encode('utf-8')) #default encoding of str.encode is utf-8
#
# i = 0
# data = s.recv(10000000)
# print(data.decode('utf-8'))
# print(f'received {len(data)} bytes')
# s.close()
#!/usr/bin/python
# USAGE: echo_client_sockets.py <HOST> <PORT> <MESSAGE>
#
# EXAMPLE: echo_client_sockets.py localhost 8000 Hello
import socket
import sys
import json
import select
from Message import MessageType
from Message import Message
length_header = 100
host = 'localhost' #sys.argv[1]
port = 50001 #int(sys.argv[2])
inputSocket = []
outputSocket = []
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
username = ""
# if len(sys.argv) < 4:
# print("USAGE: echo_client_sockets.py <HOST> <PORT> <MESSAGE>")
# sys.exit(0)
#Anonymous chat room
def client_console(s):
while True:
user_choices = {
1: "Create a room",
2: "List all rooms",
3: "Join a room",
4: "Leave a room",
5: "List all members of a room",
6: "Open and participate in rooms(send message)",
7: "Log out",
}
for eachChoice in user_choices:
print(eachChoice, user_choices[eachChoice])
x = input("Enter a number from the above: ")
if not x.isnumeric():
print("Plz enter only the number in range")
continue
remainActive = choice_def[int(x)](int(x))
if remainActive == False:
break
if x.isnumeric() and int(x) == 7:
break
x1 = input("Show menu: Y / N :")
x1 = x1.rstrip()
if x1 == "N" or x1 == "n" :
choice_def[7]()
break
def create_a_room(choice):
try:
chatRoomId = input("Enter chat room id")
chatRoomId = chatRoomId.strip()
msgObj = Message(1, username, chatRoomId, "")
encode_send_message(msgObj)
except:
return False
def send_receive_messages(choice):
if choice == 6:
print("enter 'Q' to quit chat and go back to main menu. ")
running = True
while running:
inputready, outputready, exceptready = select.select(inputSocket, outputSocket, inputSocket, 0.1)
for s in inputready:
if s == sys.stdin:
# any input closes the serves. Does not work on Windows!
engMessage = sys.stdin.readline()
engMessage = engMessage.rstrip()
if engMessage == 'Q':
running = False
break
chatRoomId = input("Input Chat Room ID: ")
msgObj = Message(6, username, chatRoomId.strip(), engMessage.strip())
encode_send_message(msgObj)
#Create message
else: #It's about to receive a message
# handle all other sockets
inputMsg = getMessage()
if not inputMsg:
print("Didn't receive any message, quitting")
log_out(7)
running = False
return False
else:
msgObj = process_incomming_message(inputMsg)
# if msgObj.msg_type == 2 or msgObj.msg_type == 5:
# running = False
# break
if msgObj.msg_type != 6:
running = False
break
return
def encode_send_message(messageObj):
# x = input("Enter the message: ")
msg = json.dumps(messageObj.__dict__) #sys.argv[3]
pack = f"{len(msg):<{length_header}}" + msg
pack = pack.encode('utf-8')
while len(pack):
transmittedBytes = s.send(pack)
pack = pack[transmittedBytes:]
def getMessage():
#"client" must be a socket object connected to a client
length = ''
templen = length_header
while len(length) < length_header:
buffer = ''
buffer = s.recv(templen).decode()
if buffer == '':
return False
length += buffer
templen -= len(buffer)
intlength = int(length.strip())
msg = ''
templen = intlength
while len(msg) < intlength:
buffer = ''
buffer = s.recv(templen).decode()
if buffer == '':
return False
msg += buffer
templen -= len(buffer)
return msg
def process_incomming_message(msg):
msgDict = json.loads(msg)
#Print the msg in the msg.
msgObj = Message(msgDict["msg_type"], msgDict["sender"], msgDict["destination"], msgDict["message"])
if msgObj.msg_type == 6:
print("From: ",msgObj.sender, "ChatRoom: ", msgObj.destination, "Msg: ", msgObj.message)
elif msgObj.msg_type == 2:
print("All rooms: ", msgObj.message)
elif msgObj.msg_type == 5:
print("All members: ", msgObj.message)
return msgObj
def list_all_rooms(choice):
try:
msgObj = Message(2, username, 0, "")
encode_send_message(msgObj)
send_receive_messages(choice)
except:
return False
def join_room(choice):
try:
roomId = input("Room name to join: ")
msgObj = Message(3, username, roomId.strip(), "")
encode_send_message(msgObj)
except:
return False
def leave_room(choice):
try:
roomId = input("Room name to leave: ")
msgObj = Message(4, username, roomId.strip(), "")
encode_send_message(msgObj)
except:
return False
def list_all_members_room(choice):
try:
roomId = input("Room name for members list: ")
msgObj = Message(5, username, roomId.strip(), "")
encode_send_message(msgObj)
send_receive_messages(choice)
except:
return False
def log_out(choice):
try:
s.close()
except:
return False
# i = 0
# data = s.recv(10000000)
# print(data.decode('utf-8'))
# print(f'received {len(data)} bytes')
# s.close()
choice_def = {
1: create_a_room,
2: list_all_rooms,
3: join_room,
4: leave_room,
5: list_all_members_room,
6: send_receive_messages,
7: log_out,
# 8: query8,
# 9: query9,
# 10: query10,
}
def init_username():
global username
username = input("Input new username")
msgObj = Message(0, username, 0, "")
#str(MessageType.InitUsername)
encode_send_message(msgObj)
if __name__ == '__main__':
inputSocket.append(s)
inputSocket.append(sys.stdin)
init_username()
client_console(s)