Connect to SiLA Servers

SiLA Server Discovery

Use the classmetod SilaClient.discover to discover SiLA Servers on the network and connect to them:

from sila2.client import SilaClient

client = SilaClient.discover(...)

This also works for generated client classes:

from my_generated_sila2_package import Client

client = Client.discover(...)

This method has the following signature to specify which server to connect to, and how:

classmethod SilaClient.discover(*, server_name: str | None = None, server_uuid: UUID | str | None = None, timeout: float = 0, root_certs: bytes | None = None, private_key: bytes | None = None, cert_chain: bytes | None = None, insecure: bool = False) ClientCls

Use SiLA Server Discovery to connect to a SiLA Server. If multiple matching servers are found, the returned client is connected to one of them.

Parameters:
  • server_name – Only connect to SiLA Servers with this server name

  • server_uuid – Only connect to SiLA Servers with this server UUID

  • timeout – Time in seconds. If no matching server was found in this time, a TimeoutError will be raised

  • root_certs – PEM-encoded root certificates

  • private_key – PEM-encoded private key

  • cert_chain – PEM-encoded certificate chain

  • insecure – If True, no encryption will be used. Violates the SiLA 2 specification, only use for debugging purposes

Returns:

client – SiLA Client connected to a discovered matching SiLA Server (instance of the calling class)

Raises:
  • TimeoutError – If no server was found in the given time

  • RuntimeError – If a server was found but did not implement required features

Warning

Using unencrypted communication violates the SiLA specification and should only be used for testing purposes

Manual connection

If you know the address of the SiLA Server, you can instantiate a SilaClient directly:

from sila2.client import SilaClient

client = SilaClient("127.0.0.1", 50052, root_certs=open("ca.pem", "rb").read())

Note

SiLA specifies that servers using untrusted certificates must broadcast their certificate authority using the SiLA Server Discovery mechanism. When using manual connection, encryption information must be provided explicitly to connect to untrusted servers (e.g. using root_certs=...).

This also works for generated client classes:

from my_generated_sila2_package import Client

client = Client("127.0.0.1", 50052, root_certs=open("ca.pem", "rb").read())

The class initializer has the following signature to specify how to connect to the server:

class sila2.client.SilaClient(address: str, port: int, *, root_certs: bytes | None = None, private_key: bytes | None = None, cert_chain: bytes | None = None, insecure: bool = False)

SiLA Client, which is connected to a SiLA Server

Parameters:
  • address – IP address or hostname of the SiLA Server

  • port – Port of the SiLA Server

  • root_certs – PEM-encoded root certificates

  • private_key – PEM-encoded private key

  • cert_chain – PEM-encoded certificate chain

  • insecure – If True, no encryption will be used. Violates the SiLA 2 specification, only use for debugging purposes

Raises:

RuntimeError – If the server does not implement required features

Warning

Using unencrypted communication violates the SiLA specification and should only be used for testing purposes

clear_binary_transfer_download_cache() None

Delete all downloaded binary transfer data.

Context Manager

The SilaClient class is a context manager, which means it can be used in a with statement:

from sila2.client import SilaClient

with SilaClient(...) as client:
    ...

Note

The client will automatically disconnect when the context manager exits, releasing any resources in use. Alternatively the client close() method can be called to release resources manually.