-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.py
More file actions
158 lines (146 loc) · 3.46 KB
/
client.py
File metadata and controls
158 lines (146 loc) · 3.46 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
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
import socket
import sys
import os
import datetime
import hashlib
def shortlist(socket1):
while 1:
data = socket1.recv(1024).decode()
if data.endswith('||end||'):
print data[:-7]
break
print data
def longlist(socket1):
while 1:
data = socket1.recv(1024).decode()
if data.endswith('||end||'):
print data[:-7]
break
print data
def Filehashsingle(socket1):
data = socket1.recv(1024).decode()
print data
def Filehashmultiple(socket1):
while 1:
data = socket1.recv(1024).decode()
if data.endswith('||end||'):
print data[:-7]
break
print data
def downloadFile(s,file1,message):
flag = 0
with open(file1, 'wb') as f:
s.send(message.encode())
while 1:
data = s.recv(1024)
if (data.decode() == 'Error: File Not Found!'):
print('Error opening file or file does not exist')
flag = 1
break
f.write(data)
if len(data) < 1024:
print 'File downloaded'
break
f.close()
if (flag == 0):
data = s.recv(1024).decode()
print data
else:
pass
def downloadFile_udp(s,file1,host,port_udp,message):
flag = 0
udp_soc=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_soc.bind((host, port_udp))
f = open(file1, 'wb')
s.send(message.encode())
try:
while(True):
data, addr=udp_soc.recvfrom(1024)
if (data == 'Error: File Not Found!'):
print('Error opening file or file does not exist')
flag = 1
break
udp_soc.settimeout(2)
f.write(data)
except socket.timeout:
f.close()
udp_soc.close()
f.close()
if (flag == 0):
data = s.recv(1024).decode()
print data
print 'File Downloaded'
else:
f.close()
udp_soc.close()
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print "Socket object created successfully"
port_udp = 12347
port=8080
host=socket.gethostname()
s.connect((host, port))
while True:
try:
data = s.recv(1024).decode()
print(data)
message = raw_input("--> ")
while True:
msg = message.strip().split(' ')
if (msg[0] == 'IndexGet'):
if msg[1]=='shortlist':
if ((len(msg) == 6) or len(msg) == 7):
s.send(message.encode())
shortlist(s)
else:
print('Please use appropriate flags')
elif ((msg[1]=='longlist') and (len(msg)<=3)):
if ((len(msg)==2) or(len(msg)==3 and msg[2]== '*.txt')) :
s.send(message.encode())
longlist(s)
else:
print('Please use appropriate flag')
else:
print('Please use appropriate flag')
elif msg[0]=='FileHash':
s.send(message.encode())
if (msg[1]=='verify' and len(msg) == 3):
Filehashsingle(s)
elif (msg[1]=='checkall' and len(msg) == 2):
Filehashmultiple(s)
else:
print("Please use appropriate flags")
elif msg[0] == 'FileDownload':
if (len(msg) == 4):
path = msg[3]
else:
addr = msg[1].split('/')
path = addr[-1]
if (len(msg) == 2):
downloadFile(s,path,message)
elif (msg[2]=='TCP'):
downloadFile(s,path,message)
elif (msg[2]=='UDP'):
downloadFile_udp(s,path,host,port_udp,message)
else:
print("Please use appropriate flags")
elif msg[0] == 'exit':
s.send(message.encode())
s.close()
sys.exit()
else:
print 'Invalid Command'
message = raw_input("--> ")
except KeyboardInterrupt:
print 'Socket Closed'
s.close()
sys.exit()
except IndexError:
print 'Invalid Command'
continue
except IOError:
print 'Wrong File Path'
continue
'''
To download File : FileDownload pathofFileServer pathofFileClient TCP/UDP
'''