-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
270 lines (243 loc) · 10.6 KB
/
client.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
###############################################################################
# #
# ChatApp: Client Script #
# #
###############################################################################
import os
import sys
import json
import errno
import base64
import socket
import select
import datetime
import optparse
BYTES_READ = 4096
key = '1234567890123456' # key to encode the client password
# Key to encode the password of Client
def encode(key, clear):
enc = []
for i in range(len(clear)):
key_c = key[i % len(key)]
enc_c = chr((ord(clear[i]) + ord(key_c)) % 256)
enc.append(enc_c)
return base64.urlsafe_b64encode("".join(enc))
# Client helper function to print operations
def print_operation():
print("0 | List of options")
print("1 | Sign up")
print("2 | Log in")
print("3 | Log out")
print("4 | Who all are online")
print("5 | List of users logged in within last hour")
print("6 | Send private message")
print("7 | Send broadcast message")
print("8 | Block a user")
print("9 | Unblock a user")
# Client helper function for signup
def signup_client(sock, operation_selected):
username=raw_input('Enter username: ')
password = raw_input('Enter password: ')
dict_to_send = {
'operation': operation_selected,
'username': username,
'password': encode(key, password)
}
dict_to_send = json.dumps(dict_to_send)
sock.sendall(dict_to_send)
# Client helper function for login
def login_client(sock, operation_selected):
username=raw_input('Enter username: ')
password = raw_input('Enter password: ')
dict_to_send = {
'operation': operation_selected,
'username': username,
'password': encode(key, password)
}
dict_to_send = json.dumps(dict_to_send)
sock.sendall(dict_to_send)
# Client helper function for private messaging
def private_msg_client(sock, operation_selected):
username = raw_input('Enter username of the user: ')
message = raw_input('Enter your message: ')
dict_to_send = {
'operation': operation_selected,
'username': username,
'message': message,
'timestamp': str(datetime.datetime.now())
}
dict_to_send = json.dumps(dict_to_send)
sock.sendall(dict_to_send)
# Client helper function for broadcast
def broadcast_client(sock, operation_selected):
message = raw_input('Enter your message: ')
dict_to_send = {
'operation': operation_selected,
'message': message,
'timestamp': str(datetime.datetime.now())
}
dict_to_send = json.dumps(dict_to_send)
sock.sendall(dict_to_send)
# Client helper function to block user
def block_user_client(sock, operation_selected):
username = raw_input('Enter username to be blocked: ')
dict_to_send = {
'operation': operation_selected,
'username': username,
}
dict_to_send = json.dumps(dict_to_send)
sock.sendall(dict_to_send)
# Client helper function to unblock user
def unblock_user_client(sock, operation_selected):
username = raw_input('Enter username to be unblocked: ')
dict_to_send = {
'operation': operation_selected,
'username': username,
}
dict_to_send = json.dumps(dict_to_send)
sock.sendall(dict_to_send)
# Client helper function for logout
def logout_client(sock, operation_selected) :
dict_to_send = {
'operation': operation_selected,
}
dict_to_send = json.dumps(dict_to_send)
sock.sendall(dict_to_send)
# Client helper function to check users online
def users_online_client(sock, operation_selected):
dict_to_send = {
'operation': operation_selected,
}
dict_to_send = json.dumps(dict_to_send)
sock.sendall(dict_to_send)
# Client helper function to check last hour logged in users
def last_hour_login_users_client(sock, operation_selected):
dict_to_send = {
'operation': operation_selected,
}
dict_to_send = json.dumps(dict_to_send)
sock.sendall(dict_to_send)
# Local helper function to check if the input is an integer
def is_integer(operation):
try:
int(operation)
return True
except ValueError:
return False
def request(host, port, child_num, con_num):
# spawn child_num children processes
for cnum in range(child_num):
pid = os.fork()
if pid == 0: # child
for i in range(con_num):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
input = [sock, sys.stdin]
flag = True
print_operation()
data = ''
while flag:
print("Enter operation number")
# Block till there is something on stdin or socket
inputready, outputready, exceptready = select.select(input, [], [])
for x in inputready:
if x == sock:
data = data + sock.recv(BYTES_READ)
if data == '' :
flag = False
break
data = data.split("|")
for i in range(0,len(data)-1):
cur_data = json.loads(data[i])
if cur_data['status'] == 0 :
print('--------------------------------------------------------------------------------')
print('operation code: ' + str(cur_data['operation']))
print('operation status: failed')
print('message: ' + cur_data['message'])
print('--------------------------------------------------------------------------------')
elif cur_data['status'] == 1 :
print('--------------------------------------------------------------------------------')
print('operation code: ' + str(cur_data['operation']))
print('operation status: success')
print('message: ' + cur_data['message'])
print('--------------------------------------------------------------------------------')
elif cur_data['status'] == 2 :
print('--------------------------------------------------------------------------------')
print('MESSAGE RECEIVED')
print('operation code: ' + str(cur_data['operation']))
print('sender: ' + cur_data['sender'])
print('timestamp: ' + cur_data['timestamp'])
print('message: ' + cur_data['message'])
print('--------------------------------------------------------------------------------')
if data[-1] == '|' :
data = ''
else :
data = data[-1]
elif x == sys.stdin:
operation_selected = sys.stdin.readline()
if is_integer(operation_selected) :
operation_selected = int(operation_selected)
if operation_selected == 0:
print_operation()
elif operation_selected == 1:
signup_client(sock, operation_selected)
elif operation_selected == 2:
login_client(sock, operation_selected)
elif operation_selected == 3:
logout_client(sock, operation_selected)
sock.close()
flag = False
elif operation_selected == 4:
users_online_client(sock, operation_selected)
elif operation_selected == 5:
last_hour_login_users_client(sock, operation_selected)
elif operation_selected == 6:
private_msg_client(sock, operation_selected)
elif operation_selected == 7:
broadcast_client(sock, operation_selected)
elif operation_selected == 8:
block_user_client(sock, operation_selected)
elif operation_selected == 9:
unblock_user_client(sock, operation_selected)
else:
print("Please enter a valid operation number")
else:
print("Please enter a valid integer")
else :
flag = False
print 'Logged out!'
os._exit(0)
# wait for all children to finish
while True:
try:
pid, status = os.wait()
except OSError as e:
if e.errno == errno.ECHILD:
break
else:
raise
if pid == 0:
break
def main():
# Command line option parsing
parser = optparse.OptionParser()
parser.add_option(
'-i', '--host', dest='host', help='Hostname or IP address')
parser.add_option('-p', '--port', dest='port', type='int', help='Port')
parser.add_option(
'-c', '--child-num', dest='childnum', type='int', default=1,
help='Number of children to spawn. Default is 5'
)
parser.add_option(
'-t', '--con-num', dest='connum', type='int', default=1,
help='Number of connections to establish. Default is 1'
)
options, args = parser.parse_args()
if not (options.host and options.port):
parser.print_help()
sys.exit(1)
# Function to take response from stdin and socket
request(options.host, options.port,
options.childnum, options.connum)
if __name__ == '__main__':
main()