-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
72 lines (67 loc) · 3.81 KB
/
server.py
File metadata and controls
72 lines (67 loc) · 3.81 KB
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
import socket
import threading
import time
# Connection Data
print("""
▄████▄ ██░ ██ ▄▄▄ ██▀███ ▒█████ ███▄ █ ██████ ▓█████ ██▀███ ██▒ █▓ ▓█████ ██▀███
▒██▀ ▀█ ▒▓██░ ██ ▒████▄ ▓██ ▒ ██▒▒██▒ ██▒ ██ ▀█ █ ▒██ ▒ ▓█ ▀▓██ ▒ ██▒▓██░ █▒ ▓█ ▀▓██ ▒ ██▒
▒▓█ ▄░▒██▀▀██ ▒██ ▀█▄ ▓██ ░▄█ ▒▒██░ ██▒▓██ ▀█ ██▒ ░ ▓██▄ ▒███ ▓██ ░▄█ ▒ ▓██ █▒░ ▒███ ▓██ ░▄█ ▒
▒▓▓▄ ▄██ ░▓█ ░██ ░██▄▄▄▄██▒██▀▀█▄ ▒██ ██░▓██▒ ▐▌██▒ ▒ ██▒ ▒▓█ ▄▒██▀▀█▄ ▒██ █░░ ▒▓█ ▄▒██▀▀█▄
▒ ▓███▀ ░▓█▒░██▓▒▓█ ▓██░██▓ ▒██▒░ ████▓▒░▒██░ ▓██░ ▒██████▒▒▒░▒████░██▓ ▒██▒ ▒▀█░ ▒░▒████░██▓ ▒██▒
░ ░▒ ▒ ▒ ░░▒░▒░▒▒ ▓▒█░ ▒▓ ░▒▓░░ ▒░▒░▒░ ░ ▒░ ▒ ▒ ▒ ▒▓▒ ▒ ░░░░ ▒░ ░ ▒▓ ░▒▓░ ░ ▐░ ░░░ ▒░ ░ ▒▓ ░▒▓░
░ ▒ ▒ ░▒░ ░░ ░ ▒▒ ░▒ ░ ▒ ░ ▒ ▒░ ░ ░░ ░ ▒░ ░ ░▒ ░ ░░ ░ ░ ░▒ ░ ▒ ░ ░░ ░ ░ ░ ░▒ ░ ▒
░ ░ ░░ ░ ░ ▒ ░░ ░ ░ ░ ░ ▒ ░ ░ ░ ░ ░ ░ ░ ░░ ░ ░ ░ ░░ ░
░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░
made by Perchant76
""")
host = '127.0.0.1'
port = 55555
textfile = open("log.txt", "w")
# Starting Server
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen()
# Lists For Clients and Their Nicknames
clients = []
nicknames = []
# Sending Messages To All Connected Clients
def broadcast(message):
for client in clients:
client.send(message)
# Handling Messages From Clients
def handle(client):
while True:
try:
# Broadcasting Messages
message = client.recv(1024)
broadcast(message)
except:
# Removing And Closing Clients
index = clients.index(client)
clients.remove(client)
client.close()
nickname = nicknames[index]
broadcast('{} left!'.format(nickname).encode('ascii'))
nicknames.remove(nickname)
break
# Receiving / Listening Function
def receive():
while True:
# Accept Connection
client, address = server.accept()
print("Connected with {}".format(str(address)))
# Request And Store Nickname
client.send('NICK'.encode('ascii'))
nickname = client.recv(1024).decode('ascii')
nicknames.append(nickname)
clients.append(client)
# Print And Broadcast Nickname
print("Nickname is {}".format(nickname))
broadcast(f"{nickname} joined!".encode('ascii'))
client.send('Connected to server!'.encode('ascii'))
# Start Handling Thread For Client
thread = threading.Thread(target=handle, args=(client,))
thread.start()
print("server is running...")
receive()
time.sleep(10)