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)等,類似于在外部增加一個(gè)第三方模塊控制fs行為。ESL接口部分會(huì)在后續(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、配置啟動(dòng)
修改freeswitch
模塊加載配置文件
cd /usr/local/freeswitch/conf/autoload_configs vi modules.conf.xml <!-- Languages --> <load module="mod_python"/>
修改dialplan
撥號(hào)計(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> …
啟動(dòng)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
命令行中輸入命令,使用pytho
n調(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撥號(hào)計(jì)劃的執(zhí)行過程中,通過mod_python
調(diào)用了“testapp
“,testapp.py
腳本中應(yīng)答了這通呼叫,打印日志”testcall
“,并在3秒后掛機(jī)。
總結(jié):
freeswitch
做業(yè)務(wù)開發(fā)時(shí),支持多種語言接入,很方便,用戶可以根據(jù)自己的技能棧來選擇接入方式和語言。
但是,不同語言在呼叫性能上肯定有差異,這個(gè)就需要用戶自己來測試并評(píng)估實(shí)際使用中的差別了。
到此這篇關(guān)于有關(guān)freeswitch python
模塊的詳情介紹的文章就介紹到這了,更多相關(guān)freeswitch python
模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python面向?qū)ο蠖嗑€程爬蟲爬取搜狐頁面的實(shí)例代碼
這篇文章主要介紹了python面向?qū)ο蠖嗑€程爬蟲爬取搜狐頁面的實(shí)例代碼,需要的朋友可以參考下2018-05-05Python中zip()函數(shù)用法實(shí)例教程
這篇文章主要介紹了Python中zip()函數(shù)用法實(shí)例教程,對Python初學(xué)者有一定的借鑒價(jià)值,需要的朋友可以參考下2014-07-07Python-OpenCV中的cv2.inpaint()函數(shù)的使用
大多數(shù)人會(huì)在家里放一些舊的退化照片,上面有一些黑點(diǎn),一些筆畫等。你有沒有想過恢復(fù)它?本文就來介紹一下方法,感興趣的可以了解一下2021-06-06python在html中插入簡單的代碼并加上時(shí)間戳的方法
今天小編就為大家分享一篇python在html中插入簡單的代碼并加上時(shí)間戳的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10python學(xué)習(xí)手冊中的python多態(tài)示例代碼
多態(tài)是面向?qū)ο笳Z言的一個(gè)基本特性,多態(tài)意味著變量并不知道引用的對象是什么,根據(jù)引用對象的不同表現(xiàn)不同的行為方式,下面使用一個(gè)示例學(xué)習(xí)他的使用方法2014-01-01