-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
executable file
·108 lines (103 loc) · 2.84 KB
/
client.py
File metadata and controls
executable file
·108 lines (103 loc) · 2.84 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
import socket
import os
import hashlib
ENDSTRING = 'oUtAnDoVeRHello'
s = socket.socket()
host = ""
port = input("Enter port for client: ")
s.connect((host, port))
folder = raw_input("Path of folder: ")
os.chdir(folder)
# s.send("Hello server!")
def md5(fname):
hash_md5 = hashlib.md5()
with open(fname, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def receiveFunc(com):
s.send(com)
data = s.recv(1024)
s.send('received')
retstr = ''
while data != ENDSTRING:
# print data+'\n'
retstr += data
data = s.recv(1024)
s.send('received')
# print retstr
return retstr
def indexFunc( com ):
com1 = com.split()
text = receiveFunc(com)
# s.recv(1024)
print text
return text
def downloadFunc(com):
s.send(com)
com = com.split()
if com[1] == 'TCP':
f = open(com[2],'wb')
data = s.recv(1024)
s.send('received')
while data!= ENDSTRING:
f.write(data)
data = s.recv(1024)
s.send('received')
f.close()
elif com[1] == 'UDP':
port2 = int(s.recv(1024))
soc2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
addr2 = (host,port2)
soc2.sendto('received',addr2)
f = open(com[2],'wb')
while True:
text,addr2 = soc2.recvfrom(1024)
# print "Text received: ",text
if text == ENDSTRING:
break
f.write(text)
soc2.sendto('received',addr2)
f.close()
soc2.close()
# s.recv(1024)
FLAG = True
while FLAG==True:
com = raw_input("prompt> ")
com1 = com.split()
if len(com1) >= 2 :
if(com1[0]=='index'):
indexFunc(com)
elif(com1[0]=='hash'):
indexFunc(com)
elif(com1[0]=='download'):
hash5 = indexFunc('hash verify '+com1[2]).split()[0]
downloadFunc(com)
if com1[1] == 'UDP':
loc5 = md5(com1[2])
print "Hash of file in remote: ",hash5
print "Hash of file in local: ",loc5
if loc5 == hash5:
print "Both the files are same!!!!"
else:
print "UDP messed up, files are not the same"
elif(com1[0]=='exit'):
FLAG=False
else:
print "Not a correct command\n"
else:
print "Not a correct command\n"
s.close()
print('connection closed')
# with open('received_file', 'wb') as f:
# print 'file opened'
# while True:
# print('receiving data...')
# data = s.recv(1024)
# print('data=%s', (data))
# if not data:
# break
# # write data to a file
# f.write(data)
# f.close()
# print('Successfully get the file')