[OpenSIPS-Users] python module - python_exec() behaviour in opensips

Tomi Hakkarainen tpaivaa at gmail.com
Sun May 17 06:36:37 EST 2020


Hi,

just another guess it uses version it had while installing opensips and was executed with python keyword.

this came based on this
https://github.com/OpenSIPS/opensips/blob/master/modules/python/Makefile

not sure you could point it to use other python version with environment variables but is one option to search.

do you run the tests on the box with python3 prefix or python?

maybe it would be as easy as try to point the system run python3 when you run .py scripts with python keyword.

Really hope you get what I mean :)

based on this #!/usr/bin/python3
your system find/run python3 with python3 not when running python

Tomi

On 17. May 2020, at 8.53, Gordon Yeong <anexiole at gmail.com> wrote:


very interesting.
 I looked at the code in handler.py and adjusted my python class.

I ran opensips  (master branch) and found 2 issues:

1) python3 is not being run. How do i specify to opensips to use python 3?

May 17 15:45:46 [21373] Just done the enum query...The ru value is (sip:service at 192.168.136.133:5060)
Calling get_rn...
May 17 15:45:46 [21373] ERROR:python:python_handle_exception: python_exec("get_rn"): Unhandled exception in the Python code:
May 17 15:45:46 [21373] ERROR:python:python_handle_exception:   Traceback (most recent call last):
May 17 15:45:46 [21373] ERROR:python:python_handle_exception:     File "/home/gordon/opensips_proxy_source/modules/python/dumbat.py", line 61, in get_rn
    return re.match('.*rn\=\+6114([\d]{2}).*', str(raw_enum_result) ).group(1);
May 17 15:45:46 [21373] ERROR:python:python_handle_exception:     File "/usr/lib64/python2.7/re.py", line 137, in match   <---- python2.7 not 3
May 17 15:45:46 [21373] ERROR:python:python_handle_exception:   TypeError: an integer is required
May 17 15:45:46 [21373] AFTER EXTRACTION (get_rn)............ The value of the rn code is [-1]
May 17 15:45:46 [21373] incoming reply



2) the type error seems to be coming from re.py . I will trace more later but we need to make my opensips use python 3.


So, how do I specify python3 to be used in my opensips?


Regards,
Gordon Yeong


