forked from VladimirAndropov/fa-np-practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftp_test.py
123 lines (113 loc) · 3.67 KB
/
ftp_test.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
import socket
import re
import os
HOST = 'localhost'
PORT = 9090
END_FLAG = b"$$STREAM_FILE_END_FLAG$$"
FAIL_FLAG = b'$FAILED$'
# Логин и пароль для тестирования
login = password = 'Mashirov'
current_directory = "\\"
# Функция создания сообщения для отправки
def creator(message, size=0):
global login, password, current_directory
return f"{login}=login{password}=password{current_directory}=cur_dir{size}=file_size{message}".encode()
# Функция получения файла
def receiving(sock, request):
global FAIL_FLAG, END_FLAG
try:
flag_finder = sock.recv(1024)
if FAIL_FLAG in flag_finder:
print((flag_finder.replace(FAIL_FLAG, b"")).decode())
else:
filename = re.split("[ \\/]+", request)[-1]
with open(filename, "wb") as bytefile:
while True:
if END_FLAG in flag_finder:
bytefile.write(flag_finder.replace(END_FLAG, b""))
break
else:
bytefile.write(flag_finder)
flag_finder = sock.recv(1024)
except Exception as e:
print(f"Произошла ошибка при получении файла: {e}")
# Функция отправки файла
def sending(sock, request):
global END_FLAG
filename = re.split("[ \\/]+", request)[-1]
if os.path.exists(filename):
try:
size = os.path.getsize(filename)
sock.send(creator(request, size))
enought_flag = sock.recv(1024).decode()
if enought_flag != '$ENOUGHT$':
print(enought_flag)
return
with open(filename, "rb") as bytefile:
while read_bytes := bytefile.read(1024):
sock.send(read_bytes)
sock.send(END_FLAG)
except Exception as e:
print(f"Произошла ошибка при отправке файла: {e}")
else:
print("Нет такого файла")
# Основная функция для выполнения команды
def main(command):
global current_directory
sock = socket.socket()
print(">", command)
request = command.strip()
if request == "exit":
print("goodbye")
return
try:
sock.connect((HOST, PORT))
if request[:9] == "send_file":
if request == "send_file":
print("Нет такого файла")
else:
sending(sock, request)
else:
sock.send(creator(request))
if request[:9] == "get_file " or request == "get_file":
receiving(sock, request)
else:
response = sock.recv(1024).decode()
if request[:3] == "cd " or request == "cd":
current_directory = response
else:
print(response)
except Exception as e:
print(f"Произошла ошибка: {e}")
finally:
sock.close()
# Список команд для выполнения
commands = [
"mkdir test1",
"cd ...../test1",
"cd ./test1",
"mkdir ../test1/test11",
"ls",
"pwd",
"rmtree test11",
"ls",
"touch 1.txt",
"rename 1.txt 11.txt",
"ls",
"remove 1.txt",
"cat 1.txt",
"cat 11.txt",
"cd ////",
"pwd",
"cd \\",
"pwd",
"rmtree test1",
"ls"
]
# Итерация через команды и их выполнение
for command in commands:
try:
main(command)
except:
print('Некорректная работа!')
raise