-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
97 lines (75 loc) · 2.51 KB
/
server.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
import socket
import threading
ip_server = ""
port = 12000
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((ip_server, port))
server.listen(5)
if len(ip_server) >= 1:
print(f"[*] Server running!, listening on, {ip_server}:{port}")
else:
ip_server = socket.gethostbyname(socket.gethostname())
print(f"[*] Server running!, listening on", ip_server, ":", port)
# Function to print out Hostname and IP
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) #Share this IP with the client
print("[*] Port:", port) #Share this port with the client
except:
print("[*] Unable to get Hostname and IP")
# Function to receive a message
def handle_rece_msg(server: socket.socket):
while True:
try:
msg_rece = server.recv(4505)
if msg_rece:
print("[Client]: ", msg_rece.decode())
else:
server.close()
break
except Exception as e:
print(f'[!] Error handling message from server: {e}')
server.close()
break
# Function to send a message
def handle_sent_msg(server: socket.socket):
while True:
try:
msg_send = input()
if msg_send:
server.send(msg_send.encode())
else:
server.close()
break
except Exception as e:
print(f'[!] Error handling message from server: {e}')
server.close()
break
# Function to handle threads
def server_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:
get_Host_name_IP()
client, addr = server.accept()
print("[*] Accepted connection from: %s:%d" % (addr[0], addr[1]))
print("[$] Chat started")
try:
server_thread()
except:
print("Error: {e}")
server.close()
break