freeswitch開源通信 python模塊介紹
1、概述
freeswitch支持多種語言的業(yè)務(wù)開發(fā),包括C/C++,java,python,js,lua,Golang等等。
freeswitch在使用python做業(yè)務(wù)開發(fā)時(shí),有倆種接入方式,一種是ESL接口,另一種是mod_python模塊。
python的ESL接口是通過socket套接字與freeswitch進(jìn)行命令交互,包括發(fā)送命令、命令響應(yīng)和事件回調(diào)等,類似于在外部增加一個第三方模塊控制fs行為。ESL接口部分會在后續(xù)的章節(jié)中詳細(xì)介紹。
今天我們要介紹的是fs內(nèi)部
的mod_python語言支持模塊,該模塊允許我們使用python腳本開發(fā)fs呼叫控制流程。
2、環(huán)境
centos:CentOS release 7.0 (Final)或以上版本
freeswitch:v1.8.7
GCC:4.8.5
3、安裝mod_python模塊
freeswitch源碼安裝時(shí),默認(rèn)不安裝mod_python模塊,需要我們進(jìn)入目錄編譯安裝。
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 9月 14 10:41 mod_python.so -rwxr-xr-x. 1 root root 1360 9月 14 10:41 mod_python.la
4、python腳本
增加testapi.py腳本
vi /usr/local/freeswitch/scripts/testapi.py
import freeswitch
def fsapi(session,stream,env,args):
stream.write("hello")
freeswitch.consoleLog("info","test")
增加testapp.py腳本
vi /usr/local/freeswitch/scripts/testapp.py
import freeswitch
def handler(session, args):
session.answer()
freeswitch.console_log("info","testCall\n")
session.streamFile("local_stream://moh")
freeswitch.msleep(3000)
session.hangup()
5、配置啟動
修改freeswitch模塊加載配置文件
cd /usr/local/freeswitch/conf/autoload_configs vi modules.conf.xml <!-- Languages --> <load module="mod_python"/>
修改dialplan撥號計(jì)劃
cd /usr/local/freeswitch/conf/dialplan vi public.xml … <include> <context name="public"> <extension name="test"> <condition> <action application="python" data="testapp"/> </condition> </extension> …
啟動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、測試
在freeswitch命令行中輸入命令,使用python調(diào)用API接口
freeswitch@localhost.localdomain> 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
在日志打印中,我們可以看到mod_python模塊調(diào)用了testapi腳本,并打印了“test“和”hello“。
注意事項(xiàng),python調(diào)用命令中,python腳本的后綴“.py“要去掉。
通過其他sip server發(fā)送呼叫請求到本機(jī),查看日志:
2021-09-14 11:24:40.988720 [NOTICE] switch_channel.c:1114 New Channel sofia/external/10011@192.168.0.110 [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/10011@192.168.0.110! 2021-09-14 11:24:40.988720 [NOTICE] switch_cpp.cpp:685 Channel [sofia/external/10011@192.168.0.110] 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/10011@192.168.0.110 [CS_EXECUTE] [NORMAL_CLEARING] 2021-09-14 11:24:44.008687 [NOTICE] switch_core_session.c:1744 Session 2 (sofia/external/10011@192.168.0.110) Ended 2021-09-14 11:24:44.008687 [NOTICE] switch_core_session.c:1748 Close Channel sofia/external/10011@192.168.0.110 [CS_DESTROY]
在日志打印中,我們可以看到在dialplan撥號計(jì)劃的執(zhí)行過程中,通過mod_python調(diào)用了“testapp“,testapp.py腳本中應(yīng)答了這通呼叫,打印日志”testcall“,并在3秒后掛機(jī)。
總結(jié):
freeswitch做業(yè)務(wù)開發(fā)時(shí),支持多種語言接入,很方便,用戶可以根據(jù)自己的技能棧來選擇接入方式和語言。
但是,不同語言在呼叫性能上肯定有差異,這個就需要用戶自己來測試并評估實(shí)際使用中的差別了。
到此這篇關(guān)于有關(guān)freeswitch python模塊的詳情介紹的文章就介紹到這了,更多相關(guān)freeswitch python模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python單元測試工具doctest和unittest使用解析
這篇文章主要介紹了Python單元測試工具doctest和unittest使用解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
python3 中文亂碼與默認(rèn)編碼格式設(shè)定方法
今天小編就為大家分享一篇python3 中文亂碼與默認(rèn)編碼格式設(shè)定方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
python3.5 email實(shí)現(xiàn)發(fā)送郵件功能
這篇文章主要為大家詳細(xì)介紹了python3.5 email實(shí)現(xiàn)發(fā)送郵件功能,包含txt、圖片、HTML、附件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Python自定義函數(shù)實(shí)現(xiàn)求兩個數(shù)最大公約數(shù)、最小公倍數(shù)示例
這篇文章主要介紹了Python自定義函數(shù)實(shí)現(xiàn)求兩個數(shù)最大公約數(shù)、最小公倍數(shù),結(jié)合實(shí)例形式分析了Python求解兩個數(shù)最大公約數(shù)與最小公倍數(shù)相關(guān)原理與算法實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-05-05
python不相等的兩個字符串的 if 條件判斷為True詳解
這篇文章主要介紹了python不相等的兩個字符串的 if 條件判斷為True詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
python服務(wù)器端收發(fā)請求的實(shí)現(xiàn)代碼
這篇文章主要介紹了python服務(wù)器端收發(fā)請求的實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-09-09
pandas中DataFrame字典互轉(zhuǎn)的實(shí)現(xiàn)
在數(shù)據(jù)處理和分析中,Pandas是一個非常強(qiáng)大的Python庫,本文主要介紹了pandas中DataFrame字典互轉(zhuǎn)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-04-04
Matplotlib之解決plt.savefig()保存多張圖片有重疊的問題
這篇文章主要介紹了Matplotlib之解決plt.savefig()保存多張圖片有重疊的問題,具有很好的參考價(jià)值,希望對大家有所幫助,2023-09-09

