Find available port when starting backend plugin

CURA-11067
This commit is contained in:
c.lamboo 2023-09-20 11:24:28 +02:00
parent 26e7995588
commit d4449c680f
2 changed files with 11 additions and 3 deletions

View file

@ -1,5 +1,6 @@
# Copyright (c) 2023 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
import socket
import subprocess
from typing import Optional, List
@ -42,6 +43,15 @@ class BackendPlugin(AdditionalSettingDefinitionsAppender, PluginObject):
def getAddress(self) -> str:
return self._plugin_address
def setAvailablePort(self) -> None:
"""
Sets the port to a random available port.
"""
sock = socket.socket()
sock.bind((self.getAddress(), 0))
port = sock.getsockname()[1]
self.setPort(port)
def _validatePluginCommand(self) -> list[str]:
"""
Validate the plugin command and add the port parameter if it is missing.