-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.py
More file actions
267 lines (222 loc) · 8.21 KB
/
client.py
File metadata and controls
267 lines (222 loc) · 8.21 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
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
import os
import socket
import ast
import subprocess
import sys
# set directory for file server
root = "Root/"
def open_file(file_name):
fd = open(root + file_name)
return fd
def close_file(fd):
fd.close()
def delete_file(file_name):
if os.path.exists(root + file_name):
os.remove(root + file_name)
return True
else:
return False
def create_file(file_name):
try:
with open(root + file_name, "x") as fd:
fd.close()
return True
except FileExistsError:
return False
def read_from_file(file_name):
fd = open(root + file_name, "r")
message = fd.read()
return message
def write_to_file(file_name, message):
fd = open(root + file_name, "w")
fd.write(message)
def append_to_file(file_name, message):
fd = open(root + file_name, "a")
fd.write(message)
def list_files(server_sock, directory):
while True:
data = server_sock.recv(1024)
if not data:
break
temp_list = data
for item in temp_list:
print(item)
def send_file(server_sock, file_name):
with open(root + file_name, 'rb') as fd:
data = fd.read(1024)
while data:
# print(data)
server_sock.sendall(data)
data = fd.read(1024)
fd.close()
server_sock.sendall('###'.encode())
def recieve_file(client_sock, file_name):
data = client_sock.recv(1024)
with open(root + file_name, 'wb') as fd:
while True:
if data.decode().endswith('###'):
data = data[:-3]
fd.write(data)
break
fd.write(data)
data = client_sock.recv(1024)
fd.close()
def create_directory(directory_name):
if not os.path.exists(root + directory_name):
os.mkdir(root + directory_name)
print("Directory ", directory_name, " Created ")
else:
print("Directory ", directory_name, " already exists")
def check_extension(file_name):
return(file_name.lower().endswith(('.txt', '.c', '.py')))
def main():
print("Project DFS!")
# get server ip
print("Enter server IP")
msg = input()
host = msg
#host = "127.0.0.1"
# get server port
print("Enter server port")
msg = input()
port = int(msg)
#port = 9999
while True:
# create a socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connection to hostname on the port.
s.connect((host, port))
# Receive no more than 1024 bytes
msg = s.recv(1024)
print(msg.decode())
while True:
print("Write a command to execute or type help")
print(">>> ", end='', flush=True)
command = input()
if len(command) < 1:
print("Please write a command or type help")
# ------------------------------------------------
elif command[:5] == "hello":
print("hello from client")
# ------------------------------------------------
elif command == "exit":
s.sendall(command.encode())
s.close()
sys.exit()
# -------------------------------------------------
elif command[:4] == "open":
open_file(command[5:])
# ------------------------------------------------
elif command[:5] == "close":
open_file(command[6:])
# ------------------------------------------------
elif command[:4] == "read":
s.sendall(command.encode())
data = s.recv(1024).decode()
if(data == 'File not readable'):
print(data)
elif(data == "sending file"):
recieve_file(s, command[5:])
print("Press 1 to read in terminal")
print("Press 2 to read in editor")
msg = input()
if(int(msg) == 1):
message = read_from_file(command[5:])
print(message)
else:
proc = subprocess.Popen(
['leafpad', root + command[6:]])
proc.wait()
elif(data[:10] == "connect to"):
print("connecting to a different server")
print("please enter the command again")
host = data[11:]
s.close()
break
else:
print(data)
# ----------------------------------------------
elif command[:6] == "create":
if(check_extension(command[5:])):
s.sendall(command.encode())
data = s.recv(1024).decode()
if(data == "File already exists"):
print(data)
elif(data == "create possible"):
print("file created on server")
else:
print("error in create")
else:
print('No support for this file extension')
# ----------------------------------------------
elif command[:6] == "delete":
s.sendall(command.encode())
data = s.recv(1024).decode()
if(data == "file deleted"):
print(data)
elif(data == "file delete error"):
print(data)
elif(data[:10] == "connect to"):
host = data[11:]
port = 9998
s.close()
break
else:
print("error in create")
# -------------------------------------------
elif command[:5] == "write":
s.sendall(command.encode())
data = s.recv(1024).decode()
if(data == "sending file"):
recieve_file(s, command[6:])
proc = subprocess.Popen(['leafpad', root + command[6:]])
proc.wait()
# print("enter message to write")
# msg = input()
# write_to_file(command[6:], msg)
send_file(s, command[6:])
elif(data[:10] == "connect to"):
print("connecting to a different server")
print("please enter the command again")
host = data[11:]
s.close()
break
else:
print("error in write")
# --------------------------------------------
elif command[:6] == "append":
s.sendall(command.encode())
data = s.recv(1024).decode()
if(data == "sending file"):
recieve_file(s, command[7:])
proc = subprocess.Popen(['leafpad', root + command[7:]])
proc.wait()
# print("enter message to write")
# msg = input()
# append_to_file(command[7:], msg)
send_file(s, command[7:])
elif(data[:10] == "connect to"):
print("connecting to a different server")
print("please enter the command again")
host = data[11:]
s.close()
break
else:
print("error in append")
# ------------------------------------------
elif command == "list":
s.sendall((command).encode())
data = s.recv(1024).decode()
while(data[-3:] != "###"):
data += s.recv(1024).decode()
data = data[:-3]
print(data)
# -----------------------------------------
# elif command_split[0] == "mkdir":
# create_directory(command_split[1])
# # elif(command_split[0]=="close"):
# elif command_split[0] == "exit":
# break
else:
print("Invalid intruction. Type help")
main()