-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
65 lines (50 loc) · 1.97 KB
/
server.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
#https://github.com/samuhay/Python-Chat-App/tree/master
from socket import AF_INET, socket, SOCK_STREAM
from threading import Thread
def accept_incoming_connections():
"""Gelen bağlantıları halleder."""
while True:
client, client_address = SERVER.accept()
print("%s:%s bağlandı." % client_address)
client.send(bytes("Server bağlantısı başarılı", "utf8")) # Kullanıcı adı için mesaj
addresses[client] = client_address
Thread(target=handle_client, args=(client,)).start()
def handle_client(client): # Bağlantı soketini bekliyor.
"""Tekil bağlantı kısımı."""
name = client.recv(BUFSIZ).decode("utf8")
x=name.split(":")
real_name=x[1]
welcome = 'Hoşgeldin %s! Eğer çıkmak isterseniz, {quit} yazınız!' % real_name
client.send(bytes(welcome, "utf8"))
msg = "%s sohbete katıldı!" % real_name
broadcast(bytes(msg, "utf8"))
clients[client] = real_name
while True:
msg = client.recv(BUFSIZ)
if msg != bytes("{quit}", "utf8"):
broadcast(msg, real_name + ": ")
else:
client.send(bytes("{quit}", "utf8"))
client.close()
del clients[client]
broadcast(bytes("%s sohbetten ayrıldı." % real_name, "utf8"))
break
def broadcast(msg, prefix=""): # prefix kullanıcı adını için.
"""tüm bağlantılar için genel mesaj yayınlar."""
for sock in clients:
sock.send(bytes(prefix, "utf8") + msg)
clients = {}
addresses = {}
HOST = '192.168.1.79' # Server ip adresi
PORT = 1234 # Server Portu
BUFSIZ = 1024 # Buffer belleği
ADDR = (HOST, PORT)
SERVER = socket(AF_INET, SOCK_STREAM)
SERVER.bind(ADDR)
if __name__ == "__main__":
SERVER.listen(5)
print("Bağlantı bekleniyor...")
ACCEPT_THREAD = Thread(target=accept_incoming_connections)
ACCEPT_THREAD.start()
ACCEPT_THREAD.join()
SERVER.close()