From d985dcfc6687faf6de9b0c5d379b9c8e3f7d4d21 Mon Sep 17 00:00:00 2001 From: Azalea Gui Date: Tue, 6 Aug 2024 03:51:52 -0400 Subject: [PATCH] [+] rand-port --- scripts/bin/rand-port | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100755 scripts/bin/rand-port diff --git a/scripts/bin/rand-port b/scripts/bin/rand-port new file mode 100755 index 0000000..238760d --- /dev/null +++ b/scripts/bin/rand-port @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 + +import socket +import random + +def find_random_open_port(): + while True: + port = random.randint(1024, 65535) + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + result = s.connect_ex(('localhost', port)) + if result != 0: # Port is available + return port + +if __name__ == "__main__": + random_port = find_random_open_port() + print(f"Random open port: {random_port}")