-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileClientCLI.py
More file actions
140 lines (114 loc) · 3.67 KB
/
FileClientCLI.py
File metadata and controls
140 lines (114 loc) · 3.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
import socket
import json
import base64
import logging
import os
IP = '0.0.0.0'
PORT = 6666
server_address=(IP, PORT)
def send_command(command_str=""):
global server_address
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(server_address)
logging.warning(f"connecting to {server_address}")
try:
logging.warning(f"sending message: {command_str[:10]}...")
sock.sendall(command_str.encode())
data_received=""
while True:
data = sock.recv(4096)
if data:
data_received += data.decode()
if "\r\n\r\n" in data_received:
break
else:
break
hasil = json.loads(data_received)
logging.warning("data received from server:")
return hasil
except:
logging.warning("error during data receiving")
return False
def remote_list():
command_str=f"LIST"
command_str = command_str + "\r\n\r\n"
hasil = send_command(command_str)
if hasil['status'] == 'OK':
print("\n\nDaftar File yang Tersedia:")
print("=============================")
for nmfile in hasil['data']:
print(f" • {nmfile}")
print("=============================\n\n")
return True
else:
print("Gagal")
return False
def remote_get(filename=""):
command_str = f"GET {filename}"
command_str = command_str + "\r\n\r\n"
hasil = send_command(command_str)
if (hasil['status']=='OK'):
namafile= hasil['data_namafile']
isifile = base64.b64decode(hasil['data_file'])
fp = open(namafile, 'wb+')
fp.write(isifile)
fp.close()
print(f"File {filename} berhasil didownload")
return True
else:
print("Gagal")
return False
def remote_add(filename=""):
isFileExist = os.path.exists(filename)
if not isFileExist:
print(f"File {filename} tidak ditemukan...")
return False
content = open(filename, 'rb').read()
decodedContent = base64.b64encode(content).decode()
command_str = f"ADD {filename} "
fullCommand = command_str + decodedContent + "\r\n\r\n"
result = send_command(fullCommand)
if (result['status']=='OK'):
print(f"File {filename} berhasil diupload")
return True
else:
print("Gagal")
return False
def remote_delete(filename=""):
command_str = f"DELETE {filename}"
command_str = command_str + "\r\n\r\n"
hasil = send_command(command_str)
if (hasil['status']=='OK'):
print(f"File {filename} berhasil dihapus")
return True
else:
print("Gagal")
return False
if __name__=='__main__':
server_address=('localhost', 6666)
while True:
print("File Transfer Client")
print("====================")
print("1. List file")
print("2. Download file")
print("3. Upload file")
print("4. Delete file")
print("5. Exit")
print("====================")
cmd = input("Pilih menu: ")
if cmd == '1':
remote_list()
elif cmd == '2':
filename = input("Masukkan nama file: ")
remote_get(filename)
elif cmd == '3':
filename = input("Masukkan nama file: ")
remote_add(filename)
elif cmd == '4':
filename = input("Masukkan nama file: ")
remote_delete(filename)
elif cmd == '5':
print("Keluar dari program")
break
else:
print("Menu tidak dikenali")