assuming I want to make a program to send a string (message) from one device to another using python, pretty much a messaging app, how would I do that? Right now I'm using a one-drive .txt file to store the message, but not only is it slow, it also needs a manual refresh. Is there any way I can use web-socket to do this?
Current code for the one-drive messaging program:
from time import sleep
import socket
import tkinter as tk
win = tk.Tk()
sending = False
def refresh():
readMessages()
def clear():
for widget in win.winfo_children():
widget.destroy()
def readMessages():
sending = False
clear()
with open("messageLog.txt", "r") as mLog:
messages = mLog.readlines()
if len(messages) > 0:
for message in messages:
tk.Label(win, text=message).pack()
else:
tk.Label(win, text="No messages!").pack()
button = tk.Button(win, text="SEND MESSAGE", command=messageInit)
button2 = tk.Button(win, text="REFRESH", command=refresh)
button.pack()
button2.pack()
sleep(1)
def messageInit():
sending = True
clear()
tk.Label(win, text="Message:").grid(row=0)
entry = tk.Entry(win)
entry.grid(row=0, column=1)
doneButton = tk.Button(win, text="DONE", command=lambda: sendMessage(entry)).grid(row=1)
def sendMessage(entry):
hostname = socket.gethostname()
with open("messageLog.txt", "r+") as mLog:
messages = mLog.readlines()
messages.append(hostname + ": " + entry.get() + "\n")
messages = "".join(messages)
mLog.truncate(0)
mLog.seek(0)
mLog.write(messages)
readMessages()
refresh()
win.mainloop()