On Sat, 16 May 2020 at 21:09, Gordon Yeong <anexiole at gmail.com> wrote:
> Tomi
> 
> I will try it on monday. Let u know. Thank you, stay safw and have a good weekend :)
> 
> On Sat, 16 May 2020, 8:08 pm Tomi Hakkarainen, <tpaivaa at gmail.com> wrote:
>> Hi,
>> My second guess :)
>> 
>> Check this
>> https://github.com/OpenSIPS/opensips/blob/master/modules/python/handler.py
>> 
>> Differs from your version… Can these differences cause the failure?
>> 
>> The __init__ located inside class
>> 
>> And the mod_init located last in the script (does the order count for anything in python?)
>> 
>> Also called handler function always returns 1 not the init
>> 
>> Tomi
>> 
>> On 16. May 2020, at 11.20, Gordon Yeong <anexiole at gmail.com> wrote:
>> 
>> Yeah I looked at that and ran my code on the python3 interpreter - it worked. 
>> Then i put it into a class - it worked.
>>  And then only did I put it (the class) to use in opensips..
>> 
>> Had there been an issue, python3 would have kicked and scream which it did not.
>> 
>>  No issue with the regexp. 
>>  Thank you
>> 
>> Gordon
>> 
>> On Sat, 16 May 2020 at 18:03, Tomi Hakkarainen <tpaivaa at gmail.com> wrote:
>>> Hi,
>>> 
>>> not sure has this nothing to do with the error but game to my eye trying to figure out what this bunch of code is really doing...
>>> 
>>> from python re docs:
>>> 
>>> Regular expressions use the backslash character ('\') to indicate special forms or to allow special characters to be used without invoking their special meaning. This collides with Python’s usage of the same character for the same purpose in string literals; for example, to match a literal backslash, one might have to write '\\\\' as the pattern string, because the regular expression must be \\, and each backslash must be expressed as \\ inside a regular Python string literal. Also, please note that any invalid escape sequences in Python’s usage of the backslash in string literals now generate a DeprecationWarning and in the future this will become a SyntaxError. This behaviour will happen even if it is a valid escape sequence for a regular expression.
>>> 
>>> The solution is to use Python’s raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'. So r"\n" is a two-character string containing '\' and 'n', while "\n"is a one-character string containing a newline.
>>> 
>>> 
>>> Tomi
>>> 
>>> On 16. May 2020, at 1.47, Gordon Yeong <anexiole at gmail.com> wrote:
>>> 
>>> 
>>> It's passed the init. If i did a print (LM_ERR) In the first line of init(), it doesn't show that print. 
>>> 
>>> If i  did a print (LM_ERR)a static string   in the get_rn()  before the line that calls compile(), i noticed that it does print out.  And if i tried to print a static string right after the compile() call,  it doesn't print and it calls TypeError.
>>> 
>>> That's why i suspect something is wrong bit can't tell exactly what....
>>> 
>>> 
>>> On Fri, 15 May 2020, 11:21 pm Ben Newlin, <Ben.Newlin at genesys.com> wrote:
>>>> Gordon,
>>>> 
>>>>  
>>>> 
>>>> Are you sure the TypeError is not occurring in mod_init? It looks like you are trying to do a print on a Class definition.
>>>> 
>>>>  
>>>> 
>>>> Ben Newlin
>>>> 
>>>>  
>>>> 
>>>> From: Users <users-bounces at lists.opensips.org> on behalf of Gordon Yeong <anexiole at gmail.com>
>>>> Reply-To: OpenSIPS users mailling list <users at lists.opensips.org>
>>>> Date: Friday, May 15, 2020 at 1:54 AM
>>>> To: OpenSIPS users mailling list <users at lists.opensips.org>
>>>> Subject: [OpenSIPS-Users] python module - python_exec() behaviour in opensips
>>>> 
>>>>  
>>>> 
>>>> hi guys,
>>>> 
>>>>  Using opensips 3.0 and python3 here.
>>>> 
>>>>  
>>>> 
>>>>  I have a script which parses a number in a string (r'.*rn=\+6114(\d{2})').
>>>> 
>>>>  
>>>> 
>>>> Script is as follows:
>>>> 
>>>> ---------- start ---------------
>>>> 
>>>>  
>>>> 
>>>>  
>>>> 
>>>> #!/usr/bin/python3
>>>> import re;
>>>> import pprint;
>>>> 
>>>> 
>>>> class test:
>>>> 
>>>>     def get_rn():
>>>>         rnRegExp = rnRegExp = re.compile(r'.*rn=\+6114(\d{2})');
>>>>         doit = rnRegExp.match( str("sip:+61386947785;npdi;rn=+611411 at tipt.ns.zippi.vocus.com;user=phone"));
>>>> 
>>>>         if doit:
>>>>             pp = pprint.PrettyPrinter(indent=4);
>>>>             return doit.group(1);
>>>>         else:
>>>>             return None;
>>>> 
>>>> result = test.get_rn();
>>>> 
>>>> if result is None:
>>>>     print("Got no match\n");
>>>> else:
>>>>     print("Result is " + result);
>>>> 
>>>>  
>>>> 
>>>> ----------- end --------------
>>>> 
>>>>  
>>>> 
>>>> In my opensips setup, I have the function above looking like this:
>>>> 
>>>>  
>>>> 
>>>> ---- start--------
>>>> 
>>>> 
>>>> from OpenSIPS import LM_ERR
>>>> import re;
>>>> import pprint;
>>>> 
>>>> def mod_init():
>>>>     print("In mod_init, SIP MSG is : ");
>>>>     pp = pprint.PrettyPrinter(indent=4)
>>>>     pp.pprint(SIPMsg);
>>>>     return SIPMsg();
>>>> 
>>>> def __init__():
>>>>     return 1;
>>>> 
>>>> import re;
>>>> import pprint;
>>>> 
>>>> class SIPMsg:
>>>> 
>>>>     def child_init(self, rank):
>>>>         return 0
>>>> 
>>>>     def get_rn(self, msg, raw_enum_result):
>>>>         try:
>>>>              get_rn = re.compile(r'.*rn=\+6114(\d{2})');
>>>> 
>>>>              rn = get_rn.match(str(raw_enum_result));
>>>>              return rn.group(1);
>>>>         except AttributeError:
>>>>              # Invalid input string
>>>>              LM_ERR(" attribute ERRor" );
>>>>              return '';
>>>>         except TypeError:
>>>>              # Invalid input string
>>>>              LM_ERR(" Type Error" );
>>>>              return '';
>>>>         else:
>>>>              LM_ERR(" general ERRor" );
>>>>              return '';
>>>> 
>>>>  
>>>> 
>>>> ----- end ------
>>>> 
>>>>  
>>>> 
>>>>  
>>>> 
>>>>  
>>>> 
>>>> In my opensips config, I call the get_rn() function via python_exec() as below.
>>>> 
>>>>  
>>>> 
>>>> $var(rn_code)=python_exec("get_rn", $var(raw_string));
>>>> 
>>>>  
>>>> 
>>>> I'm facing an issue here because when I run the script by itself in a command line, it works.
>>>> 
>>>>  No "Error: Text Type" but when my opensips run, it keeps hitting a TypeError.
>>>> 
>>>>  I have even explicitly fed the " rn = get_rn.match(str(raw_enum_result));" with an explicit string (ie. I dont send use the value passed in from the argument BUT instead, just feed it an explicit string value) and yet the same issue occurs.
>>>> 
>>>>  
>>>> 
>>>> Does processing control belong directly to python3 when we call python_exec() or is there something funny going on here?
>>>> 
>>>>  
>>>> 
>>>> Thank you
>>>> 
>>>>  
>>>> 
>>>>  
>>>> 
>>>> Regards,
>>>> Gordon 
>>>> 
>>>> _______________________________________________
>>>> Users mailing list
>>>> Users at lists.opensips.org
>>>> http://lists.opensips.org/cgi-bin/mailman/listinfo/users
>>> _______________________________________________
>>> Users mailing list
>>> Users at lists.opensips.org
>>> http://lists.opensips.org/cgi-bin/mailman/listinfo/users
>>> _______________________________________________
>>> Users mailing list
>>> Users at lists.opensips.org
>>> http://lists.opensips.org/cgi-bin/mailman/listinfo/users
>> _______________________________________________
>> Users mailing list
>> Users at lists.opensips.org
>> http://lists.opensips.org/cgi-bin/mailman/listinfo/users
>> 
>> _______________________________________________
>> Users mailing list
>> Users at lists.opensips.org
>> http://lists.opensips.org/cgi-bin/mailman/listinfo/users
_______________________________________________
Users mailing list
Users at lists.opensips.org
http://lists.opensips.org/cgi-bin/mailman/listinfo/users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.opensips.org/pipermail/users/attachments/20200517/afcbdf9f/attachment-0001.html>


More information about the Users mailing list