I have an HTTP server in one program (server.py), and the main application in another (client.py). The essence of the program is that users exchange data (Or on a different chat). server.py:
import socket, time host = socket.gethostbyname(socket.gethostname()) 192.168.0.101 port = 9090 clients = [] s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # TCP socket + IP socket s.bind((host,port)) quit= False print("[ Server Started ]") while not quit: try: data, addr = s.recvfrom(1024) if addr not in clients: clients.append(addr) itsatime = time.strftime("%Y-%m-%d-%H.%M.%S", time.localtime()) print("["+addr[0]+"]=["+str(addr[1])+"]=["+itsatime+"]/",end="") print(data.decode("utf-8")) for client in clients: if addr != client: s.sendto(data,client) except: print("\n[ Server Stopped ]") quit = True s.close() client.py:
import socket, threading, time key = 8194 shutdown = False join = False def receving (name, sock): while not shutdown: try: while True: data, addr = sock.recvfrom(1024) #print(data.decode("utf-8")) # Begin decrypt = ""; k = False for i in data.decode("utf-8"): if i == ":": k = True decrypt += i elif k == False or i == " ": decrypt += i else: decrypt += chr(ord(i)^key) print(decrypt) # End time.sleep(0.2) except: pass host = socket.gethostbyname(socket.gethostname()) port = 0 server = ("192.168.0.101",9090) s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) s.bind((host,port)) s.setblocking(0) # не вийде з чату alias = input("Name: ") # вводимо імя користувача # багатопоточність rT = threading.Thread(target = receving, args = ("RecvThread",s)) rT.start() while shutdown == False: if join == False: s.sendto(("["+alias + "] => join chat ").encode("utf-8"),server) join = True else: try: message = input() # Begin crypt = "" for i in message: crypt += chr(ord(i)^key) message = crypt # End if message != "": s.sendto(("["+alias + "] :: "+message).encode("utf-8"),server) time.sleep(0.2) except: s.sendto(("["+alias + "] <= left chat ").encode("utf-8"),server) shutdown = True rT.join() s.close()