Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions ftp-client.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import socket

HOST = 'localhost'
PORT = 6666

try:
os.system('clear')
os.system('cls')
except:
pass


while True:
request = input('>')

sock = socket.socket()
sock.connect((HOST, PORT))

sock.send(request.encode())

response = sock.recv(1024).decode()
print(response)

sock.close()
if request == 'exit':
print('Клиент закрыт')
break
else:
sock = socket.socket()
sock.connect((HOST, PORT))
sock.send(request.encode())

response = sock.recv(1024).decode()
print(response)

sock.close()
51 changes: 49 additions & 2 deletions ftp-server.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import socket
import os
import shutil
'''
pwd - показывает название рабочей директории
ls - показывает содержимое текущей директории
cat <filename> - отправляет содержимое файла
mkdir <Название папки> - создает новую папку
rmdir <Название папки> - удаляет папку
create <Название файла> - создает файл
remove <Название файла> - удаляет файл
rename <Название файла> - переименовывает файл
copy <Название файла> <Название нового файла> - копирует файл
exit - Выход (отключение клиента от сервера)
'''

dirname = os.path.join(os.getcwd(), 'docs')
Expand All @@ -13,6 +21,44 @@ def process(req):
return dirname
elif req == 'ls':
return '; '.join(os.listdir(dirname))
elif req[:3] == 'cat':
path = os.path.join(os.getcwd(), 'docs', req[4::])
if os.path.exists(path):
with open(path, 'r+') as f:
line = ''
for l in f:
line+=l
return line
else:
return 'Такого файла не существет'
elif req[:5] == 'mkdir':
path = os.path.join(os.getcwd(), 'docs', req[6::])
if not os.path.exists(path):
os.makedirs(path)
return f'Папка создана'
else:
return 'Такая папка уже существет'
elif req[:5] == 'rmdir':
path = os.path.join(os.getcwd(), 'docs', req[6::])
if os.path.exists(path):
shutil.rmtree(os.path.join(os.getcwd(), 'docs', req[6::]))
return f'Папка удалена'
else:
return 'Такой папки не существует'
elif req[:6] == 'create':
open(os.path.join(os.getcwd(), 'docs', req[7:]), 'tw', encoding='utf-8').close()
return f'Файл создан'
elif req[:6] == 'remove':
os.remove(os.path.join(os.getcwd(), 'docs', req[7:]))
return f'Файл удален'
elif req[:6] == 'rename':
req = req.split(' ')
os.rename(os.path.join(os.getcwd(), 'docs', req[1]), os.path.join(os.getcwd(), 'docs', req[2]))
return 'Файл переименован'
elif req[:4] == 'copy':
req = req.split(' ')
shutil.copyfile(os.path.join(os.getcwd(), 'docs', req[1]), os.path.join(os.getcwd(), 'docs', req[2]))
return 'Файл скопирован'
return 'bad request'


Expand All @@ -23,12 +69,13 @@ def process(req):
sock.listen()
print("Прослушиваем порт", PORT)


while True:
conn, addr = sock.accept()

request = conn.recv(1024).decode()
print(request)

response = process(request)
conn.send(response.encode())

Expand Down