-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
63 lines (57 loc) · 2.38 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
import socket
import threading
print("---------------------------------Client is running----------------------------")
nickname = input("Enter\'s Your Nickname : ")
if nickname == 'admin' :
password = input('Enter the Password: ')
client = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
client.connect(('127.0.0.1', 55555))
thread_stop= False
def receive():
while True:
global thread_stop
if thread_stop:
break
try:
message = client.recv(1024).decode('ascii')
if message == 'NICK':
client.send(nickname.encode('ascii'))
message2=client.recv(1024).decode('ascii')
if message2 == 'pass':
client.send(password.encode('ascii'))
if client.recv(1024).decode('ascii') == 'Incorrect':
print("Your password is incorrect ,connection broke!")
thread_stop= True
elif message2=='BAN':
print('You are banned , can\'t connect to server')
client.close()
thread_stop=True
else:
print(message)
except:
print("Server is closed or there is an error occured!")
client.close()
break
def write():
try:
while True:
if thread_stop:
break
message = f'{nickname}: {input("")}'
if message[len(nickname)+2:].startswith('$'):
if nickname == 'admin' :
if message[len(nickname)+2:].startswith('$kick'):
client.send(f'KICK {message[len(nickname) +2 + 6:]}'.encode('ascii'))
elif message[len(nickname)+2:].startswith('$ban'):
client.send(f'BAN {message[len(nickname) +2 + 5:]}'.encode('ascii'))
else:
print('You have no powers to perform any commands!')
else:
client.send(message.encode('ascii'))
except:
print("Can't write you are not longer connected to the server, try relaunching the client!")
rcv_thread = threading.Thread(target=receive)
rcv_thread.start()
send_thread = threading.Thread(target=write)
send_thread.start()