server
server
Server side of the Nyx bridge: accepts clients and sends them code to run.
Everything here runs on plain threads and standard library sockets, no Qt. The crawler is synchronous, so a request just blocks its caller until the client answers. Never touch stage objects from the threads of this module.
Attributes
LOGGER = logging.getLogger(__name__)
module-attribute
Classes
BridgeServer(name=None, host=protocol.DEFAULT_HOST, port=None, token=None, address_file=None)
TCP server clients connect to.
Nyx listens and the client applications connect out, so the side executing code
never accepts connections. A client has to open with a hello carrying the
server's token. Requests are then sent to its ClientSession.
Subclass it to change the class attributes and validate_client. To follow what
happens to the server, add a listener, see add_listener.
Attributes:
| Name | Type | Description |
|---|---|---|
DEFAULT_PORT |
first port tried when no port is requested |
|
PORT_FALLBACK_COUNT |
number of ports tried from the default one |
|
HANDSHAKE_TIMEOUT |
seconds a new connection has to send its hello |
Initialize BridgeServer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
server name, the class name if omitted. |
None
|
host
|
str
|
interface to listen on. Keep it local: clients can have code run on them by whoever holds the token. |
DEFAULT_HOST
|
port
|
int | None
|
port to listen on. None tries |
None
|
token
|
str | None
|
shared secret clients have to present, random if omitted. |
None
|
address_file
|
str | PathLike | None
|
file that tells clients where the server is. Written when the server starts and removed when it stops. |
None
|
Source code in src/nyx_bridge/server.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | |
Attributes
DEFAULT_PORT = protocol.DEFAULT_PORT
class-attribute
instance-attribute
HANDSHAKE_TIMEOUT = 5.0
class-attribute
instance-attribute
PORT_FALLBACK_COUNT = 20
class-attribute
instance-attribute
address_file = address_file
instance-attribute
clients
property
list[ClientSession]: connected clients.
host = host
instance-attribute
is_running
property
bool: the server accepts connections.
name = name or type(self).__name__
instance-attribute
port
property
int | None: port the server listens on, the requested one before start.
token = token or secrets.token_urlsafe(16)
instance-attribute
Methods:
__enter__()
Start the server.
Source code in src/nyx_bridge/server.py
330 331 332 333 | |
__exit__(*exc_info)
Stop the server.
Source code in src/nyx_bridge/server.py
335 336 337 | |
__repr__()
str: server with its name and address.
Source code in src/nyx_bridge/server.py
326 327 328 | |
add_listener(listener)
Get told about what happens to the server.
A listener is any object with some of these methods, the others are skipped:
on_server_started(server), on_server_stopped(server),
on_client_connected(server, session) and
on_client_disconnected(server, session).
They are called on the threads of the server, not on the one that added the listener, so they have to be quick and thread safe. A failing listener is logged and does not affect the server or the other listeners.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
listener
|
Any
|
object to call, ignored if it is already added. |
required |
Source code in src/nyx_bridge/server.py
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | |
find_clients(name=None, kind=None, client_id=None)
Find connected clients. Criteria left out match anything.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
client name. |
None
|
kind
|
str | None
|
client kind. |
None
|
client_id
|
str | None
|
client id. |
None
|
Returns:
| Type | Description |
|---|---|
list[ClientSession]
|
list[ClientSession]: matching clients, in order of connection. |
Source code in src/nyx_bridge/server.py
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 | |
remove_listener(listener)
Stop telling a listener about the server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
listener
|
Any
|
listener to remove, ignored if it is not added. |
required |
Source code in src/nyx_bridge/server.py
387 388 389 390 391 392 393 394 395 | |
start()
Start listening and accepting clients.
Raises:
| Type | Description |
|---|---|
OSError
|
no port could be bound. |
Source code in src/nyx_bridge/server.py
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | |
stop()
Stop listening and disconnect every client.
Source code in src/nyx_bridge/server.py
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | |
validate_client(hello)
Decide whether a client may connect, once its token has been checked.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hello
|
dict
|
hello message of the client. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
str | None: reason to refuse the client, None to accept it. |
Source code in src/nyx_bridge/server.py
356 357 358 359 360 361 362 363 364 365 | |
wait_for_client(name=None, kind=None, client_id=None, timeout=None)
Wait until a matching client is connected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
client name. |
None
|
kind
|
str | None
|
client kind. |
None
|
client_id
|
str | None
|
client id. |
None
|
timeout
|
float | None
|
seconds to wait, forever if None. |
None
|
Raises:
| Type | Description |
|---|---|
TimeoutError
|
no matching client within the timeout. |
RuntimeError
|
the server was stopped while waiting. |
Returns:
| Name | Type | Description |
|---|---|---|
ClientSession |
ClientSession
|
first matching client. |
Source code in src/nyx_bridge/server.py
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 | |
write_address_file(path)
Write where this server listens, see protocol.write_address_file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike
|
file to write. |
required |
Source code in src/nyx_bridge/server.py
452 453 454 455 456 457 458 | |
ClientDisconnected
Bases: ConnectionError
The client went away before it answered.
ClientSession(sock, client_id, name, kind, version, address)
A connected client, with the requests waiting for its answers.
Initialize ClientSession.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sock
|
socket
|
connection, already handshaken. |
required |
client_id
|
str
|
id given to the client by the server. |
required |
name
|
str
|
client name. |
required |
kind
|
str
|
client kind, e.g. "blender". |
required |
version
|
str
|
client application version. |
required |
address
|
tuple
|
remote address of the connection. |
required |
Source code in src/nyx_bridge/server.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
Attributes
address = address
instance-attribute
id = client_id
instance-attribute
is_connected
property
bool: the connection is open.
kind = kind
instance-attribute
name = name
instance-attribute
version = version
instance-attribute
Methods:
__repr__()
str: client with its name and kind.
Source code in src/nyx_bridge/server.py
108 109 110 | |
close()
Drop the connection. Pending requests fail with ClientDisconnected.
Source code in src/nyx_bridge/server.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
execute(code, args=None, mode=protocol.EXEC, timeout=None)
Run code in the client and wait for the answer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
source to run. |
required |
args
|
dict | None
|
names to set in the client's namespace first, has to be JSON serializable. |
None
|
mode
|
str
|
|
EXEC
|
timeout
|
float | None
|
seconds to wait for the answer, forever if None. |
None
|
Raises:
| Type | Description |
|---|---|
TypeError
|
args are not JSON serializable. |
ClientDisconnected
|
the client is gone or went away while waiting. |
TimeoutError
|
no answer within the timeout. |
RemoteExecutionError
|
the code raised in the client. |
Returns:
| Name | Type | Description |
|---|---|---|
ExecutionResult |
ExecutionResult
|
value and output. |
Source code in src/nyx_bridge/server.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
ping(timeout=5.0)
Check the client answers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float | None
|
seconds to wait for the pong. |
5.0
|
Raises:
| Type | Description |
|---|---|
ClientDisconnected
|
the client is gone. |
TimeoutError
|
no answer within the timeout. |
Source code in src/nyx_bridge/server.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
request_shutdown()
Ask the client to disconnect and stop serving.
Source code in src/nyx_bridge/server.py
173 174 175 176 177 178 | |
run()
Read messages until the connection ends. Runs on the connection's thread.
Source code in src/nyx_bridge/server.py
196 197 198 199 200 201 202 203 204 205 206 | |
ExecutionResult(value=None, stdout='', stderr='')
dataclass
What a client answered to an execution.
Attributes:
| Name | Type | Description |
|---|---|---|
value |
Any
|
value of the code, JSON data (see |
stdout |
str
|
output written by the code |
stderr |
str
|
error output written by the code |
Attributes
stderr = ''
class-attribute
instance-attribute
stdout = ''
class-attribute
instance-attribute
value = None
class-attribute
instance-attribute
Methods:
RemoteExecutionError(error, stdout='', stderr='')
Bases: Exception
Code run by a client raised an exception.
Attributes:
| Name | Type | Description |
|---|---|---|
type_name |
name of the exception class raised in the client |
|
message |
message of the exception |
|
remote_traceback |
traceback formatted by the client |
|
stdout |
output of the code before it failed |
|
stderr |
error output of the code before it failed |
Initialize RemoteExecutionError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error
|
dict
|
|
required |
stdout
|
str
|
output of the code. |
''
|
stderr
|
str
|
error output of the code. |
''
|
Source code in src/nyx_bridge/server.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |