-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
90 lines (63 loc) · 2.1 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
import socket
import threading
ip = input("[*] Enter IP of server: ")
port = int(input("[*] Enter port of server: "))
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((ip, port))
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)
print("[*] Connected!")
except:
print("[!] Unable to get Hostname and IP")
def handle_rece_msg(client: socket.socket):
while True:
try:
msg_rece = client.recv(4505)
if msg_rece:
print("[Server]: ",msg_rece.decode())
else:
client.close()
break
except Exception as e:
print(f'[!] Error handling message from server: {e}')
client.close()
break
def handle_sent_msg(client: socket.socket):
while True:
try:
msg_send = input()
if msg_send:
client.send(msg_send.encode())
else:
client.close()
break
except Exception as e:
print(f'[!] Error handling message from server: {e}')
client.close()
break
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 as e:
print(f'[!] Error handling message from server: 2{e}')
client.close()
break