-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
122 lines (101 loc) · 3.19 KB
/
client.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import socket
import time
import ssl
from tkinter import *
from tkinter.scrolledtext import *
from threading import Thread
class Chat(Frame):
def addMessage(self, text):
self.messages.config(state=NORMAL)
self.messages.insert(END, text)
self.messages.config(state=DISABLED)
self.messages.see('end')
def sendMessageEvent(self, event):
self.sendMessage()
def sendMessage(self):
global s
text = self.entryText.get()
if len(text) > 0:
if text[0] == "/":
s.send(b'C=' + bytes(text[1:].encode('utf-8')))
else:
s.send(b'M=' + bytes(text.encode('utf-8')))
self.entryText.set('')
def createWidgets(self):
self.messages = ScrolledText(self, height=15, width=30)
self.messages.config(state=DISABLED)
self.messages.pack({'side':'right'})
self.entryText = StringVar()
self.INP = Entry(self, textvariable = self.entryText)
self.INP.pack()
self.INP.bind('<Return>', self.sendMessageEvent)
self.SEND = Button(self)
self.SEND['text'] = 'SEND'
self.SEND['fg'] = 'green'
self.SEND['command'] = self.sendMessage
self.SEND.pack({'side':'left'})
self.QUIT = Button(self)
self.QUIT['text'] = 'QUIT'
self.QUIT['fg'] = 'red'
self.QUIT['command'] = self.quit
self.QUIT.pack({'side':'right'})
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
class Connect(Frame):
def createWidgets(self):
self.IPFrame = Frame(self)
self.IPFrame.pack()
self.PortFrame = Frame(self)
self.PortFrame.pack()
self.ButtonFrame = Frame(self)
self.ButtonFrame.pack()
self.ipLabel = Label(self.IPFrame)
self.ipLabel['text'] = "IP: "
self.ip = StringVar()
self.ipEntry = Entry(self.IPFrame, var = self.ip)
name = input("Name: ")
host = input("Host: ")
port = int(input("Port (default 25565): ") or 25565)
s = ssl.wrap_socket(socket.socket(), ciphers='SHA1')
s.settimeout(3)
try:
s.connect((host, port))
except:
print("Could not connect to server.")
connectionAlive = True
s.send(bytes(name.encode('utf-8'))) # send name
timeFormat = '{:%Y-%m-%d %H:%M:%S}'
def receive():
global s, connectionAlive, chat, tk
print(connectionAlive)
while connectionAlive:
try:
new = s.recv(1024).decode('utf-8') # received message
if new == '?=HB': # if heartbeat message:
time.sleep(0.2)
s.send(b'?=HB')
elif new == '?=QUIT':
connectionAlive = False
s.close()
elif new[:2] == 'M=':
chat.addMessage(new[2:] + "\n")
except:
connectionAlive = False
s.close()
tk.destroy()
print("Connection closed")
receiverThread = Thread(target=receive)
receiverThread.daemon = True
receiverThread.start()
tk = Tk(screenName='Chat')
tk.geometry('400x300')
chat = Chat(master=tk)
chat.mainloop()
try:
tk.destroy()
except: pass
connectionAlive = False
s.close()
input("Press enter to close.")