7

opensips开启python支持 - Mike_Zhang

 1 week ago
source link: https://www.cnblogs.com/MikeZhang/p/18154182/opCallPy20240423
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

python作为脚本语言,使用起来很方便,查了下opensips的文档,支持使用python脚本写逻辑代码。今天整理下CentOS7环境下opensips2.4.9的python模块笔记及使用示例,并提供运行效果视频和配套文件下载。

我将从以下几方面进行展开:

  • 模块安装说明

  • 模块参数说明

  • 模块函数说明

  • 模块使用示例

python模块官方文档:
https://opensips.org/docs/modules/2.4.x/python.html

300959-20240424000437772-2041728983.png

一、模块安装说明

1)安装python开发库

yum install  python-devel.x86_64

2)进入opensips源码目录

cd opensips-2.4.9

3)选中 python 模块

make menuconfig
300959-20240424000617566-233675105.png

 4)编译及安装opensips

make && make install
300959-20240424000654935-746905219.png

 CentOS7环境下源码安装opensips,可参考如下文章:

CentOS7环境源码安装opensips2.4.9

二、模块参数说明

模块文件: python.so
模块参数如下:

  • script_name
    用于设置python脚本的路径。

  • mod_init_function
    用于设置python脚本的初始化函数,默认是mod_init

  • child_init_method
    用于设置python脚本的子进程(子类)的初始化函数,默认是child_init

模块加载及配置数据库
文件:opensips.cfg

配置示例:

#### python module
loadmodule "python.so"
modparam("python", "script_name", "/usr/local/etc/opensips/op-test1.py")
#modparam("python", "mod_init_function", "module_initializer")
#modparam("python", "child_init_method", "child_initializer")

三、函数说明

1、cfg文件调用函数

函数: python_exec

python_exec(method_name [, extra_args])

该函数调用python脚本里面的方法。

2、python脚本内部调用函数

2.1 LM_ERR函数

在python里面,opensips作为独立的模块,导出了LM_ERR函数,定义如下(python_iface.c):

300959-20240424000846717-48678950.png

 该函数用于打印日志,示例如下:

LM_ERR('test msg from python')

2.2 sip msg相关属性及方法

属性定义如下(python_msgobj.c):

300959-20240424000928728-1463629985.png

 方法定义如下(python_msgobj.c):

300959-20240424000950777-1528994814.png
  • Type
    msg的类型,SIP_REQUEST 或 SIP_REPLY

  • Method
    msg的方法,比如:"INVITE"

  • Status
    msg的状态,只适用于reply

  • RURI
    msg的R-URI,只适用于request

  • src_address
    msg的源地址信息

  • dst_address
    msg的目的地址信息

  • copy()
    复制当前SIP消息

  • rewrite_ruri(uri)
    修改msg的request URI,仅适用于request

  • set_dst_uri(uri)
    修改msg的destination URI ,仅适用于request

  • getHeader(hdr)
    获取SIP头的具体内容

  • call_function(funName,...)
    调用其它模块的函数

四、使用示例

这里演示下python模块的加载,及使用该模块进行简单测试。

1、打印日志

使用LM_ERR函数进行日志打印。

示例代码(op-test1.py):

from OpenSIPS import LM_ERR

class ObjA():
    def child_init(self,rank):
        return 0

    def pytest1(self,var1):
          LM_ERR("test msg from python script")
          return 1

def mod_init():
    return ObjA()

opensips.cfg配置如下:

#### python module
loadmodule "python.so"
modparam("python", "script_name", "/usr/local/etc/opensips/op-test1.py")
#modparam("python", "mod_init_function", "module_initializer")
#modparam("python", "child_init_method", "child_initializer")

####### Routing Logic ########

# main request routing logic

route{
    if(python_exec("pytest1"))  {
        xlog("python test1 return true\n");
    }
    exit;

运行效果如下:

300959-20240424001144637-450822168.png

运行效果视频可从如下渠道获取:

关注微信公众号(聊聊博文,文末可扫码)后回复 2024042301 获取。

2、调用其它模块函数

示例代码及配置可从如下渠道获取:

关注微信公众号(聊聊博文,文末可扫码)后回复 20240423 获取。

运行效果如下:

300959-20240424001317651-83366359.png

 运行效果视频可从如下渠道获取:

关注微信公众号(聊聊博文,文末可扫码)后回复 2024042302 获取。

3、实现呼叫路由

这里演示下python调用drouting模块函数实现话务路由的功能。

机器列表:

freeswitchA :192.168.137.31:5080

opensips :192.168.137.33:5060

freeswitchB :192.168.137.32:5080

测试目标:将freeeswitchA呼叫opensips的通话路由到freeswitchB机器。

拨号方案等信息参考如下文章:

opensips使用drouting进行路由

示例代码及配置可从如下渠道获取:

关注微信公众号(聊聊博文,文末可扫码)后回复 20240423 获取。

运行效果如下:

300959-20240424001551287-1300212465.png

 抓包效果如下:

300959-20240424001622710-957834854.png

 运行效果视频可从如下渠道获取:

关注微信公众号(聊聊博文,文末可扫码)后回复 2024042303 获取。

五、资源下载

本文涉及资源,可以从如下途径获取:

关注微信公众号(聊聊博文,文末可扫码)后回复 20240423 获取。

300959-20240424001715420-1204782116.png

 好,就这么多了,别忘了点赞哈!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK