-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_without_encryption.py
157 lines (91 loc) · 4.04 KB
/
client_without_encryption.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import socket
import threading
import sys
nickname = input("[*] Enter your nickname: ")
if nickname.lower() == "admin":
password = input("[*] Enter admin password: ")
server_ip = input("[*] Enter IP address of the server: ")
server_port = int(input("[*] Enter port of the server: "))
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
client.connect((server_ip, server_port))
except Exception as e:
print(f"[!] Unable to connecet to the server: {e}")
#-----------------------------------------------------------------------
def get_Host_name_IP():
try:
# Importing socket library
# Function to display hostname and
# IP address "Support windows and linux"
host_name = socket.gethostname()
x = socket.gethostbyname(host_name)
x = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
x.connect(('10.0.0.0', 0))
host_ip = x.getsockname()[0]
print("[-] Hostname:", host_name)
print("[-] IP:", host_ip)
except Exception or KeyboardInterrupt as e:
print(f"[!] Unable to get Hostname and IP: {e}")
close_connection()
#-----------------------------------------------------------------------
def handle_rece_msg(client: socket.socket):
while True:
try:
message = client.recv(1024).decode("ascii")
if message == "NICK":
client.send(nickname.encode("ascii"))
admin_message = client.recv(1024).decode("ascii")
if admin_message == 'PASS':
client.send(password.encode("ascii"))
if client.recv(1024).decode("ascii") == "REFUSE":
print("[!] Connection Refused!: Wrong Password")
close_connection()
elif message == f"[!] Admin removed {nickname}":
close_connection()
break
else:
print(message)
except Exception or KeyboardInterrupt as e:
print("[!] Error in handle_rece_msg: function")
print(f"[!] Error handling message from server: {e}")
close_connection()
#-----------------------------------------------------------------------
def handle_sent_msg(client: socket.socket):
while True:
try:
x = input("")
message = f'{nickname}: {x}'
if x.lower() == "bye":
client.send(message.encode("ascii"))
client.close()
sys.exit("Bye")
else:
client.send(message.encode("ascii"))
except Exception or KeyboardInterrupt as e:
print("[!] Error in handle_sent_msg: function")
print(f"[!] Error handling message from server: {e}")
close_connection()
#-----------------------------------------------------------------------
def close_connection():
msg = f'{nickname}: bye'
client.send(msg.encode("ascii"))
client.close()
sys.exit("Bye")
#-----------------------------------------------------------------------
def client_thread():
send = threading.Thread(target=handle_sent_msg, args=(client,))
rece = threading.Thread(target=handle_rece_msg, args=(client,))
send.start()
rece.start()
send.join()
rece.join()
#-----------------------------------------------------------------------
while True:
try:
get_Host_name_IP()
print("[$] Chat started")
client_thread()
except Exception or KeyboardInterrupt as e:
print("[!] Error while trying to connect to the server")
print(f"[!] Error handling message from server: {e}")
close_connection()