code: def new_port():
      
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
      for i in range(12042, 16042):
          try:
              s.bind(('127.0.0.1', i))
              s.close()
              return i
          except socket.error:
              pass
      raise Exception('No local port available')
docs: Find a free local port and allocate it