-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIClient.py
554 lines (451 loc) · 14.7 KB
/
UIClient.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
from Tkinter import *
from socket import *
from ftplib import FTP
import tkFileDialog
from tkMessageBox import *
import thread
import sys
import pyaudio
import wave
import threading
import time
import os
from Crypto.Hash import MD5
from Crypto.PublicKey import RSA
from Crypto.Util import randpool
import pickle
#generate the RSA key
num = randpool.RandomPool()
RSAKey = RSA.generate(2048, num.get_bytes)
RSAPubKey = RSAKey.publickey()
########### FILE UPLOAD ###########3
########### MODIFY ########################
USER = 'amitkmr'
PASS = '121258'
########### MODIFY IF YOU WANT ############
SERVER = 'webhome.cc.iitk.ac.in'
PORT = 21
BINARY_STORE = True # if False then line store (not valid for binary files (videos, music, photos...))
SERVER_ADDRESS = ('','')
RECORD_SECONDS = 1
CALL = 0
connection_done = False # to check if connection is established or not
call_flag = True
def connect_ftp():
#Connect to the server
ftp = FTP()
ftp.connect(SERVER, PORT)
ftp.login(USER, PASS)
return ftp
def upload_file(ftp_connetion, upload_file_path):
#Open the file
try:
upload_file = open(upload_file_path, 'r')
#get the name
path_split = upload_file_path.split('/')
final_file_name = path_split[len(path_split)-1]
#transfer the file
print('Uploading ' + final_file_name + '...')
if BINARY_STORE:
ftp_connetion.storbinary('STOR '+ final_file_name, upload_file)
else:
ftp_connetion.storlines('STOR '+ final_file_name, upload_file)
print('Upload finished.')
return
except IOError:
print ("No such file or directory... passing to next file")
ftp_conn = connect_ftp()
def secure_sendFile():
if connection_done :
Tk().withdraw()
filePath =tkFileDialog.askopenfilename()
filename = filePath.split('/')
file_extension = filename[-1].split('.')
data = open(filePath, "r").read()
enc_data = publickey.encrypt(data, 32)
message = ""
if len(file_extension) == 1:
message = "9^"
else:
message = "9^"+file_extension[1]
clientSocket.send(message)
clientSocket.sendall(pickle.dumps(enc_data))
# secure_recvFile(str(enc_data),file_extension[1])
chatBox("File sent "+filename[-1])
else :
chatBox("Connection not yet established")
def secure_recvFile(rcstring,file_extension):
Tk().withdraw()
if askyesno('Download File', 'Do you really want to Download file?'):
save_path = ''
if file_extension == '':
save_path = file_save()
else:
save_path = file_save()+'.'+file_extension
encrypt_msg = pickle.loads(rcstring)
file = open(save_path, "w")
file.write(RSAKey.decrypt(encrypt_msg))
file.close()
showwarning('File downloaded', 'you have received a file')
else:
showinfo('File Download Cancel', 'Download quit')
def file_save():
f = tkFileDialog.asksaveasfilename()
if f is None: # asksaveasfile return `None` if dialog closed with "cancel".
return
return f
def retrieveFile(list):
Tk().withdraw()
file_extension = list.split('.')
if askyesno('Download File', 'Yes you really want to Download file'):
ftp = FTP('webhome.cc.iitk.ac.in')
ftp.login(user='amitkmr', passwd = '121258')
filename =list
save_path = ''
if len(file_extension) == 1:
save_path = file_save()
else:
save_path = file_save()+'.'+file_extension[1]
localfile = open( save_path, 'wb')
ftp.retrbinary('RETR ' + filename, localfile.write, 1024)
ftp.quit()
showwarning('File downloaded', 'you have received a file')
else:
showinfo('File Download Cancel', 'Download quit')
def retrieveAudio(filename):
ftp = FTP('webhome.cc.iitk.ac.in')
ftp.login(user='amitkmr', passwd = '121258')
localfile = open( "sandip.wav", 'wb')
ftp.retrbinary('RETR ' + filename, localfile.write, 1024)
ftp.quit()
if askyesno('play Audio','Do you want to play?'):
play_audio("sandip.wav")
else:
chatBox("Audio saved")
def sendFile():
if connection_done :
Tk().withdraw()
filePath =tkFileDialog.askopenfilename()
upload_file(ftp_conn,filePath)
filename = filePath.split('/')
message = "1^"+filename[-1]
clientSocket.send(message)
chatBox("File sent "+filename[-1])
else :
chatBox("Connection not yet established")
def sendMessage(event):
if connection_done :
message = entry1.get()
# t = time.time()
# print("%.6f" % t)
clientSocket.send(message)
chatBox("You: "+message)
else :
chatBox("Connection not yet established")
def receivedMessage():
while 1:
global SERVER_ADDRESS
if call_flag and CALL == 0:
message = clientSocket.recv(1024)
splitMessage = message.split('^')
if splitMessage[0]=="1":
fileName = splitMessage[1]
chatBox("Received file")
retrieveFile(fileName)
elif splitMessage[0]=="2":
chatBox(splitMessage[1])
global connection_done
connection_done = True
entry2.config(state=DISABLED)
elif splitMessage[0]=="3":
if askyesno('Incoming Call', 'Do you want to receive?'):
message = "5^accept call"
clientSocket.send(message)
call1()
else:
message = "6^end call"
clientSocket.send(message)
elif splitMessage[0] == "6":
chatBox("Call Rejected")
elif splitMessage[0] == "5":
call1()
elif splitMessage[0]=="7":
showinfo('Chat Disconnected', 'Chat Disconnected')
connection_done = False
window.destroy()
clientSocket.close()
elif splitMessage[0]=="8":
fileName = splitMessage[1]
retrieveAudio(fileName)
elif splitMessage[0]=="9":
rcstring = ''
buf = clientSocket.recv(1024)
rcstring += buf
chatBox("Recieved file")
secure_recvFile(rcstring,splitMessage[1])
else:
if message != '':
chatBox("He: " + message)
def chatBox(text):
listbox.insert(END, text)
listbox.yview(END)
listbox.pack(side=LEFT, fill=BOTH)
scrollbar.config(command=listbox.yview)
entry1.delete(0, END)
return
def setIP (event):
ipaddress = entry2.get()
global serverName
global connection_done
global SERVER_ADDRESS
global publickey
serverName = ipaddress
SERVER_ADDRESS = (serverName,serverPort)
try:
clientSocket.connect(SERVER_ADDRESS)
rcstring = clientSocket.recv(2048)
publickey = pickle.loads(rcstring)
clientSocket.send(pickle.dumps(RSAPubKey))
time.sleep(0.2)
message = "2^Someone has connected to you"
clientSocket.send(message)
connection_done = True
chatBox("Connected to " + ipaddress)
entry2.config(state=DISABLED)
try:
thread.start_new_thread(receivedMessage,())
except:
print "Error: unable to start thread"
except Exception, e :
chatBox("Unknown service")
entry2.delete(0, END)
def start_record ():
if connection_done:
global RECORD_SECONDS
RECORD_SECONDS=1
button2.config(state="normal")
button1.config(state=DISABLED)
try:
thread.start_new_thread(record,())
except:
print "Error: unable to start thread"
else :
chatBox("Connection not yet established")
def record():
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
WAVE_OUTPUT_FILENAME = "output.wav"
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
chatBox("* recording")
frames = []
while RECORD_SECONDS==1:
data = stream.read(CHUNK)
frames.append(data)
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
def stop_record():
global RECORD_SECONDS
RECORD_SECONDS = 0
time.sleep(1)
chatBox("* done recording")
button1.config(state="normal")
button2.config(state=DISABLED)
send_audio('output.wav')
def send_audio(audio_file):
upload_file(ftp_conn,audio_file)
# time.sleep(1)
message = "8^"+audio_file
clientSocket.send(message)
os.remove(audio_file)
def play_audio(filename):
chunk = 1024
# open the file for reading.
wf = wave.open(filename, 'rb')
# create an audio object
p = pyaudio.PyAudio()
# open stream based on the wave object which has been input.
stream = p.open(format =
p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = wf.getframerate(),
output = True)
# read data (based on the chunk size)
data = wf.readframes(chunk)
# play stream (looping from beginning of file to the end)
while data != '':
# writing to the stream is what *actually* plays the sound.
stream.write(data)
data = wf.readframes(chunk)
# cleanup stuff.
stream.close()
p.terminate()
def call():
if connection_done:
message = "3^incoming call"
clientSocket.send(message)
else :
chatBox("Connection not yet established")
def call1():
entry1.config(state=DISABLED)
global call_flag
call_flag = False
global CALL
CALL=1
button4.config(state="normal")
button3.config(state=DISABLED)
threading.Thread(target=call_send).start()
threading.Thread(target=call_recv).start()
def end_call1():
global CALL
CALL = 0
chatBox("* call ended")
global call_flag
call_flag = True
entry1.config(state="normal")
button3.config(state="normal")
button4.config(state=DISABLED)
def end_call():
global CALL
CALL = 0
time.sleep(2)
message = "4^incoming call"
clientSocket.send(message)
chatBox("* call ended")
global call_flag
call_flag = True
entry1.config(state="normal")
button3.config(state="normal")
button4.config(state=DISABLED)
def call_send():
# s.connect((HOST, PORT))
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 20
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
chatBox("* call start")
frames = []
while CALL==1:
data = stream.read(CHUNK)
frames.append(data)
clientSocket.sendall(data)
if CALL == 0:
return
stream.stop_stream()
stream.close()
p.terminate()
def call_recv():
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 20
WAVE_OUTPUT_FILENAME = "server_output.wav"
WIDTH = 2
frames = []
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(WIDTH),
channels=CHANNELS,
rate=RATE,
output=True,
frames_per_buffer=CHUNK)
data = clientSocket.recv(1024)
while CALL==1:
stream.write(data)
data = clientSocket.recv(1024)
splitMessage = data.split('^')
if splitMessage[0] == "4":
end_call1()
return
frames.append(data)
if CALL == 0:
return
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
stream.stop_stream()
stream.close()
p.terminate()
def new_chat():
os.system("python UIClient.py &")
def on_closing():
if askokcancel("Quit", "Do you want to quit?"):
global connection_done
if connection_done:
message="7^chat end"
clientSocket.send(message)
connection_done = False
window.destroy()
clientSocket.close()
else:
connection_done = False
window.destroy()
clientSocket.close()
########## Socket Connection ###############
serverName = ''
serverPort = 12000
clientSocket = socket(AF_INET,SOCK_STREAM)
#####################
#############
window = Tk()
window.title("chatWindow")
window.geometry("400x500")
frame = Frame(window)
frame.pack(side = TOP, fill = X)
frame1 = Frame(window)
frame1.pack(side = BOTTOM, fill = X)
button6 = Button(window, text ="Secure Send File",command= secure_sendFile)
button6.pack(side=BOTTOM)
button = Button(window, text ="Send File",command= sendFile)
button.pack(side=BOTTOM)
button1 = Button(frame1, text ="record",command= start_record, bg='#63dd7a',state='normal')
button1.pack(side=LEFT)
button2 = Button(frame1, text ="stop & send",command= stop_record, bg='#e86f5e',state=DISABLED)
button2.pack(side=LEFT)
button3 = Button(frame1, text ="Record-Call",bg = '#5d37f9',state='normal')
button3.pack(side=RIGHT)
button4 = Button(frame1, text ="End",command= end_call,bg = '#df2620',state=DISABLED)
button4.pack(side=RIGHT)
button3 = Button(frame1, text ="Call",command= call, bg = '#29ab22',state='normal')
button3.pack(side=RIGHT)
button5 = Button(frame, text ="New Chat",command= new_chat,bg = '#0b2fee')
button5.pack(side=TOP,fill=X)
message = StringVar()
entry1 = Entry(window,text = message)
entry1.pack(side=BOTTOM,fill=X)
entry1.bind("<Return>", sendMessage)
scrollbar = Scrollbar(window)
scrollbar.pack(side=RIGHT, fill=Y)
listbox = Listbox(window, width = 50,yscrollcommand=scrollbar.set)
window.protocol("WM_DELETE_WINDOW", on_closing)
label1 = Label( frame, text="Connect to IP address : " , bg = "#a3dfdd" )
label1.pack(fill = X, side = LEFT, ipadx=20)
ipaddress = StringVar()
entry2 = Entry(frame,text = ipaddress,width=30)
entry2.pack(side=LEFT)
entry2.bind("<Return>", setIP)
frame.pack()
window.mainloop()