Initial commit
This commit is contained in:
commit
55c3caa766
49 files changed
+1602
No files matched your search
@@ -0,0 +1,32 @@
|
||||
import socket
|
||||
import struct
|
||||
|
||||
class Discover:
|
||||
DISCOVER_PORT = 28149
|
||||
MAGIC = b"OK"
|
||||
|
||||
@staticmethod
|
||||
def listen(real_port, host="0.0.0.0"):
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
s.bind((host, Discover.DISCOVER_PORT))
|
||||
s.listen()
|
||||
|
||||
while True:
|
||||
conn, _ = s.accept()
|
||||
try:
|
||||
data = conn.recv(2)
|
||||
if data == Discover.MAGIC:
|
||||
conn.sendall(struct.pack("!H", real_port))
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
@staticmethod
|
||||
def request(target_host, timeout=3):
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.settimeout(timeout)
|
||||
s.connect((target_host, Discover.DISCOVER_PORT))
|
||||
s.sendall(Discover.MAGIC)
|
||||
data = s.recv(2)
|
||||
s.close()
|
||||
return struct.unpack("!H", data)[0]
|
||||
Reference in new issue
Block a user