bgpy package

Subpackages

Submodules

bgpy.cli module

bgpy.cli.main()
bgpy.cli.run_server(host: str = <typer.models.ArgumentInfo object>, port: int = <typer.models.ArgumentInfo object>, token: bool = <typer.models.OptionInfo object>, log_level: str = <typer.models.OptionInfo object>, log_file: Optional[pathlib.Path] = <typer.models.OptionInfo object>, init_file: Optional[pathlib.Path] = <typer.models.OptionInfo object>) None

Start a bgpy server.

Run a bgpy server on the given host, which starts listening to the provided port. Note: Before calling the ‘initialize()’ method of the ‘Client’ class and passing ‘init_task()’, exec_task()’ and ‘exit_task()’ to the server, the server will not respond to requests.

bgpy.cli.terminate_server(host: str = <typer.models.ArgumentInfo object>, port: int = <typer.models.ArgumentInfo object>, token: bool = <typer.models.OptionInfo object>, log_level: str = <typer.models.OptionInfo object>, log_file: Optional[pathlib.Path] = <typer.models.OptionInfo object>) None

Terminate a bgpy server.

Terminate a bgpy server on the given host, which is listening to the provided port.

bgpy.cli.version_info()

Version information.

Prints the version information of the package.

bgpy.client module

class bgpy.client.Client(host: str, port: int, token: Optional[str] = None, log_level: str = 'INFO', log_file: Optional[pathlib.Path] = None)

Bases: object

Client to send INIT, EXEC and EXIT messages to the the server.

execute(exec_args: dict, await_response: bool = False) dict

Send a command to the server

Sends an EXEC signal to the server with custom arguments, which are passed to the predefined ‘exec_task’.

Parameters
  • exec_args (dict) – Arguments to send to the ‘exec_task’. It is most useful to define a command key and a short abbreviation for which the task is checking and therefore knows what to do.

  • await_response (bool, optional) – Wait for a second ‘custom’ response of the server, by default False.

Returns

Response of the server.

Return type

dict

host
initialize(init_task: Callable, exec_task: Callable, exit_task: Callable) Optional[dict]

Run and intialize a bgpy server on the given host, which starts listening to the provided port. After starting the server, a INIT message with the ‘init_task()’, ‘exec_task()’ and ‘exit_task()’ tasks are send to the server in order to complete the initialization.

Parameters
  • init_task (Callable) – Task that runs once during initialization and can be used to set up the server. The return value of this function must be a dict, which is then passed to the ‘exec_task’ function with every request of a client.

  • exec_task (Callable) – Task that is called each time a request is made. Here the message from the ‘execute’ method is interpreted and a task has to be defined accordingly. Using the function ‘respond’ on the server, a second response can be sent to the client after the standard confirmation of the receipt of the message by the server.

  • exit_task (Callable) – Task that is executed once if an exit singal is sent to the server by the ‘terminate’ method. The input of this function is the return value of the ‘exec_task’ function (or if bever called, the return value from the ‘init_task’). With ‘respond’ a second message can be sent to the client.

Returns

Response of the server.

Return type

Optional[dict]

log_file
log_level
port
terminate(exit_args: dict = {}, await_response: bool = False) dict

Terminate the server

Send an EXIT message to the server in order to execute the ‘exit_task’ and exit the main loop on the server.

Parameters
  • exit_args (dict, optional) – Arguments to send to the ‘exit_task’.

  • await_response (bool, optional) – Wait for a second ‘custom’ response of the server, by default False.

Returns

Response of the server.

Return type

dict

token

bgpy.server module

class bgpy.server.Server(host: str, port: int, token: Optional[str] = None, log_level: str = 'INFO', log_file: Optional[pathlib.Path] = None, init_file: Optional[pathlib.Path] = None)

Bases: object

Server to which reveives INIT, EXEC and EXIT messages from clients, and responds with messages of type OK or ERROR.

host
init_file
log
log_file
log_level
port
run()

Start the main loop of the server.

run_background() None

Run the server in the background.

Returns

Response of the server.

Return type

Optional[dict]

token
bgpy.server.respond(client_socket: bgpy.core.sockets.ClientSocket, response: dict, error: bool = False) Optional[bgpy.core.message.Message]

