code: def get_unused_port(port=None):
      
      if port is None or port < 1024 or port > 65535:
          port = random.randint(1024, 65535)
      assert(1024 <= port <= 65535)
      while True:
          s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          try:
              s.bind(('', port))  # Try to open port
          except socket.error as e:
              if e.errno in (98, 10048):  # 98, 10048 means address already bound
                  return get_unused_port(None)
              raise e
          s.close()
          return port
docs: Checks if port is already in use.