-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathear.py
47 lines (34 loc) · 1.35 KB
/
ear.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
#!/usr/bin/env python
import socketserver
import threading
# Credit for much of this code goes to the following:
# https://stackoverflow.com/questions/35279655/connecting-to-multiple-ports-using-python-socketserver
class Ear(socketserver.BaseRequestHandler):
def handle(self):
self.data = self.request.recv(2048)
if self.data:
if "ping" in repr(self.data.strip()):
print("%s reached us on port %i." % ( self.client_address[0], self.request.getsockname()[1]))
self.request.sendall(b'pong %i\n' %(self.request.getsockname()[1]))
else:
print("Received: '%s' from '%s:%i' on port '%i'." % (repr(self.data), self.client_address[0], self.client_address[1], self.request.getsockname()[1]))
self.request.sendall(self.data)
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
allow_reuse_address = True
pass
def main():
global Ears
global Ear_threads
Ears = []
Ear_threads =[]
for port in range(1,1000):
Ears.append(ThreadedTCPServer(('', port), Ear))
for ear in Ears:
Ear_threads.append(threading.Thread(target=ear.serve_forever))
for ear_thread in Ear_threads:
ear_thread.setDaemon(True)
ear_thread.start()
while True:
continue
if __name__ == '__main__':
main()