-
Notifications
You must be signed in to change notification settings - Fork 0
/
servertk.py
executable file
·74 lines (63 loc) · 1.74 KB
/
servertk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python3.5
import tkinter
import threading
import socket
root = tkinter.Tk()
root.maxsize(700,700)
root.title("Messenger-Server")
s = socket.socket()
host = socket.gethostname()
port = 8080
s.bind((host,port))
send_msg = tkinter.StringVar()
def start_server():
label1=tkinter.Label(root,text="Starting server on : %s"%host)
label1.pack()
label2=tkinter.Label(root,text="Waiting for someine to join...")
label2.pack()
label3=tkinter.Label(root,text="Click accept and then enter hostname on client device")
label3.pack()
accept_btn = tkinter.Button(root,text='Accept',command=accept)
accept_btn.pack()
def accept():
global conn
global w
global send_box
s.listen(1)
conn,addr=s.accept()
addr_new = str(addr)
label4=tkinter.Label(root,text="%s has now connected and is now online."%addr_new)
label4.pack()
w = tkinter.Text(root)
w.pack()
bottomframe = tkinter.Frame(root,width=300,height=50)
bottomframe.pack()
send_box = tkinter.Entry(bottomframe,textvariable=send_msg)
send_box.pack()
send_btn = tkinter.Button(bottomframe,text='Send',command = send_func)
send_btn.pack()
x = threading.Thread(target = send_func)
y = threading.Thread(target = receieve)
x.start()
y.start()
def send_func():
send_msg = send_box.get()
send_msg_new = str(send_msg)
send(send_msg_new)
w.insert(0.0,'You : %s \n' % send_msg_new )
send_box.delete(0,tkinter.END)
def receieve():
m = 0
while m!=1:
imessage = conn.recv(1024)
imessage = imessage.decode()
print(imessage)
print("\n")
w.insert(0.0,'Other : %s \n' % imessage )
def send(msg):
message = msg
message = message.encode()
conn.send(message)
start_btn = tkinter.Button(root,text='Start Server',command=start_server)
start_btn.pack()
root.mainloop()