import socket from pathlib import Path # ----- KNOWN CONNECTION FILE READ/WRITE/LIST/CLEAR PACKAGE ----- # # Read Specific Line Package def read_specific_line(filename, line_number): # index (0 is first) with open(filename, 'r') as file: for index, line in enumerate(file): if index == line_number: current_line = line.split() # Splits and removes nick return current_line[1] return None def known_connection_file(mode, nick, ip_addr, no_of_ip_chosen): # use normal numbers e.g. 1 for first for no_of_ip_chosen file_path = Path("known_connections.txt") if not file_path.exists(): print(f"Known connections file not found. Creating...", end = " ") # Create if not exist with open(file_path, 'w') as f: f.write("") print(f"Done.") if file_path.exists(): # If file exists: # ----- READ MODE ----- # if mode == "read": # Read out all the lines with open('known_connections.txt', 'r') as f: for line in f: print(f"{line}", end = "") # ----- SELECT MODE ----- # elif mode == "select": # Find the "no_of_ip_chosen"th line and return the ip address select_result = read_specific_line("known_connections.txt", no_of_ip_chosen - 1) if select_result == None: print("Error: Line does not exist") else: return select_result # ----- WRITE MODE ----- # elif mode == "write": with open('known_connections.txt', 'a') as f: f.writelines(f"{nick}: {ip_addr}\n") print("Writing completed.") # ----- CLEAR MODE ----- # elif mode == "clear": with open('known_connections.txt', 'w') as f: f.writelines("") print("File cleared.") else: print("Error: Invalid mode.") # ---------- PORT SELECTION ---------- # def port_selection(): print("=" * 80) print("CLRadio Sender Version A-1 | by P7MJ") print("-" * 80) print("[1] Use/Edit saved ports") print("[2] Specify port") user_port_choice = input(" > ") # ---------- Use or edit saved ports ---------- # if user_port_choice == "1": print("Current Saved Profiles:") known_connection_file("read", None, None, None) print("\n[1] Write new profile entry") print("[2] Select a profile") print("[3] Clear all profiles (IRREVERSIBLE, WILL ANNIHILATE MASSDEBATER67'S BRAIN)") profile_user_choice = input(" > ") # ----- Write new profile entry ----- # if profile_user_choice == "1": profile_nick = input("Profile Nick > ") profile_ip = input("Profile IP > ") known_connection_file("write", profile_nick, profile_ip, None) return 0 # ----- Select a profile by number ----- # elif profile_user_choice == "2": selection = input("Select by number > ") if selection.isdigit(): return known_connection_file("select", None, None, int(selection)) # ----- Clear profiles ------ # elif profile_user_choice == "3": known_connection_file("clear", None, None, None) return 0 # ---------- elif user_port_choice == "2": custom_port = input("Insert custom port > ") return custom_port else: print("Error: You need mental help") def run_client(): while True: while True: port_selection_outcome = port_selection() if port_selection_outcome == 0: pass else: break # REPLACE WITH YOUR SERVER'S LAN IP ADDRESS host = port_selection_outcome port = 443 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, port)) print(f"Server initiated. Sending to: {host}. Enter \"exit\" to exit.") message = input(" > ") while message != 'exit': s.send(message.encode()) # Optional: receive a response # data = s.recv(1024).decode() # print(f"Received from server: {data}") message = input(" > ") s.close() if __name__ == '__main__': run_client()