-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.py
195 lines (163 loc) · 5.21 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
#Assignment 1 by Aman Varshney(201402188) and Vinay Kumar Singh(201402035)
import socket
import sys
import os
import hashlib
HOST = 'localhost' #server name goes in here
PORT = 5000
def put(commandName):
socket1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket1.connect((HOST, PORT))
socket1.send(commandName)
string = commandName.split(' ')
inputFile = string[1]
with open(inputFile, 'rb') as file_to_send:
for data in file_to_send:
print "Client se jaata hua " + data
socket1.send(data)
print 'Upload Successful'
socket1.close()
return
def get(commandName):
socket1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket1.connect((HOST, PORT))
socket1.send(commandName)
string = commandName.split(' ')
inputFile = string[1]
with open(inputFile, 'wb') as file_to_write:
while True:
data = socket1.recv(1024)
if not data:
break
# print data
file_to_write.write(data)
file_to_write.close()
print 'Download Successful'
socket1.close()
return
def FileHash(commandName):
string = commandName.split(' ')
if string[1] == 'verify':
verify(commandName)
elif string[1] == 'checkall':
checkall(commandName)
def verify(commandName):
socket1=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket1.connect((HOST, PORT))
socket1.send(commandName)
hashValServer=socket1.recv(1024)
string = commandName.split(' ')
BLOCKSIZE = 65536
hasher = hashlib.sha1()
with open(string[2], 'rb') as afile:
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(BLOCKSIZE)
hashValClient = hasher.hexdigest()
print('hashValServer= %s', (hashValServer))
print('hashValClient= %s', (hashValClient))
if hashValClient == hashValServer:
print 'No updates'
else:
print 'Update Available'
socket1.close()
return
def checkall(commandName):
socket1=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket1.connect((HOST, PORT))
socket1.send(commandName)
string = commandName.split(' ')
BLOCKSIZE = 65536
hasher = hashlib.sha1()
# f=socket1.recv(1024)
while True:
f=socket1.recv(1024)
with open(f, 'rb') as afile:
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(BLOCKSIZE)
hashValClient = hasher.hexdigest()
hashValServer=socket1.recv(1024)
print ('Filename = %s', f)
print('hashValServer= %s', (hashValServer))
print('hashValClient= %s', (hashValClient))
if hashValClient == hashValServer:
print 'No updates'
else:
print 'Update Available'
if not f:
break
socket1.close()
return
def quit():
socket1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket1.connect((HOST, PORT))
socket1.send(commandName)
socket1.close()
return
def IndexGet(commandName):
socket1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket1.connect((HOST, PORT))
string = commandName.split(' ')
if string[1] == 'shortlist':
socket1.send(commandName)
strng=socket1.recv(1024)
strng=strng.split('\n')
for f in strng:
print f
elif (string[1]=='longlist'):
socket1.send(commandName)
path=socket1.recv(1024)
rslt=path.split('\n')
for f in rslt[1:]:
print f
socket1.close()
return
def serverList(commandName):
socket1=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket1.connect((HOST, PORT))
socket1.send(commandName)
fileStr=socket1.recv(1024)
fileList=fileStr.split(' ')
for f in fileList[:-1]:
print f
socket1.close()
return
msg = raw_input('Enter your name: ')
while(1):
print "\n"
print "****************"
print 'Instruction'
print '"FileUpload [filename]" to send the file the server '
print '"FileDownload [filename]" to download the file from the server '
print '"ls" to list all files in this directory'
print '"lls" to list all files in the server'
print '"IndexGet shortlist <starttimestamp> <endtimestamp>" to list the files modified in mentioned timestamp.'
print '"IndexGet longlist" similar to shortlist but with complete file listing'
print '"FileHash verify <filename>" checksum of the modification of the mentioned file.'
print '"quit" to exit'
print "\n"
sys.stdout.write ('%s> ' %msg)
inputCommand = sys.stdin.readline().strip()
if (inputCommand == 'quit'):
quit()
break
elif (inputCommand == 'ls'):
path = os.getcwd()
dirs = os.listdir(path)
for f in dirs:
print f
elif (inputCommand == 'lls'):
serverList('lls')
else:
string = inputCommand.split(' ')
if string[0] == 'FileDownload':
get(inputCommand)
elif string[0] == 'FileUpload':
put(inputCommand)
elif string[0] =='IndexGet':
IndexGet(inputCommand)
elif string[0] == 'FileHash':
FileHash(inputCommand)