-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
114 lines (81 loc) · 2.86 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
# Importing modules
import socket
import threading
import tkinter as tk
from tkinter import ttk
import sv_ttk
class menu:
def __init__(self, master):
# server details
self.HOST = "185.215.180.30"
self.PORT = 1234
# ttk theme
sv_ttk.set_theme("dark")
# menu gui
self.master = master
self.master.title("Python Chat App")
self.master.geometry("500x500")
self.master.config(bg="Black")
self.master.resizable(True, True)
# Home menu GUI
def home_menu(self):
self.frame = ttk.Frame(self.master)
#self.home_menu.place(x=0, y=0, width=500, height=500)
# grid setup
self.frame.columnconfigure(0, weight=1)
self.frame.rowconfigure(0, weight=1)
self.frame.rowconfigure(1, weight=1)
self.frame.rowconfigure(2, weight=1)
self.frame.rowconfigure(3, weight=1)
self.frame.rowconfigure(4, weight=1)
self.frame.rowconfigure(5, weight=1)
# login button
login_button = ttk.Button(self.frame, text="Login", command=self.main)
login_button.grid(column=0, row=0, padx=0, pady=0)
# register button
register_button = ttk.Button(self.frame, text="Register")
register_button.grid(column=0, row=1, padx=0, pady=0)
#self.frame.place(x=0,y=0)
self.frame.pack(fill="both", expand=1)
# Listens for messages from the server
def listening(self):
while True:
message = self.client.recv(2048).decode("utf-8")
if message != "":
username = message.split("~")[0]
contents = message.split("~")[1]
print(f"{username}: {contents}")
else:
print("Message is empty")
def sending(self):
while True:
print("Type your message: ")
message = input("")
if message != "":
self.client.sendall(message.encode("utf-8"))
else:
print("Message empty")
def chat_to_server(self):
username = input("Username: ")
if username != "":
self.client.sendall(username.encode("utf-8"))
else:
print("Username empty")
self.chat_to_server()
thread = threading.Thread(target=self.listening)
thread.start()
self.sending()
def main(self):
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self.client.connect((self.HOST, self.PORT))
print(f"Connected to {self.HOST}:{self.PORT}")
except:
print(f"Unable to connect to {self.HOST}:{self.PORT}")
exit()
self.chat_to_server()
if __name__ == "__main__":
root = tk.Tk()
R = menu(root)
R.home_menu()
root.mainloop()