Respond to the client

Send a message OK with custom arguments to the client that initially sent a EXEC message. Note: The client has to know that a second response is sent by the server and therefore wait for it (await_response=True).

Parameters
  • client_socket (ClientSocket) – Client socket, which was set up by the server to handle the communication with the client socket on the client.

  • response (dict) – Response to send to the client.

  • error (bool) – Respond with a Message of type ERROR instead of OK, default is False.

Returns

Response of the client.

Return type

Optional[Message]

Module contents

Top-level package for bgpy.

class bgpy.Client(host: str, port: int, token: Optional[str] = None, log_level: str = 'INFO', log_file: Optional[pathlib.Path] = None)

Bases: object

Client to send INIT, EXEC and EXIT messages to the the server.

execute(exec_args: dict, await_response: bool = False) dict

Send a command to the server

Sends an EXEC signal to the server with custom arguments, which are passed to the predefined ‘exec_task’.

Parameters
  • exec_args (dict) – Arguments to send to the ‘exec_task’. It is most useful to define a command key and a short abbreviation for which the task is checking and therefore knows what to do.

  • await_response (bool, optional) – Wait for a second ‘custom’ response of the server, by default False.

Returns

Response of the server.

Return type

dict

host
initialize(init_task: Callable, exec_task: Callable, exit_task: Callable) Optional[dict]

Run and intialize a bgpy server on the given host, which starts listening to the provided port. After starting the server, a INIT message with the ‘init_task()’, ‘exec_task()’ and ‘exit_task()’ tasks are send to the server in order to complete the initialization.

Parameters
  • init_task (Callable) – Task that runs once during initialization and can be used to set up the server. The return value of this function must be a dict, which is then passed to the ‘exec_task’ function with every request of a client.

  • exec_task (Callable) – Task that is called each time a request is made. Here the message from the ‘execute’ method is interpreted and a task has to be defined accordingly. Using the function ‘respond’ on the server, a second response can be sent to the client after the standard confirmation of the receipt of the message by the server.

  • exit_task (Callable) – Task that is executed once if an exit singal is sent to the server by the ‘terminate’ method. The input of this function is the return value of the ‘exec_task’ function (or if bever called, the return value from the ‘init_task’). With ‘respond’ a second message can be sent to the client.

Returns

Response of the server.

Return type

Optional[dict]

log_file
log_level
port
terminate(exit_args: dict = {}, await_response: bool = False) dict

Terminate the server

Send an EXIT message to the server in order to execute the ‘exit_task’ and exit the main loop on the server.

Parameters
  • exit_args (dict, optional) – Arguments to send to the ‘exit_task’.

  • await_response (bool, optional) – Wait for a second ‘custom’ response of the server, by default False.

Returns

Response of the server.

Return type

dict

token
class bgpy.Server(host: str, port: int, token: Optional[str] = None, log_level: str = 'INFO', log_file: Optional[pathlib.Path] = None, init_file: Optional[pathlib.Path] = None)

Bases: object

Server to which reveives INIT, EXEC and EXIT messages from clients, and responds with messages of type OK or ERROR.

host
init_file
log
log_file
log_level
port
run()

Start the main loop of the server.

run_background() None

Run the server in the background.

Returns

Response of the server.

Return type

Optional[dict]

token
bgpy.respond(client_socket: bgpy.core.sockets.ClientSocket, response: dict, error: bool = False) Optional[bgpy.core.message.Message]

Respond to the client

Send a message OK with custom arguments to the client that initially sent a EXEC message. Note: The client has to know that a second response is sent by the server and therefore wait for it (await_response=True).

Parameters
  • client_socket (ClientSocket) – Client socket, which was set up by the server to handle the communication with the client socket on the client.

  • response (dict) – Response to send to the client.

  • error (bool) – Respond with a Message of type ERROR instead of OK, default is False.

Returns

Response of the client.

Return type

Optional[Message]

bgpy.token_create(length: int = 64) str

Creates a url safe token with a defined length.

Parameters

length (int, optional) – Length of the token, by default 64

Returns

The token.

Return type

str