-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsever.py
93 lines (66 loc) · 2.15 KB
/
sever.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
from tkinter import *
import socket
import threading
from datetime import datetime
current_time = datetime.now().strftime("%I:%M %p")
combined_string = f'server started at {current_time}\n'
windows = Tk()
windows.title("YEGNA-Chat")
PORT = 2104
# socket.gethostbyname(socket.gethostname()) # '127.0.0.1' #192.168.50.147
HOST = '192.168.94.179'
print(HOST)
disMessage = '!disconnect'
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORT))
clients_online = []
windows.minsize(width=300, height=400)
labelONe = Label(windows, text='CHAT SEAMLESSLY !!!', font=(
'times new roman', 11, 'bold'), fg='grey').pack(side='top')
label1 = Label(windows, text='', font=('times new roman', 11), fg='grey')
label1.pack(side='top')
def start():
server.listen()
label1['text'] += f"LISTENING ON {HOST}"
while True:
connection, address = server.accept()
label1['text'] += f"connected to {address}"
thread = threading.Thread(target=handleClient, args=(connection,))
thread.start()
def send_to_client(client, message):
client.send(message.encode('utf-8'))
def send_to_all(message):
for user in clients_online:
try:
send_to_client(user[1], message)
except:
continue
def accept_message(client, userName):
while True:
try:
msg = client.recv(1024).decode('utf-8')
except:
message = f'{userName} left the chat'
send_to_all(message)
break
else:
message = f'{userName}: {msg}'
send_to_all(message)
def handleClient(con):
send_to_client(con, combined_string)
connected = True
while connected:
userName = con.recv(1024).decode('utf-8')
if userName:
clients_online.append([userName, con])
note = f'{userName} joined chat'
send_to_all(note)
break
else:
label1['text'] = 'empty User Name'
threading.Thread(target=accept_message, args=(con, userName)).start()
def start_server():
thread = threading.Thread(target=start)
thread.start()
start_server()
windows.mainloop()