-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
219 lines (190 loc) · 6.81 KB
/
Copy pathserver.py
File metadata and controls
219 lines (190 loc) · 6.81 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import socket
import threading
import sqlite3
import os
import configparser
import logging
# Import shared protocol helpers
from protocol import (
read_line, send_line, recv_int, send_int, recv_str, send_str,
recv_exact, normalize_filename, calculate_checksum
)
# ------------------------
# Logging configuration
# ------------------------
logging.basicConfig(
level=logging.DEBUG,
filename='server.log',
filemode='w',
format='%(asctime)s - %(levelname)s - %(message)s'
)
# ------------------------
# Database
# ------------------------
thread_local = threading.local()
def get_db_connection(db_path):
"""
Return a thread-local SQLite connection.
"""
if not hasattr(thread_local, 'conn'):
thread_local.conn = sqlite3.connect(db_path)
return thread_local.conn
def init_db(db_path):
"""
Create the files table if it doesn't exist.
"""
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS files (
id INTEGER PRIMARY KEY AUTOINCREMENT,
file_name TEXT UNIQUE,
file_size INTEGER,
upload_date DATETIME DEFAULT CURRENT_TIMESTAMP,
checksum TEXT
)
''')
conn.commit()
conn.close()
# ------------------------
# Handlers
# ------------------------
def handle_upload(sock, directory):
conn = get_db_connection(os.path.join(directory, "files.db"))
try:
# 1) Receive the file name
file_name = normalize_filename(recv_str(sock))
logging.info(f"UPLOAD request for file: {file_name}")
cursor = conn.cursor()
cursor.execute("SELECT file_name FROM files WHERE file_name = ?", (file_name,))
if cursor.fetchone():
send_line(sock, "FILE_EXISTS")
logging.info(f"File '{file_name}' already exists in database.")
return
else:
send_line(sock, "PROCEED")
# 2) Receive file size + checksum
file_size = recv_int(sock)
checksum = recv_str(sock)
# 3) Receive the file data
file_path = os.path.join(directory, file_name)
logging.info(f"Saving uploaded file to: {file_path}")
with open(file_path, 'wb') as f:
received = 0
while received < file_size:
chunk = sock.recv(min(4096, file_size - received))
if not chunk:
raise ConnectionError("Client disconnected during upload.")
f.write(chunk)
received += len(chunk)
# 4) Verify checksum
if calculate_checksum(file_path) == checksum:
cursor.execute("INSERT INTO files (file_name, file_size, checksum) VALUES (?, ?, ?)",
(file_name, file_size, checksum))
conn.commit()
send_line(sock, "SUCCESS")
logging.info(f"File '{file_name}' uploaded successfully.")
else:
os.remove(file_path)
send_line(sock, "FAILURE")
logging.error(f"Checksum mismatch for file '{file_name}'.")
except Exception as e:
logging.error(f"Upload error: {e}")
send_line(sock, "FAILURE")
def handle_download(sock, directory):
"""
The client sends a 'base_name' (no extension).
We do a partial match in the DB (base_name%).
If exactly one file matches, we send back:
1) FILE_FOUND
2) The actual full filename
3) The file size
4) The checksum
5) The file data
Then we wait for "SUCCESS" or "FAILURE".
"""
conn = get_db_connection(os.path.join(directory, "files.db"))
try:
base_name = normalize_filename(recv_str(sock))
logging.info(f"DOWNLOAD request for base name: {base_name}")
cursor = conn.cursor()
cursor.execute("""
SELECT file_name, file_size, checksum
FROM files
WHERE file_name LIKE ?
""", (base_name + '%',))
results = cursor.fetchall()
if len(results) == 0:
send_line(sock, "FILE_NOT_FOUND")
logging.info(f"No file found matching '{base_name}'.")
return
elif len(results) > 1:
send_line(sock, "MULTIPLE_FILES")
logging.info(f"Multiple files found matching '{base_name}'.")
return
actual_file_name, file_size, checksum = results[0]
send_line(sock, "FILE_FOUND")
# Send the actual file name so the client can keep extension
send_str(sock, actual_file_name)
send_int(sock, file_size)
send_str(sock, checksum)
file_path = os.path.join(directory, actual_file_name)
logging.info(f"Sending file '{actual_file_name}' from: {file_path}")
with open(file_path, 'rb') as f:
sent = 0
while sent < file_size:
data = f.read(min(4096, file_size - sent))
if not data:
break
sock.sendall(data)
sent += len(data)
# Wait for the client's "SUCCESS" or "FAILURE"
final_resp = read_line(sock)
if final_resp != "SUCCESS":
logging.error(f"Client reported download failure for '{actual_file_name}'.")
except Exception as e:
logging.error(f"Download error: {e}")
send_line(sock, "FILE_NOT_FOUND")
def handle_client(sock, directory):
"""
Reads the initial command line: "UPLOAD" or "DOWNLOAD",
then calls the appropriate handler.
"""
try:
command = read_line(sock)
logging.info(f"Received command: {command}")
if command == "UPLOAD":
handle_upload(sock, directory)
elif command == "DOWNLOAD":
handle_download(sock, directory)
else:
logging.error(f"Invalid command received: '{command}'")
send_line(sock, "INVALID_COMMAND")
except Exception as e:
logging.error(f"Error handling client: {e}")
finally:
sock.close()
# ------------------------
# Main
# ------------------------
def main():
config = configparser.ConfigParser()
config.read('config.ini')
port = config.getint('server', 'port', fallback=5000)
directory = config.get('server', 'directory', fallback='./files')
if not os.path.exists(directory):
os.makedirs(directory)
db_path = os.path.join(directory, "files.db")
init_db(db_path)
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('0.0.0.0', port))
server_socket.listen(5)
print(f"Server listening on port {port}")
logging.info(f"Server listening on port {port}")
while True:
client_sock, addr = server_socket.accept()
logging.info(f"Accepted connection from {addr}")
t = threading.Thread(target=handle_client, args=(client_sock, directory), daemon=True)
t.start()
if __name__ == "__main__":
main()