-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
96 lines (77 loc) · 2.89 KB
/
server.py
File metadata and controls
96 lines (77 loc) · 2.89 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
from xmlrpc.server import SimpleXMLRPCServer
from tabulate import tabulate
import xmlrpc.client
import os
import pymysql
LISTEN_PORT = 8080
LISTEN_HOST = "127.0.0.1"
connection = pymysql.connect(db="ftp", user="root", password="")
def perform_update(column_name, username):
# Execute cursor
cursor = connection.cursor(pymysql.cursors.DictCursor)
cursor.execute(f'SELECT id, {column_name} FROM ftp WHERE username=%s', username)
# Convert cursor to dictionary
client = cursor.fetchone()
updated_data = client[column_name] + 1
update_cursor = connection.cursor()
# Update data
update_cursor.execute(f'UPDATE ftp SET {column_name} = %s WHERE id = %s', (updated_data, client['id']))
# Close cursor
cursor.close()
update_cursor.close()
# Commit change
connection.commit()
clients_activity()
def login(credentials):
username, password = credentials.split(',')
cursor = connection.cursor(pymysql.cursors.DictCursor)
cursor.execute('SELECT username, password FROM ftp WHERE username=%s', username)
client = cursor.fetchone()
cursor.close()
return True if client and client['password'] == password else False
def download(file_name, username):
if os.path.exists(f'storage/{file_name}'):
perform_update('activity_download', username)
with open(f'storage/{file_name}', 'rb') as file:
return xmlrpc.client.Binary(file.read())
else:
print("Error: File not found")
return xmlrpc.client.Binary(b'Error: File not found !!')
def upload(file_data, filename, username):
if not os.path.exists(f'storage/{filename}'):
with open(f"storage/{filename}", 'wb') as file:
file.write(file_data.data)
perform_update('activity_upload', username)
return True
else:
print('Error: File already exists')
return False
def show_all_files():
files = ','.join(os.listdir('storage')).encode()
return xmlrpc.client.Binary(bytes(files))
def clients_activity():
cursor = connection.cursor(pymysql.cursors.DictCursor)
cursor.execute('SELECT username, activity_upload, activity_download FROM ftp;')
clients = cursor.fetchall()
header = clients[0].keys()
rows = [x.values() for x in clients]
print(tabulate(rows, header))
return clients
# Create server
server = SimpleXMLRPCServer((LISTEN_HOST, LISTEN_PORT))
# Register function to RPC
server.register_function(download, 'download')
server.register_function(upload, 'upload')
server.register_function(show_all_files, 'show_all_files')
server.register_function(login, 'login')
server.register_function(clients_activity, 'clients_activity')
def loop():
#exception handling saat terminate menggunkan ctrl+c
try:
while True:
server.serve_forever()
except KeyboardInterrupt:
print("Program dihentikan..")
pass
print(f"Listen on {LISTEN_HOST}:{LISTEN_PORT}")
loop()