tools.py Linked function:
from colorama import Fore, Style
def Linked(url, text=None):
if text is None:
text = url
print(f"{Fore.BLUE}{Style.BRIGHT}{text}{Style.RESET_ALL} ({Fore.BLUE}{url}{Style.RESET_ALL})")
Part of my client side code, that is responsible for making links:
import tools
import threading
import socket
from colorama import Fore, Style
def receive_messages(client_socket):
print(client_socket.recv(1024).decode())
while True:
try:
data = str(client_socket.recv(1024).decode())
if data.startswith("$"):
try:
action = data.replace("$","").lower()
if action.startswith("link:"):
content = action.replace("link:","")
info = content.split("~", 1)
print(f"Link Text: {info[0]}, URL: {info[1]}")
tools.Linked(info[0], info[1])
except Exception as e:
print(f"Error formatting received message, because {str(e)}")
else:
print(data)
except:
break
receive_thread = threading.Thread(target=receive_messages, args=(client,))
receive_thread.start()
I got a problem, when it checks if the message is starting with $link:
, if it is, then it formats the text as a link.
It gets to the point where it check if message starts with $, else it prints raw message. So I think it doesn't detect $ at the start because it prints just text without links.
Thank you in advance.
I tried adding try-except blocks, error handling, printing out some info, but there was nothing useful. I expect making a blue clickable link with custom text.
I found out the solution:
data = str(client_socket.recv(1024).decode())
if "$link:" in data:
try:
index = data.find("$link:")
data = data[index:]
content = data.replace("$link:", "")
info = content.split("~")
tools.Linked(info[0], info[1])
except Exception as e:
print(f"Error formatting received message, because {str(e)}")
I think the issue was in all that startswith and indexes stuff, so I made the if statement way shorter. Now it seems to work