-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathclient.py
111 lines (96 loc) · 3.61 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
import socket
import threading
import json
import os
def enter_server():
os.system('cls||clear')
# Enter servers.json to print the names of the servers
with open('servers.json') as f:
data = json.load(f)
print('Your servers: ', end="")
# Print the servers that are stored in the servers.json file
for servers in data:
print(servers, end=" ")
# Ask user for the name of the server to join
server_name = input("\nEnter the server name:")
global nickname
global password
nickname = input("Choose Your Nickname:")
if nickname == 'admin':
password = input("Enter Password for Admin:")
# Store the ip and port number for connection
ip = data[server_name]["ip"]
port = data[server_name]["port"]
global client
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to a host
client.connect((ip, port))
def add_server():
os.system('cls||clear')
server_name = input("Enter a name for the server:")
server_ip = input("Enter the ip address of the server:")
server_port = int(input("Enter the port number of the server:"))
with open('servers.json', 'r') as f:
data = json.load(f)
# Store the info of the new server in servers.json
with open('servers.json', 'w') as f:
data[server_name] = {"ip": server_ip, "port": server_port}
json.dump(data, f, indent=4)
# Menu loop, it will loop until the user choose to enter a server
while True:
os.system('cls||clear')
option = input("(1)Enter server\n(2)Add server\n")
if option == '1':
enter_server()
break
elif option == '2':
add_server()
stop_thread = False
def receive():
while True:
global stop_thread
if stop_thread:
break
try:
message = client.recv(1024).decode('ascii')
if message == 'NICK':
client.send(nickname.encode('ascii'))
next_message = client.recv(1024).decode('ascii')
if next_message == 'PASS':
client.send(password.encode('ascii'))
if client.recv(1024).decode('ascii') == 'REFUSE':
print("Connection is Refused !! Wrong Password")
stop_thread = True
# Clients those are banned can't reconnect
elif next_message == 'BAN':
print('Connection Refused due to Ban')
client.close()
stop_thread = True
else:
print(message)
except socket.error:
print('Error Occured while Connecting')
client.close()
break
def write():
while True:
if stop_thread:
break
# Getting Messages
message = f'{nickname}: {input("")}'
if message[len(nickname) + 2:].startswith('/'):
if nickname == 'admin':
if message[len(nickname) + 2:].startswith('/kick'):
# 2 for : and whitespace and 6 for /KICK_
client.send(f'KICK {message[len(nickname) + 2 + 6:]}'.encode('ascii'))
elif message[len(nickname) + 2:].startswith('/ban'):
# 2 for : and whitespace and 5 for /BAN
client.send(f'BAN {message[len(nickname) + 2 + 5:]}'.encode('ascii'))
else:
print("Commands can be executed by Admins only !!")
else:
client.send(message.encode('ascii'))
receive_thread = threading.Thread(target=receive)
receive_thread.start()
write_thread = threading.Thread(target=write)
write_thread.start()