SoFunction
Updated on 2024-10-29

freeswitch open source communication python module introduction

1. Overview

freeswitchSupports business development in multiple languages, including C/C++, java, python, js, lua, Golang and more.

freeswitchin usingpythonWhen doing business development, there are two types of access, one is the ESL interface and the other is themod_pythonModule.

pythonThe ESL interface of thesocketsocket withfreeswitchPerform command interactions, including sending commands, command responses and event callbacks, similar to adding a third-party module externally to control fs behavior.

The ESL interface section is covered in detail in subsequent chapters.

Today we are going to introduce the fs internalmod_pythonLanguage Support Module, which allows us to usepythonScript development fs call control process.

2. Environment

centos: CentOS release 7.0 (Final) or above

freeswitch:v1.8.7

GCC:4.8.5

3. Install mod_python module

freeswitchWhen the source code is installed, it is not installed by defaultmod_pythonmodule, which requires us to go into the catalog and compile and install it.

cd /root/freeswitch-1.8.7/src/mod/languages/mod_python
make install

cd /usr/local/freeswitch/mod
ll -tr
-rwxr-xr-x. 1 root root 753208 9moon 14 10:41 mod_python.so
-rwxr-xr-x. 1 root root 1360 9moon 14 10:41 mod_python.la

4. python script

risescripts

vi /usr/local/freeswitch/scripts/
import freeswitch
def fsapi(session,stream,env,args):
    ("hello")
    ("info","test")


risescripts

vi /usr/local/freeswitch/scripts/
import freeswitch
def handler(session, args):
()
freeswitch.console_log("info","testCall\n")
("local_stream://moh")
(3000)
()

5. Configuration startup

modificationsfreeswitchModule Loading Configuration File

cd /usr/local/freeswitch/conf/autoload_configs
vi 
<!-- Languages -->
<load module="mod_python"/>


modificationsdialplandial plan

cd /usr/local/freeswitch/conf/dialplan
vi 
…
<include>
<context name="public">
<extension name="test">
<condition>
<action application="python" data="testapp"/>
</condition>
</extension>
…


activate (a plan)freeswitch

cd /usr/local/freeswitch/bin
./freeswitch -nonat
2021-09-14 10:57:06.392800 [NOTICE] mod_python.c:551 Python Framework Loading...
2021-09-14 10:57:06.405965 [CONSOLE] switch_loadable_module.c:1540 Successfully Loaded [mod_python]
2021-09-14 10:57:06.405982 [NOTICE] switch_loadable_module.c:292 Adding Application 'python'
2021-09-14 10:57:06.406012 [NOTICE] switch_loadable_module.c:315 Adding Chat Application 'python'
2021-09-14 10:57:06.406030 [NOTICE] switch_loadable_module.c:338 Adding API Function 'pyrun'
2021-09-14 10:57:06.406046 [NOTICE] switch_loadable_module.c:338 Adding API Function 'python'

6. Testing

existfreeswitchEnter the command on the command line using thepythonCall API interface

freeswitch@> python testapi
2021-09-14 11:13:56.068722 [NOTICE] mod_python.c:212 Invoking py module: testapi
2021-09-14 11:13:56.088701 [INFO] switch_cpp.cpp:1443 test
hello


In the log print, we can see thatmod_pythonThe module calls thetestapiscript and printed the "test"and"hello“。

Attention.pythonIn the call command, thepythonThe suffix ".py" should be removed from the script.

Through othersip serverSend a call request to the local machine to view the log:

2021-09-14 11:24:40.988720 [NOTICE] switch_channel.c:1114 New Channel sofia/external/[email protected] [73b09c9b-6a62-4372-839b-4c076af7dfc2]
2021-09-14 11:24:40.988720 [INFO] mod_dialplan_xml.c:637 Processing 10011 <10011>->10012 in context public
2021-09-14 11:24:40.988720 [NOTICE] mod_python.c:212 Invoking py module: testapp
2021-09-14 11:24:40.988720 [NOTICE] sofia_media.c:92 Pre-Answer sofia/external/[email protected]!
2021-09-14 11:24:40.988720 [NOTICE] switch_cpp.cpp:685 Channel [sofia/external/[email protected]] has been answered
2021-09-14 11:24:40.988720 [INFO] switch_cpp.cpp:1443 testCall
2021-09-14 11:24:40.988720 [WARNING] mod_local_stream.c:870 Unknown source moh, trying 'default'
2021-09-14 11:24:40.988720 [ERR] mod_local_stream.c:878 Unknown source default
2021-09-14 11:24:43.988724 [NOTICE] switch_cpp.cpp:733 Hangup sofia/external/[email protected] [CS_EXECUTE] [NORMAL_CLEARING]
2021-09-14 11:24:44.008687 [NOTICE] switch_core_session.c:1744 Session 2 (sofia/external/[email protected]) Ended
2021-09-14 11:24:44.008687 [NOTICE] switch_core_session.c:1748 Close Channel sofia/external/[email protected] [CS_DESTROY]


In the log printout, we can see that during the execution of the dialplan dialing plan through themod_pythonCalled "testapp“,The call is answered in the script, printing the log "testcall" and hang up after 3 seconds.

Summary:

freeswitchWhen doing business development, it is convenient to support multiple language access, and users can choose the access method and language according to their skill stack.

However, there are definitely differences in call performance from one language to another, and it is up to the user to test and evaluate the differences in actual use.

Up to this point in this article on the subject offreeswitch pythonModule details of the article is introduced to this, more relevantfreeswitch pythonFor module content please search my previous posts or continue to browse the related posts below I hope you will support me in the future!