import socket import threading from Library import Library from Song import Song import os SONGS_DIR = os.path.join(os.path.dirname(__file__), 'songs') library = Library() library.initialize(SONGS_DIR) def handle_client(client_socket): request = client_socket.recv(1024).decode('utf-8') print(f"Received: {request}") if request == 'getSongList': client_socket.send(library.get_song_list().encode('utf-8')) elif request.startswith('getSong:'): song_id = int(request.split(':')[1]) song: Song = library.get_song_by_id(song_id) print(f"Sending song: {song.name} by {song.author} to {client_socket.getpeername()}") client_socket.send(song.get_data()) client_socket.close() def start_server(): server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind(("0.0.0.0", 9999)) server.listen(5) print("Listening on port 9999") while True: client, addr = server.accept() print(f"Accepted connection from: {addr[0]}:{addr[1]}") client_handler = threading.Thread(target=handle_client, args=(client,)) client_handler.start() start_server()