-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.py
More file actions
227 lines (168 loc) · 6.67 KB
/
Copy pathclient.py
File metadata and controls
227 lines (168 loc) · 6.67 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
import socket
import os
IP = 'localhost'
PORT = 2100
ADDR = (IP, PORT)
FORMAT = "utf-8"
SIZE = 1024
SAVE_DIR = '/home/lash/Downloads/'
def handle_stor(command, control_channel):
# Check if command is valid
if len(command.split(' ')) != 3:
print("STOR command is not valid.")
return
_, client_path, server_path = command.split(' ')
if os.path.exists(client_path):
file_size = os.path.getsize(client_path)
else:
print('550 File not found')
return
print(f'filesize: {file_size}')
# Send the command to the server with the size of file.
control_channel.sendall(f'STOR {server_path} {file_size}'.encode(FORMAT))
# Recieve data port to connect to
server_response = control_channel.recv(SIZE).decode().split(' ')
if server_response[0] == '200':
data_port = int(server_response[2])
else:
print(' '.join(server_response))
return
print(f'data port: {data_port}')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as data_channel:
data_channel.connect((IP, data_port))
with open(client_path, 'rb') as file:
while True:
data = file.read(SIZE)
if not data:
break
data_channel.sendall(data)
server_response = control_channel.recv(SIZE).decode()
print(server_response)
# End of STOR
def handle_retr(command, control_channel):
# Check if command is valid
if len(command.split(' ')) != 2:
print("RETR command is not valid.")
return
# Send the command to the server
control_channel.send(command.encode(FORMAT))
# Extract filename from the command
filename = SAVE_DIR + command.split(' ')[1].split('/')[-1]
# Get the port number and size of file from the server
# PORT {port_num} {file_size}
server_response = control_channel.recv(SIZE).decode()
if server_response.split(' ')[0] == "550": # Check if the file exists
print(server_response)
return
data_port = int(server_response.split(' ')[2])
file_size = int(server_response.split(' ')[4])
print(f'data_port: {data_port}, file_size:{file_size}')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as data_channel:
data_channel.connect((IP, data_port))
with open(filename, 'wb') as file:
rcv_size = 0
while True:
data = data_channel.recv(SIZE)
file.write(data)
rcv_size += len(data)
if rcv_size >= file_size:
break
server_response = control_channel.recv(SIZE).decode()
print(server_response)
# End of RETR
def handle_report(control_channel):
control_channel.send("REPORT".encode(FORMAT))
# Get the port number and size of file from the server
# PORT: {port_num}, FILE_SIZE: {file_size}
server_response = control_channel.recv(SIZE).decode()
data_port = int(server_response.split(' ')[1])
file_size = int(server_response.split(' ')[3])
print(f'data_port: {data_port}, file_size:{file_size}')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as data_channel:
data_channel.connect((IP, data_port))
rcv_size = 0
report = ""
while True:
data = data_channel.recv(SIZE).decode()
report += data
rcv_size += len(data)
if rcv_size >= file_size:
break
print(report)
server_response = control_channel.recv(SIZE).decode()
print(server_response)
def handle_dele_rmd(command, control_channel):
# Check if the user is sure
choice = input("Do you really wish to delete y/n? ")
if choice == 'y' or choice == 'Y':
# Send the command to the server
control_channel.send(command.encode(FORMAT))
server_response = control_channel.recv(SIZE).decode()
print(server_response)
# Else do nothing
def handle_list(command, control_channel):
control_channel.send(command.encode(FORMAT))
file_size = int(control_channel.recv(SIZE).decode().split('n')[0].split(' ')[1])
rcv_size = 0
listing = ""
while True:
data = control_channel.recv(SIZE).decode()
listing += data
rcv_size += len(data)
if rcv_size >= file_size:
break
print(listing)
def main():
""" Starting a TCP socket. """
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client:
""" Connecting to the server. """
client.connect(ADDR)
server_response = client.recv(SIZE).decode()
print(server_response)
username = input("Enter your Username: ")
client.sendall(f"USER {username}".encode())
server_response = client.recv(SIZE).decode()
print(server_response)
# Check if Username was not valid.
if not server_response.startswith("200"):
client.close()
main()
password = input("Enter your Password: ")
client.sendall(f"PASS {password}".encode())
server_response = client.recv(SIZE).decode()
print(server_response)
# Check if Password was not valid.
if not server_response.startswith("200"):
client.close()
main()
while True: # Main loop
command = input("Enter your command: ")
if command.upper().startswith("STOR"):
handle_stor(command=command, control_channel=client)
elif command.upper().startswith("RETR"):
handle_retr(command=command, control_channel=client)
elif command.upper().startswith("DELE") or command.upper().startswith("RMD"):
handle_dele_rmd(command=command, control_channel=client)
elif command.upper().startswith("REPORT"):
handle_report(control_channel=client)
elif command.upper().startswith("LIST"):
handle_list(command=command, control_channel=client)
elif command.upper().startswith("QUIT"):
# Send the command to the server
client.send(command.encode(FORMAT))
# Close the connection after receiving server's response and then break
server_response = client.recv(SIZE).decode()
print(server_response)
client.close()
break
else: # For other command:
# Send the command to the server
client.send(command.encode(FORMAT))
# Get the response
server_response = client.recv(SIZE).decode()
print(server_response)
if __name__ == "__main__":
try:
main()
except Exception as e:
print(e)