forked from network02/network-project-phase02-momonut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
112 lines (85 loc) · 3.45 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
import socket
import subprocess
import shlex
SERVER = 'localhost'
DATA_PORT = 8020
CTRL_PORT = 8021
BUFFER_SIZE = 1024
def send(request):
try:
control_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
control_socket.settimeout(2)
control_socket.connect((SERVER, CTRL_PORT))
control_socket.sendall(request.encode())
response = control_socket.recv(1024).decode()
control_socket.close()
except:
response = 'Server is Offline'
return response
def main():
request = 'HELP'
response = send(request)
print(response)
while True:
request = input().strip()
response = send(request)
if response == 'Server is Offline':
print(response)
break
if response == 'Ready Retr':
data_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
data_socket.settimeout(2)
data_socket.connect((SERVER, DATA_PORT))
file_name = './downloads/' + request.split()[1].split('/')[-1]
command = 'mkdir -p ./downloads/'
subprocess.run(shlex.split(command), stdout=subprocess.PIPE).stdout.decode()
command = 'rm -rf ' + file_name
subprocess.run(shlex.split(command), stdout=subprocess.PIPE).stdout.decode()
command = 'touch ' + file_name
subprocess.run(shlex.split(command), stdout=subprocess.PIPE).stdout.decode()
try:
with open(file_name, 'ab') as f:
while True:
data = data_socket.recv(BUFFER_SIZE)
if not data:
break
f.write(data)
data_socket.close()
control_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
control_socket.settimeout(2)
control_socket.connect((SERVER, CTRL_PORT))
response = control_socket.recv(1024).decode()
control_socket.close()
except:
response = '400 Connection loss\n'
elif response == 'Ready Stor':
data_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
data_socket.settimeout(2)
data_socket.connect((SERVER, DATA_PORT))
file_name = request.split()[1]
try:
with open(file_name, 'rb') as f:
data = f.read(BUFFER_SIZE)
while data:
data_socket.sendall(data)
data = f.read(BUFFER_SIZE)
data_socket.close()
control_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
control_socket.settimeout(2)
control_socket.connect((SERVER, CTRL_PORT))
response = control_socket.recv(1024).decode()
control_socket.close()
except:
response = '400 Connection loss\n'
elif response == 'Not Ready':
control_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
control_socket.settimeout(2)
control_socket.connect((SERVER, CTRL_PORT))
response = control_socket.recv(1024).decode()
control_socket.close()
elif response == '200 Goodnight!':
print(response)
break
print(response)
if __name__ == '__main__':
main()