-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedfs.py
180 lines (176 loc) · 6.29 KB
/
edfs.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
from ipaddress import ip_address
import socket
import sys
import os
import pathlib
import subprocess
import time
def main(argvs):
# build connection
port = 5050
ip_addr = socket.gethostbyname(socket.gethostname())
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
client.connect((ip_addr, port))
# process command case by case
if argvs[0] == '-ls':
if len(argvs) > 2:
print(
'Error: Please follow the format: -ls [path to directory on server]')
client.close()
sys.exit()
else:
f = argvs[1]
ind = f.rfind('.')
if ind != -1:
print('Error: Please specify path to a directory instead of file.')
client.close()
sys.exit()
elif argvs[0] == '-rm':
if len(argvs) > 2:
print('Error: Please follow the format: -rm [path to file]')
client.close()
sys.exit()
else:
f = argvs[1]
ind = f.rfind('.')
if ind == -1:
print('Error: Please specify path to a file instead of directory.')
client.close()
sys.exit()
elif argvs[0] == '-put':
if len(argvs) != 3:
print(
'Error: Please follow the format: -put [path to file on local machine] [path to file on server]')
client.close()
sys.exit()
else:
ind1 = argvs[1].rfind('.')
ind2 = argvs[2].rfind('.')
if ind1 == -1 or ind2 == -1:
print('Error: Please specify path to a file instead of directory.')
client.close()
sys.exit()
elif not os.path.isfile(argvs[1]):
print('Error: Failed to find file on your local machine.')
client.close()
sys.exit()
else:
exts1 = pathlib.Path(argvs[1]).suffix
exts2 = pathlib.Path(argvs[2]).suffix
if exts1 != exts2:
print('Error: Please make sure the file extensions match.')
client.close()
sys.exit()
elif argvs[0] == '-get':
if len(argvs) != 3:
print(
'Error: Please follow the format: -get [path to file on server] [path to file on local machine]')
client.close()
sys.exit()
else:
ind1 = argvs[1].rfind('.')
ind2 = argvs[2].rfind('.')
ind3 = argvs[2].rfind('/')
if ind1 == -1 or ind2 == -1:
print('Error: Please specify path to a file instead of directory.')
client.close()
sys.exit()
elif not os.path.exists(argvs[2][:ind3]):
print('Error: Please specify a valid location to save your file.')
client.close()
sys.exit()
else:
exts1 = pathlib.Path(argvs[1]).suffix
exts2 = pathlib.Path(argvs[2]).suffix
if exts1 != exts2:
print('Error: Please make sure the file extensions match.')
client.close()
sys.exit()
elif argvs[0] == '-mkdir':
if len(argvs) != 2:
print(
'Error: Please follow the format: -mkdir [path to directory on server]')
client.close()
sys.exit()
else:
f = argvs[1]
ind = f.rfind('.')
if ind != -1:
print('Error: Please specify path to a directory instead of file.')
client.close()
sys.exit()
elif argvs[0] == '-rmdir':
if len(argvs) != 2:
print(
'Error: Please follow the format: -rmdir [path to directory on server]')
client.close()
sys.exit()
else:
f = argvs[1]
ind = f.rfind('.')
if ind != -1:
print('Error: Please specify path to a directory instead of file.')
client.close()
sys.exit()
elif argvs[0] == '-cat':
if len(argvs) != 2:
print(
'Error: Please follow the format: -cat [path to file on server]')
client.close()
sys.exit()
else:
f = argvs[1]
ind = f.rfind('.')
if ind == -1:
print('Error: Please specify path to a file instead of directory.')
client.close()
sys.exit()
elif argvs[0] == '-fs':
if len(argvs) != 2:
print(
'Error: Please follow the format: -fs [path to file on server]')
client.close()
sys.exit()
else:
f = argvs[1]
ind = f.rfind('.')
if ind == -1:
print('Error: Please specify path to a file instead of directory.')
client.close()
sys.exit()
else:
print('Error: Invalid command. The system only accepts the following requests: ls, rm, put, get, mkdir, rmdir, cat, and fs.')
client.close()
sys.exit()
# build connection to server and send command line arguments to the server
s = ' '.join(argvs)
msg = s.encode('utf-8')
msg_len = str(len(msg)).encode('utf-8')
msg_len += b' '*(30-len(msg_len))
client.send(msg_len)
client.send(msg)
# waiting for server's response, non-empty responses will be printed
while True:
msg_len = client.recv(30).decode('utf-8', 'ignore')
if msg_len:
msg_len = int(msg_len)
if msg_len == -1:
break
else:
received = 0
s = ''
while received < msg_len:
msg = client.recv(2048).decode('utf-8', 'ignore')
received += 2048
s += msg
print(s)
client.close()
break
# verify number of arguments and then call the main function
if __name__ == '__main__':
if len(sys.argv) <= 2 or len(sys.argv) > 4:
print('Error: Invalid command.')
p = subprocess.Popen(["python", "server.py"])
time.sleep(0.8)
main(sys.argv[1:])