Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Find a local free port to start a server in linux
#1
The following function uses the output of ss -lntu to solve this problem
import subprocess as sp
import re
import itertools as itt

def find_free_port(start=9000):
    s = sp.check_output(['ss', '-lntu']).decode()
    maybusy = set(int(x) for x in re.findall('\d+', s))
    return next(itt.filterfalse(maybusy.__contains__, itt.count(start)))

if __name__ == '__main__':
    print('free port:', find_free_port())
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  web crawler with Tor proxy & Firefox ESR on Linux Kali rootVIII 0 155 Jul-01-2019, 04:02 AM
Last Post: rootVIII
  port scanner rootVIII 0 357 Jan-16-2019, 05:25 AM
Last Post: rootVIII
  The Python Tkinter Youtube Audio Downloader for Linux rootVIII 0 454 Dec-19-2018, 11:33 PM
Last Post: rootVIII
  Getting wanted data from the 'top' command (Linux) rootVIII 7 822 Nov-19-2018, 09:41 PM
Last Post: DeaD_EyE
  Linux user folder names (if translated) Axel_Erfurt 0 443 Sep-23-2018, 05:55 PM
Last Post: Axel_Erfurt
  ptb.py - process trace back for linux Skaperen 3 867 Feb-06-2018, 08:56 AM
Last Post: Gribouillis
  Watch files and automatically run a script in Linux Gribouillis 3 1,172 Jan-23-2018, 06:50 PM
Last Post: nilamo
  ATA Pass-Through library for linux kazenniy 0 1,174 Nov-07-2016, 08:36 AM
Last Post: kazenniy
  msvcrt.getkey for linux metulburr 0 4,773 Nov-03-2016, 08:18 PM
Last Post: metulburr

Forum Jump:


Users browsing this thread: 1 Guest(s)