README
1app_python3 Module
2
3Maxim Sobolev
4
5Anthony Alba
6
7Edited by
8
9Maxim 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
31 4. Functions
32
33 4.1. python_exec(method [, args])
34
35 5. RPC Commands
36
37 5.1. app_python.reload
38 5.2. app_python.api_list
39
40 List of Examples
41
42 1.1. Set load parameter
43 1.2. Set mod_init_function parameter
44 1.3. Set child_init_method parameter
45 1.4. python_exec usage
46
47Chapter 1. Admin Guide
48
49 Table of Contents
50
51 1. Overview
52 2. Dependencies
53
54 2.1. Kamailio Modules
55 2.2. External Libraries or Applications
56
57 3. Parameters
58
59 3.1. load (string)
60 3.2. script_name (string)
61 3.3. mod_init_function (string)
62 3.4. child_init_method (string)
63
64 4. Functions
65
66 4.1. python_exec(method [, args])
67
68 5. RPC Commands
69
70 5.1. app_python.reload
71 5.2. app_python.api_list
72
731. Overview
74
75 This module is a port of the 'app_python' module to Python 3. It is
76 based on the work of Maxim Sobolev.
77
78 This module cannot be loaded together with 'app_python' as global
79 symbols have not been renamed. To ease transition, the functions, KEMI
80 exports, and RPC commands have the same names as 'app_python', which
81 also means the two modules cannot coexist.
82
83 This module allows executing Python scripts from the config file,
84 exporting functions to access the SIP message from Python.
85
86 For some basic examples of Python scripts that can be used with this
87 module, look at the files inside the source tree located at
88 'modules/app_python3/python_examples/'.
89
902. Dependencies
91
92 2.1. Kamailio Modules
93 2.2. External Libraries or Applications
94
952.1. Kamailio Modules
96
97 The following modules must be loaded before this module:
98 * none.
99
1002.2. External Libraries or Applications
101
102 The following libraries or applications must be installed before
103 running Kamailio with this module loaded:
104 * python3 - Python 3 runtime.
105
106 To compile this module the Python 3 development package is needed.
107 Requirements:
108 * python3-dev - Python 3 development package.
109 * python3-config - (part of python3-dev) tool to output C includes
110 and library paths.
111
1123. Parameters
113
114 3.1. load (string)
115 3.2. script_name (string)
116 3.3. mod_init_function (string)
117 3.4. child_init_method (string)
118
1193.1. load (string)
120
121 The path to the file with Python code to be executed from configuration
122 file.
123
124 Default value is “/usr/local/etc/kamailio/handler.py”.
125
126 Example 1.1. Set load parameter
127...
128modparam("app_python3", "load", "/usr/local/etc/kamailio/myscript.py")
129...
130
1313.2. script_name (string)
132
133 This is same as "load" parameter, kept for backward compatibility with
134 the older versions of the module.
135
1363.3. mod_init_function (string)
137
138 The Python function to be executed by this module when it is
139 initialized by Kamailio.
140
141 Default value is “mod_init”.
142
143 Example 1.2. Set mod_init_function parameter
144...
145modparam("app_python3", "mod_init_function", "my_mod_init")
146...
147
1483.4. child_init_method (string)
149
150 The Python function to be executed by this module when a new worker
151 process (child) is initialized by Kamailio.
152
153 Default value is “child_init”.
154
155 Example 1.3. Set child_init_method parameter
156...
157modparam("app_python3", "child_init_method", "my_child_init")
158...
159
1604. Functions
161
162 4.1. python_exec(method [, args])
163
1644.1. python_exec(method [, args])
165
166 Execute the Python function with the name given by the parameter
167 'method'. Optionally can be provided a second string with parameters to
168 be passed to the Python function.
169
170 Both parameters can contain pseudo-variables.
171
172 Example 1.4. python_exec usage
173...
174python_exec("my_python_function");
175python_exec("my_python_function", "my_params");
176python_exec("my_python_function", "$rU");
177...
178
1795. RPC Commands
180
181 5.1. app_python.reload
182 5.2. app_python.api_list
183
1845.1. app_python.reload
185
186 IMPORTANT: this is not thread-safe. In your Python script do not use C
187 extensions with threads that call into apy_exec().
188
189 Marks the need to reload the Python script. The actual reload is done
190 in each worker when it next invokes a Python method. The module uses a
191 worker process lock to prevent recursive reloads.
192
193 This function only reloads the user script and creates a new script
194 object. It does not reinitialize the interpreter. E.g., references in
195 the old module remain if not redefined by the new version.
196
197 Name: app_python.reload
198
199 Parameters: none
200
201 Example:
202...
203kamcmd app_python.reload
204...
205
2065.2. app_python.api_list
207
208 List the functions available via Kemi framework.
209
210 Name: app_python.api_list
211
212 Parameters: none
213
214 Example:
215...
216kamcmd app_python.api_list
217...
218
README.TestCase-Loggers
1
2Example of using loggers in Python:
3
4loadmodule "app_python3.so"
5modparam("app_python3", "script_name", "/path/to/Loggers.py")
6modparam("app_python3", "mod_init_function", "mod_init")
7modparam("app_python3", "child_init_method", "child_init")
8
9...
10
11route
12{
13 python_exec("TestLoggers", "Test Message\n");
14 ...
15}
16
17