# &adminguide;

## Overview

This module is a port of the 'app_python' module to Python 3. It is based on the work of Maxim Sobolev.

This module cannot be loaded together with 'app_python' as global symbols have not been renamed. To ease transition, the functions, KEMI exports, and RPC commands have the same names as 'app_python', which also means the two modules cannot coexist.

This module allows executing Python scripts from the config file, exporting functions to access the SIP message from Python.

For some basic examples of Python scripts that can be used with this module, look at the files inside the source tree located at 'modules/app_python3/python_examples/'.

Note: if symbols exported to KEMI (module or function names) conflict with Python's reserved keywords, use the 'getattr()' function or the '__dict__' attribute for 'KSR' (e.g., 'KSR.__dict__["async"].task_route("myroute")').

## Dependencies

### &kamailio; Modules

The following modules must be loaded before this module:

- none.

### External Libraries or Applications

The following libraries or applications must be installed before running &kamailio; with this module loaded:

- python3 - Python 3 runtime.

To compile this module the Python 3 development package is needed. Requirements:

- python3-dev - Python 3 development package.

- python3-config - (part of python3-dev) tool to output C includes and library paths.

## Parameters

### `load` (string)

The path to the file with Python code to be executed from configuration file.

Default value is /usr/local/etc/kamailio/handler.py.

#### Set `load` parameter

    ...
    modparam("app_python3", "load", "/usr/local/etc/kamailio/myscript.py")
    ...

### `script_name` (string)

This is same as "load" parameter, kept for backward compatibility with the older versions of the module.

### `mod_init_function` (string)

The Python function to be executed by this module when it is initialized by &kamailio;.

Default value is mod_init.

#### Set `mod_init_function` parameter

    ...
    modparam("app_python3", "mod_init_function", "my_mod_init")
    ...

### `child_init_method` (string)

The Python function to be executed by this module when a new worker process (child) is initialized by &kamailio;.

Default value is child_init.

#### Set `child_init_method` parameter

    ...
    modparam("app_python3", "child_init_method", "my_child_init")
    ...

## Functions

### `python_exec(method [, args])`

Execute the Python function with the name given by the parameter 'method'. Optionally can be provided a second string with parameters to be passed to the Python function.

Both parameters can contain pseudo-variables.

#### `python_exec` usage

    ...
    python_exec("my_python_function");
    python_exec("my_python_function", "my_params");
    python_exec("my_python_function", "$rU");
    ...

## RPC Commands

### `app_python.reload`

IMPORTANT: this is not thread-safe. In your Python script do not use C extensions with threads that call into `apy_exec()`.

Marks the need to reload the Python script. The actual reload is done in each worker when it next invokes a Python method. The module uses a worker process lock to prevent recursive reloads.

This function only reloads (re-executes) the user script and creates a new script object. It does not reinitialize the interpreter (references in the old module remain if not redefined by the new version).

Name: app_python.reload

Parameters: none

Example:

    ...
    &kamcmd; app_python.reload
    ...

Note that reload is done for the Python script provided as parameter to this &kamailio; module. To reload the Python libraries imported in this script, use something like:

    ...
    import mod1
    ...
    import modN
    from importlib import reload

    def mod_init():
        reload(mod1)
        ...
        reload(modN)
        return kamailio()
    ...

Where "modX" are the modules imported at the top.

### `app_python.api_list`

List the functions available via Kemi framework.

Name: app_python.api_list

Parameters: none

Example:

    ...
    &kamcmd; app_python.api_list
    ...

## KEMI Usage

The module exports KEMI engine with id "python".

Example:

    ...
    loadmodule "app_python3.so"
    ...
    cfgengine "python"
    ...

For more details about KEMI, see: https://www.kamailio.org/docs/tutorials/devel/kamailio-kemi-framework/