| 1 |
app_python3 Module |
| 2 |
|
| 3 |
Maxim Sobolev |
| 4 |
|
| 5 |
Anthony Alba |
| 6 |
|
| 7 |
Edited by |
| 8 |
|
| 9 |
Maxim Sobolev |
| 10 |
|
| 11 |
Copyright © 2010 Maxim Sobolev |
| 12 |
__________________________________________________________________ |
| 13 |
|
| 14 |
Table of Contents |
| 15 |
|
| 16 |
1. Admin Guide |
| 17 |
|
| 18 |
1. Overview |
| 19 |
2. Dependencies |
| 20 |
|
| 21 |
2.1. Kamailio Modules |
| 22 |
2.2. External Libraries or Applications |
| 23 |
|
| 24 |
3. Parameters |
| 25 |
|
| 26 |
3.1. load (string) |
| 27 |
3.2. script_name (string) |
| 28 |
3.3. mod_init_function (string) |
| 29 |
3.4. child_init_method (string) |
| 30 |
3.5. threads_mode (int) |
| 31 |
|
| 32 |
4. Functions |
| 33 |
|
| 34 |
4.1. python_exec(method [, args]) |
| 35 |
|
| 36 |
5. RPC Commands |
| 37 |
|
| 38 |
5.1. app_python.reload |
| 39 |
5.2. app_python.api_list |
| 40 |
|
| 41 |
6. KEMI Usage |
| 42 |
|
| 43 |
List of Examples |
| 44 |
|
| 45 |
1.1. Set load parameter |
| 46 |
1.2. Set mod_init_function parameter |
| 47 |
1.3. Set child_init_method parameter |
| 48 |
1.4. Set threads_mode parameter |
| 49 |
1.5. python_exec usage |
| 50 |
|
| 51 |
Chapter 1. Admin Guide |
| 52 |
|
| 53 |
Table of Contents |
| 54 |
|
| 55 |
1. Overview |
| 56 |
2. Dependencies |
| 57 |
|
| 58 |
2.1. Kamailio Modules |
| 59 |
2.2. External Libraries or Applications |
| 60 |
|
| 61 |
3. Parameters |
| 62 |
|
| 63 |
3.1. load (string) |
| 64 |
3.2. script_name (string) |
| 65 |
3.3. mod_init_function (string) |
| 66 |
3.4. child_init_method (string) |
| 67 |
3.5. threads_mode (int) |
| 68 |
|
| 69 |
4. Functions |
| 70 |
|
| 71 |
4.1. python_exec(method [, args]) |
| 72 |
|
| 73 |
5. RPC Commands |
| 74 |
|
| 75 |
5.1. app_python.reload |
| 76 |
5.2. app_python.api_list |
| 77 |
|
| 78 |
6. KEMI Usage |
| 79 |
|
| 80 |
1. Overview |
| 81 |
|
| 82 |
This module is a port of the 'app_python' module to Python 3. It is |
| 83 |
based on the work of Maxim Sobolev. |
| 84 |
|
| 85 |
This module cannot be loaded together with 'app_python' as global |
| 86 |
symbols have not been renamed. To ease transition, the functions, KEMI |
| 87 |
exports, and RPC commands have the same names as 'app_python', which |
| 88 |
also means the two modules cannot coexist. |
| 89 |
|
| 90 |
This module allows executing Python scripts from the config file, |
| 91 |
exporting functions to access the SIP message from Python. |
| 92 |
|
| 93 |
For some basic examples of Python scripts that can be used with this |
| 94 |
module, look at the files inside the source tree located at |
| 95 |
'modules/app_python3/python_examples/'. |
| 96 |
|
| 97 |
Note: if symbols exported to KEMI (module or function names) conflict |
| 98 |
with Python's reserved keywords, use the 'getattr()' function or the |
| 99 |
'__dict__' attribute for 'KSR' (e.g., |
| 100 |
'KSR.__dict__["async"].task_route("myroute")'). |
| 101 |
|
| 102 |
2. Dependencies |
| 103 |
|
| 104 |
2.1. Kamailio Modules |
| 105 |
2.2. External Libraries or Applications |
| 106 |
|
| 107 |
2.1. Kamailio Modules |
| 108 |
|
| 109 |
The following modules must be loaded before this module: |
| 110 |
* none. |
| 111 |
|
| 112 |
2.2. External Libraries or Applications |
| 113 |
|
| 114 |
The following libraries or applications must be installed before |
| 115 |
running Kamailio with this module loaded: |
| 116 |
* python3 - Python 3 runtime. |
| 117 |
|
| 118 |
To compile this module the Python 3 development package is needed. |
| 119 |
Requirements: |
| 120 |
* python3-dev - Python 3 development package. |
| 121 |
* python3-config - (part of python3-dev) tool to output C includes |
| 122 |
and library paths. |
| 123 |
|
| 124 |
3. Parameters |
| 125 |
|
| 126 |
3.1. load (string) |
| 127 |
3.2. script_name (string) |
| 128 |
3.3. mod_init_function (string) |
| 129 |
3.4. child_init_method (string) |
| 130 |
3.5. threads_mode (int) |
| 131 |
|
| 132 |
3.1. load (string) |
| 133 |
|
| 134 |
The path to the file with Python code to be executed from configuration |
| 135 |
file. |
| 136 |
|
| 137 |
Default value is “/usr/local/etc/kamailio/handler.py”. |
| 138 |
|
| 139 |
Example 1.1. Set load parameter |
| 140 |
... |
| 141 |
modparam("app_python3", "load", "/usr/local/etc/kamailio/myscript.py") |
| 142 |
... |
| 143 |
|
| 144 |
3.2. script_name (string) |
| 145 |
|
| 146 |
This is same as "load" parameter, kept for backward compatibility with |
| 147 |
the older versions of the module. |
| 148 |
|
| 149 |
3.3. mod_init_function (string) |
| 150 |
|
| 151 |
The Python function to be executed by this module when it is |
| 152 |
initialized by Kamailio. |
| 153 |
|
| 154 |
Default value is “mod_init”. |
| 155 |
|
| 156 |
Example 1.2. Set mod_init_function parameter |
| 157 |
... |
| 158 |
modparam("app_python3", "mod_init_function", "my_mod_init") |
| 159 |
... |
| 160 |
|
| 161 |
3.4. child_init_method (string) |
| 162 |
|
| 163 |
The Python function to be executed by this module when a new worker |
| 164 |
process (child) is initialized by Kamailio. |
| 165 |
|
| 166 |
Default value is “child_init”. |
| 167 |
|
| 168 |
Example 1.3. Set child_init_method parameter |
| 169 |
... |
| 170 |
modparam("app_python3", "child_init_method", "my_child_init") |
| 171 |
... |
| 172 |
|
| 173 |
3.5. threads_mode (int) |
| 174 |
|
| 175 |
Control how locking for Python interpreter threads in done. If set to |
| 176 |
1, use Py_BLOCK_THREADS/Py_UNBLOCK_THREADS (new mode added for v6.0.x). |
| 177 |
If set to 0, use PyGILState_Ensure()/PyGILState_Release() (the mode |
| 178 |
implemented initially). |
| 179 |
|
| 180 |
If set to 1, Python threading Thread objects created in the KEMI script |
| 181 |
will be scheduled. Otherwise, additional Python threads will not be |
| 182 |
scheduled (no release of GIL). |
| 183 |
|
| 184 |
Default value is “0”. |
| 185 |
|
| 186 |
Example 1.4. Set threads_mode parameter |
| 187 |
... |
| 188 |
modparam("app_python3", "threads_mode", 1) |
| 189 |
... |
| 190 |
|
| 191 |
4. Functions |
| 192 |
|
| 193 |
4.1. python_exec(method [, args]) |
| 194 |
|
| 195 |
4.1. python_exec(method [, args]) |
| 196 |
|
| 197 |
Execute the Python function with the name given by the parameter |
| 198 |
'method'. Optionally can be provided a second string with parameters to |
| 199 |
be passed to the Python function. |
| 200 |
|
| 201 |
Both parameters can contain pseudo-variables. |
| 202 |
|
| 203 |
Example 1.5. python_exec usage |
| 204 |
... |
| 205 |
python_exec("my_python_function"); |
| 206 |
python_exec("my_python_function", "my_params"); |
| 207 |
python_exec("my_python_function", "$rU"); |
| 208 |
... |
| 209 |
|
| 210 |
5. RPC Commands |
| 211 |
|
| 212 |
5.1. app_python.reload |
| 213 |
5.2. app_python.api_list |
| 214 |
|
| 215 |
5.1. app_python.reload |
| 216 |
|
| 217 |
IMPORTANT: this is not thread-safe. In your Python script do not use C |
| 218 |
extensions with threads that call into apy_exec(). |
| 219 |
|
| 220 |
Marks the need to reload the Python script. The actual reload is done |
| 221 |
in each worker when it next invokes a Python method. The module uses a |
| 222 |
worker process lock to prevent recursive reloads. |
| 223 |
|
| 224 |
This function only reloads (re-executes) the user script and creates a |
| 225 |
new script object. It does not reinitialize the interpreter (references |
| 226 |
in the old module remain if not redefined by the new version). |
| 227 |
|
| 228 |
Name: app_python.reload |
| 229 |
|
| 230 |
Parameters: none |
| 231 |
|
| 232 |
Example: |
| 233 |
... |
| 234 |
kamctl rpc app_python.reload |
| 235 |
... |
| 236 |
|
| 237 |
Note that reload is done for the Python script provided as parameter to |
| 238 |
this Kamailio module. To reload the Python libraries imported in this |
| 239 |
script, use something like: |
| 240 |
... |
| 241 |
import mod1 |
| 242 |
... |
| 243 |
import modN |
| 244 |
from importlib import reload |
| 245 |
|
| 246 |
def mod_init(): |
| 247 |
reload(mod1) |
| 248 |
... |
| 249 |
reload(modN) |
| 250 |
return kamailio() |
| 251 |
... |
| 252 |
|
| 253 |
Where "modX" are the modules imported at the top. |
| 254 |
|
| 255 |
5.2. app_python.api_list |
| 256 |
|
| 257 |
List the functions available via Kemi framework. |
| 258 |
|
| 259 |
Name: app_python.api_list |
| 260 |
|
| 261 |
Parameters: none |
| 262 |
|
| 263 |
Example: |
| 264 |
... |
| 265 |
kamctl rpc app_python.api_list |
| 266 |
... |
| 267 |
|
| 268 |
6. KEMI Usage |
| 269 |
|
| 270 |
The module exports KEMI engine with id "python". |
| 271 |
|
| 272 |
Example: |
| 273 |
... |
| 274 |
loadmodule "app_python3.so" |
| 275 |
... |
| 276 |
cfgengine "python" |
| 277 |
... |
| 278 |
|
| 279 |
For more details about KEMI, see: |
| 280 |
https://www.kamailio.org/docs/tutorials/devel/kamailio-kemi-framework/ |