diff --git a/src/modules/app_python3/README b/src/modules/app_python3/README index 54ea0697c78..518f72c71d2 100644 --- a/src/modules/app_python3/README +++ b/src/modules/app_python3/README @@ -1,7 +1,9 @@ -app_python Module +app_python3 Module Maxim Sobolev +Anthony Alba + Edited by Maxim Sobolev @@ -70,12 +72,20 @@ Chapter 1. Admin Guide 1. Overview - This module allows executing Python scripts from config file, exporting - functions to access the SIP message from Python. + 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 source tree located at - 'modules/app_python/python_examples/'. + module, look at the files inside the source tree located at + 'modules/app_python3/python_examples/'. 2. Dependencies @@ -91,7 +101,13 @@ Chapter 1. Admin Guide The following libraries or applications must be installed before running Kamailio with this module loaded: - * python-dev - Python devel library. + * 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. 3. Parameters @@ -109,7 +125,7 @@ Chapter 1. Admin Guide Example 1.1. Set load parameter ... -modparam("app_python", "load", "/usr/local/etc/kamailio/myscript.py") +modparam("app_python3", "load", "/usr/local/etc/kamailio/myscript.py") ... 3.2. script_name (string) @@ -126,7 +142,7 @@ modparam("app_python", "load", "/usr/local/etc/kamailio/myscript.py") Example 1.2. Set mod_init_function parameter ... -modparam("app_python", "mod_init_function", "my_mod_init") +modparam("app_python3", "mod_init_function", "my_mod_init") ... 3.4. child_init_method (string) @@ -138,7 +154,7 @@ modparam("app_python", "mod_init_function", "my_mod_init") Example 1.3. Set child_init_method parameter ... -modparam("app_python", "child_init_method", "my_child_init") +modparam("app_python3", "child_init_method", "my_child_init") ... 4. Functions @@ -170,8 +186,8 @@ python_exec("my_python_function", "$rU"); IMPORTANT: not functional yet (can crash a running instance, use it only for testing). - Marks the need to reload the js script. The actual reload is done by - every working process when the next call to KEMI config is executed. + Marks the need to reload the Python script. The actual reload is done + by every working process when the next call to KEMI config is executed. Name: app_python.reload diff --git a/src/modules/app_python3/README.TestCase-Loggers b/src/modules/app_python3/README.TestCase-Loggers index 8eea4b2ac5f..6a41034fb7b 100644 --- a/src/modules/app_python3/README.TestCase-Loggers +++ b/src/modules/app_python3/README.TestCase-Loggers @@ -1,10 +1,10 @@ Example of using loggers in Python: -loadmodule "app_python.so" -modparam("app_python", "script_name", "/path/to/Loggers.py") -modparam("app_python", "mod_init_function", "mod_init") -modparam("app_python", "child_init_method", "child_init") +loadmodule "app_python3.so" +modparam("app_python3", "script_name", "/path/to/Loggers.py") +modparam("app_python3", "mod_init_function", "mod_init") +modparam("app_python3", "child_init_method", "child_init") ... diff --git a/src/modules/app_python3/doc/Makefile b/src/modules/app_python3/doc/Makefile index 0d90e7f6150..2b64ef18ce8 100644 --- a/src/modules/app_python3/doc/Makefile +++ b/src/modules/app_python3/doc/Makefile @@ -1,4 +1,4 @@ -docs = app_python.xml +docs = app_python3.xml docbook_dir = ../../../../doc/docbook include $(docbook_dir)/Makefile.module diff --git a/src/modules/app_python3/doc/app_python.xml b/src/modules/app_python3/doc/app_python3.xml similarity index 80% rename from src/modules/app_python3/doc/app_python.xml rename to src/modules/app_python3/doc/app_python3.xml index e525f758be5..b911caf7294 100644 --- a/src/modules/app_python3/doc/app_python.xml +++ b/src/modules/app_python3/doc/app_python3.xml @@ -10,13 +10,17 @@ - app_python Module + app_python3 Module &kamailioname; Maxim Sobolev + + Anthony + Alba + Maxim Sobolev @@ -29,6 +33,6 @@ - + diff --git a/src/modules/app_python3/doc/app_python_admin.xml b/src/modules/app_python3/doc/app_python3_admin.xml similarity index 75% rename from src/modules/app_python3/doc/app_python_admin.xml rename to src/modules/app_python3/doc/app_python3_admin.xml index 50191f562dc..943a0065528 100644 --- a/src/modules/app_python3/doc/app_python_admin.xml +++ b/src/modules/app_python3/doc/app_python3_admin.xml @@ -17,13 +17,22 @@
Overview - This module allows executing Python scripts from config file, + 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 source tree located at - 'modules/app_python/python_examples/'. + this module, look at the files inside the source tree located at + 'modules/app_python3/python_examples/'.
@@ -49,7 +58,24 @@ - python-dev - Python devel library. + 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. @@ -58,7 +84,7 @@
Parameters -
+
<varname>load</varname> (string) The path to the file with Python code to be executed @@ -73,13 +99,13 @@ Set <varname>load</varname> parameter ... -modparam("app_python", "load", "/usr/local/etc/kamailio/myscript.py") +modparam("app_python3", "load", "/usr/local/etc/kamailio/myscript.py") ...
-
+
<varname>script_name</varname> (string) This is same as "load" parameter, kept for backward compatibility @@ -87,7 +113,7 @@ modparam("app_python", "load", "/usr/local/etc/kamailio/myscript.py")
-
+
<varname>mod_init_function</varname> (string) The Python function to be executed by this module when @@ -102,13 +128,13 @@ modparam("app_python", "load", "/usr/local/etc/kamailio/myscript.py") Set <varname>mod_init_function</varname> parameter ... -modparam("app_python", "mod_init_function", "my_mod_init") +modparam("app_python3", "mod_init_function", "my_mod_init") ...
-
+
<varname>child_init_method</varname> (string) The Python function to be executed by this module when @@ -123,7 +149,7 @@ modparam("app_python", "mod_init_function", "my_mod_init") Set <varname>child_init_method</varname> parameter ... -modparam("app_python", "child_init_method", "my_child_init") +modparam("app_python3", "child_init_method", "my_child_init") ... @@ -133,7 +159,7 @@ modparam("app_python", "child_init_method", "my_child_init")
Functions -
+
<function moreinfo="none">python_exec(method [, args])</function> @@ -160,7 +186,7 @@ python_exec("my_python_function", "$rU");
RPC Commands -
+
<function moreinfo="none">app_python.reload</function> @@ -169,7 +195,7 @@ python_exec("my_python_function", "$rU"); use it only for testing). - Marks the need to reload the js script. + Marks the need to reload the Python script. The actual reload is done by every working process when the next call to KEMI config is executed. @@ -186,7 +212,7 @@ python_exec("my_python_function", "$rU"); ...
-
+
<function moreinfo="none">app_python.api_list</function> diff --git a/src/modules/app_python3/python_examples/Loggers.py b/src/modules/app_python3/python_examples/Loggers.py index 8ae2ed0493b..24bd95d4dc7 100644 --- a/src/modules/app_python3/python_examples/Loggers.py +++ b/src/modules/app_python3/python_examples/Loggers.py @@ -46,61 +46,62 @@ PROC_TCP_MAIN PROC_UNIXSOCK PROC_ATTENDANT - PROC_INIT - PROC_NOCHLDINIT - PROC_SIPINIT - PROC_SIPRPC - PROC_MIN + PROC_INIT + PROC_NOCHLDINIT + PROC_SIPINIT + PROC_SIPRPC + PROC_MIN """ + class Loggers: def __init__(self): - pass + pass def __del__(self): - pass + pass def child_init(self, rank): - if rank == Ranks.PROC_MAIN: - Logger.LM_ERR("rank is PROC_MAIN") - elif rank == Ranks.PROC_TIMER: - Logger.LM_ERR("rank is PROC_TIMER") - elif rank == Ranks.PROC_RPC: - Logger.LM_ERR("rank is PROC_RPC") - elif rank == Ranks.PROC_FIFO: - Logger.LM_ERR("rank is PROC_FIFO") - elif rank == Ranks.PROC_TCP_MAIN: - Logger.LM_ERR("rank is PROC_TCP_MAIN") - elif rank == Ranks.PROC_UNIXSOCK: - Logger.LM_ERR("rank is PROC_UNIXSOCK") - elif rank == Ranks.PROC_ATTENDANT: - Logger.LM_ERR("rank is PROC_ATTENDANT") - elif rank == Ranks.PROC_INIT: - Logger.LM_ERR("rank is PROC_INIT") - elif rank == Ranks.PROC_NOCHLDINIT: - Logger.LM_ERR("rank is PROC_NOCHLDINIT") - elif rank == Ranks.PROC_SIPINIT: - Logger.LM_ERR("rank is PROC_SIPINIT") - elif rank == Ranks.PROC_SIPRPC: - Logger.LM_ERR("rank is PROC_SIPRPC") - elif rank == Ranks.PROC_MIN: - Logger.LM_ERR("rank is PROC_MIN") - - return 0 + if rank == Ranks.PROC_MAIN: + Logger.LM_ERR("rank is PROC_MAIN") + elif rank == Ranks.PROC_TIMER: + Logger.LM_ERR("rank is PROC_TIMER") + elif rank == Ranks.PROC_RPC: + Logger.LM_ERR("rank is PROC_RPC") + elif rank == Ranks.PROC_FIFO: + Logger.LM_ERR("rank is PROC_FIFO") + elif rank == Ranks.PROC_TCP_MAIN: + Logger.LM_ERR("rank is PROC_TCP_MAIN") + elif rank == Ranks.PROC_UNIXSOCK: + Logger.LM_ERR("rank is PROC_UNIXSOCK") + elif rank == Ranks.PROC_ATTENDANT: + Logger.LM_ERR("rank is PROC_ATTENDANT") + elif rank == Ranks.PROC_INIT: + Logger.LM_ERR("rank is PROC_INIT") + elif rank == Ranks.PROC_NOCHLDINIT: + Logger.LM_ERR("rank is PROC_NOCHLDINIT") + elif rank == Ranks.PROC_SIPINIT: + Logger.LM_ERR("rank is PROC_SIPINIT") + elif rank == Ranks.PROC_SIPRPC: + Logger.LM_ERR("rank is PROC_SIPRPC") + elif rank == Ranks.PROC_MIN: + Logger.LM_ERR("rank is PROC_MIN") + + return 0 def TestLoggers(self, msg, args): - Logger.LM_GEN1(Logger.L_INFO, "Loggers.py: LM_GEN1: msg: %s" % str(args)) - Logger.LM_GEN2(Logger.L_INFO, Logger.DEFAULT_FACILITY, "Loggers.py: LM_GEN2: msg: %s" % str(args)) - Logger.LM_ALERT( "Loggers.py: LM_ALERT: msg: %s" % str(args)) - Logger.LM_CRIT( "Loggers.py: LM_CRIT: msg: %s" % str(args)) - Logger.LM_ERR( "Loggers.py: LM_ERR: msg: %s" % str(args)) - Logger.LM_WARN( "Loggers.py: LM_WARN: msg: %s" % str(args)) - Logger.LM_NOTICE( "Loggers.py: LM_NOTICE: msg: %s" % str(args)) - Logger.LM_INFO( "Loggers.py: LM_INFO: msg: %s" % str(args)) - Logger.LM_DBG( "Loggers.py: LM_DBG: msg: %s" % str(args)) - return 1 + Logger.LM_GEN1(Logger.L_INFO, "Loggers.py: LM_GEN1: msg: %s" % str(args)) + Logger.LM_GEN2(Logger.L_INFO, Logger.DEFAULT_FACILITY, "Loggers.py: LM_GEN2: msg: %s" % str(args)) + Logger.LM_ALERT( "Loggers.py: LM_ALERT: msg: %s" % str(args)) + Logger.LM_CRIT( "Loggers.py: LM_CRIT: msg: %s" % str(args)) + Logger.LM_ERR( "Loggers.py: LM_ERR: msg: %s" % str(args)) + Logger.LM_WARN( "Loggers.py: LM_WARN: msg: %s" % str(args)) + Logger.LM_NOTICE( "Loggers.py: LM_NOTICE: msg: %s" % str(args)) + Logger.LM_INFO( "Loggers.py: LM_INFO: msg: %s" % str(args)) + Logger.LM_DBG( "Loggers.py: LM_DBG: msg: %s" % str(args)) + return 1 def mod_init(): return Loggers() diff --git a/src/modules/app_python3/python_examples/TestCase_Traceback.py b/src/modules/app_python3/python_examples/TestCase_Traceback.py index 6a673a06e83..1457d9125ac 100644 --- a/src/modules/app_python3/python_examples/TestCase_Traceback.py +++ b/src/modules/app_python3/python_examples/TestCase_Traceback.py @@ -10,28 +10,28 @@ class Loggers: def __init__(self): - pass + pass def __del__(self): - pass + pass def child_init(self, y): - return 0 + return 0 def BuggyCode_lvl5(self, a): - a / 0 + a / 0 def BuggyCode_lvl4(self, a): - return self.BuggyCode_lvl5(a) + return self.BuggyCode_lvl5(a) def BuggyCode_lvl3(self, a): - return self.BuggyCode_lvl4(a) + return self.BuggyCode_lvl4(a) def BuggyCode_lvl2(self, a): - return self.BuggyCode_lvl3(a) + return self.BuggyCode_lvl3(a) def BuggyCode(self, a, b=None): - return self.BuggyCode_lvl2(a) + return self.BuggyCode_lvl2(a) diff --git a/src/modules/app_python3/python_examples/handler.py b/src/modules/app_python3/python_examples/handler.py index 6e3eb5edcc6..1af6558097e 100644 --- a/src/modules/app_python3/python_examples/handler.py +++ b/src/modules/app_python3/python_examples/handler.py @@ -1,6 +1,7 @@ import sys from Router import LM_ERR + class test: def __init__(self): LM_ERR('test.__init__\n') @@ -10,7 +11,7 @@ def child_init(self, y): return 0 def handler(self, msg, args): - LM_ERR('test.handler(%s, %s)\n' % (msg.Type, str(arg))) + LM_ERR('test.handler(%s, %s)\n' % (msg.Type, str(args))) if msg.Type == 'SIP_REQUEST': if msg.Method == 'INVITE': msg.rewrite_ruri('sip:0022@192.168.2.24:5073') @@ -22,5 +23,6 @@ def handler(self, msg, args): msg.call_function('append_hf', 'This-is: test\r\n') return 1 + def mod_init(): return test()