-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
70 lines (44 loc) · 1.48 KB
/
server.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
import socket
import os
SIZE = 1024
PORT = 12345
my_socket = socket.socket()
my_socket.bind(("", PORT))
my_socket.listen()
print("[+] Welcome to FTP server\n")
def cli():
command = client_socket.recv(SIZE).decode()
if command == 'upload':
upload()
elif command == 'download':
download()
elif command == 'list':
list_files()
else:
pass
def upload():
file_name = client_socket.recv(SIZE).decode().split('/')[-1]
print(f"[+] [received] upload, file name: {file_name}")
with open(f'/home/david/mefathim4/socket/server/{file_name}', 'wb+') as f:
data = client_socket.recv(SIZE)
while data:
f.write(data)
data = client_socket.recv(SIZE)
print("[!] Done")
def download():
file_name = client_socket.recv(SIZE).decode().split('/')[-1]
print(f"[+] [received] download, file name: {file_name}")
with open(f'/home/david/mefathim4/socket/server/{file_name}', 'rb') as f:
data = f.read(SIZE)
client_socket.send(data)
print("[!] Done")
def list_files():
print(f"[+] [received] list all files")
all_files = os.listdir('/home/david/mefathim4/socket/server/')
client_socket.send(str(all_files).encode())
print("[!] Done")
if __name__=="__main__":
while True:
client_socket, address = my_socket.accept()
print(f'[!] {address} is connected')
cli()