Table of Contents
List of Examples
Table of Contents
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")').
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.
The path to the file with Python code to be executed from configuration file.
Default value is “/usr/local/etc/kamailio/handler.py”.
Example 1.1. Set load
parameter
... modparam("app_python3", "load", "/usr/local/etc/kamailio/myscript.py") ...
This is same as "load" parameter, kept for backward compatibility with the older versions of the module.
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.
Example 1.4. python_exec
usage
... python_exec("my_python_function"); python_exec("my_python_function", "my_params"); python_exec("my_python_function", "$rU"); ...
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.
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/