-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
175 lines (108 loc) · 3.45 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import socket
# # take the server name and port name
# host = 'localhost'
# port = 5000
# # create a socket at server side
# # using TCP / IP protocol
# s = socket.socket(socket.AF_INET,
# socket.SOCK_STREAM)
# # bind the socket with server
# # and port number
# s.bind(('', port))
# # allow maximum 1 connection to
# # the socket
# s.listen(2)
# # wait till a client accept
# # connection
# c, addr = s.accept()
# # display client address
# print("CONNECTION FROM:", str(addr))
# # send message to the client after
# # encoding into binary string
# c.send(b"HELLO, How are you ? \
# Welcome to Akash hacking World")
# msg = "Bye.............."
# c.send(msg.encode())
# trials = 0
# while trials < 10:
# msg = c.recv(1024)
# c.send(msg.decode())
# trials+=1
# # disconnect the server
# c.close()
import socket
import threading
import time
import sys
from getmac import getmac
port = 5005
def jls_extract_def():
for ip in allips:
mac = getmac.get_mac_address(
ip=f'{ip}', network_request=True)
sock = socket.socket(
socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) # UDP
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.bind((ip, 0))
sock.sendto(
(f'ip: {ip} mac: {mac}: host disconnected').encode(), ("255.255.255.255", port))
raise KeyboardInterrupt()
sock.close()
return None
def udp_threading(allips):
port = 5005
getmac.PORT = port # Default is 55555
while True:
try:
ip = allips[0]
mac = getmac.get_mac_address(
ip=f'{ip}', network_request=True)
sock = socket.socket(
socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) # UDP
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.bind((ip, 0))
msg = f'ip: {ip} mac: {mac}: {input("Server: ")}'.encode()
sock.sendto(msg, ("255.255.255.255", port))
data, addr = sock.recvfrom(1024)
print('Client: ', data.decode())
time.sleep(2)
except KeyboardInterrupt or SystemExit:
jls_extract_def()
break
def tcp_threading(allips):
host = "localhost"
port = 5005
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(2)
print(f'Server listening on {host}:{port}')
while True:
try:
c, addr = s.accept()
# print(f'Client {addr} connected')
c.send(f'{addr} joined current users {allips}'.encode())
while True:
data = c.recv(1024).decode()
if not data:
break
print('Client: ', data)
try:
c.send(input('Server: ').encode())
except EOFError:
s.close()
exit()
c.close()
s.close()
except:
print('Exceptions!!!!!!!!!!!!!!!!!!!!!!')
s.close()
sys.exit()
def main():
interfaces = socket.getaddrinfo(
host=socket.gethostname(), port=None, family=socket.AF_INET)
allips = [ip[-1][0] for ip in interfaces]
udp_thread = threading.Thread(target=udp_threading, args=(allips,))
tcp_thread = threading.Thread(target=tcp_threading, args=(allips,))
udp_thread.start()
tcp_thread.start()
main()