1

I am currently using an older version of Kamailio in production. I want to be able to write some custom logic, and v5 allows me to do that in Python using the KEMI interpreter - so I'm interested in upgrading. However I'm encountering something I've never seen before..

The example script on the Kamailio website shows this sample .py script:

import sys
import KSR as KSR

def dumpObj(obj):
    for attr in dir(obj):
        KSR.info("obj.%s = %s\n" % (attr, getattr(obj, attr)))

def mod_init():
    KSR.info("===== from Python mod init\n")
    # dumpObj(KSR)
    return kamailio()

class kamailio:
    def __init__(self):
        KSR.info('===== kamailio.__init__\n')

    def child_init(self, rank):
        KSR.info('===== kamailio.child_init(%d)\n' % rank)
        return 0

    def ksr_request_route(self, msg):
        KSR.info("===== request - from kamailio python script\n")
        KSR.setdsturi("sip:alice@127.0.0.1:5080")
        KSR.tm.t_on_branch("ksr_branch_route_one")
        KSR.tm.t_on_reply("ksr_onreply_route_one")
        KSR.tm.t_on_failure("ksr_failure_route_one")
        KSR.sl.send_reply(100, "Trying")
        if KSR.tm.t_relay() < 0 :
            KSR.sl.send_reply(500, "Server error")
        return 1

    def ksr_reply_route(self, msg):
        KSR.info("===== response - from kamailio python script\n")
        return 1

    def ksr_onsend_route(self, msg):
        KSR.info("===== onsend route - from kamailio python script\n")
        return 1

    def ksr_branch_route_one(self, msg):
        KSR.info("===== branch route - from kamailio python script\n")
        return 1

    def ksr_onreply_route_one(self, msg):
        KSR.info("===== onreply route - from kamailio python script\n")
        return 1

    def ksr_failure_route_one(self, msg):
        KSR.info("===== failure route - from kamailio python script\n")
        return 1

So my question is: Where does "KSR" come from? It almost seems like Kamailio is magically hanging that library out there for Python to import. I can't find any way to pip install it, so I'm curious to hear what the deal is, if anyone knows.

Thanks!

Joel
  • 199
  • 2
  • 10

1 Answers1

1

KSR comes from the kamailio.cfg file. You can't run the sample.py file by itself.

Basically, if you want to use the Python KEMI interpreter, you have to install the app_python3.so Kamailio module. Then in kamailio.cfg (Kamailio's config file) you loadmodule "app_python3.so" and in the routing logic you import your sample.py file.

modparam("app_python3", "load", "/etc/kamailio/sample.py")

cfgengine "python"

So you start Kamailio on it's own and it will handle running sample.py.

For more information, I recommend this article: https://nickvsnetworking.com/kamailio-bytes-python-sip-with-kemi/

Olivier
  • 11
  • 1