[Asterisk-Users] agi problem
Moises Silva
moises.silva at gmail.com
Tue May 3 19:55:28 MST 2005
Hi Smadi. I have tested the script in my box and seems to be working
just fine. By the way, in any programming language, the way to get the
asterisk environment vars, and in general all the communication is
through STDIN, STDOUT and STDERR
try to give us more info so we can help you
best regards
On 5/3/05, M.N.A.Smadi <smadim2 at grads.ece.mcmaster.ca> wrote:
> hi;
>
> am using an agi script to do some call forwarding. I got the following
> pyton script off http://home.cogeco.ca/~camstuff/agi.html:
> #!/usr/bin/python
>
> import sys,string
>
> class AGI:
> """
> Class AGI facilitates writing AGI scripts in Python.
>
> Exported functions:
>
> Write(message):
> Writes message to Asterisk Console.
>
> Cmd(command):
> Send command to Asterisk, read result.
> The result is a two element tuple:
> [0] A text string giving the entire result returned by
> Asterisk
> [1] The "result=" integer extracted from the result line
>
> If we get an unhappy response from Asterisk or if the result
> returned to the right of the equals sign is not an integer we
> issue an error message and terminate the script.
>
> Exported Variables:
>
> env
> Dictionary containing the various environment startup items, as
> passed to us by Asterisk.
> """
>
> def Write(self,data):
> """
> Write unbuffered line output to STDERR.
> Ensures data is flushed out.
> """
> sys.stderr.write(str(data) + "\n")
> sys.stderr.flush()
>
> def Cmd(self,command):
> """
> Send an AGI command to Asterisk; read back the response.
> The result is a two element tuple:
> [0] A text string giving the entire result returned by Asterisk
> [1] The "result=" integer extracted from the result line
>
> If we get an unhappy response from Asterisk or if the result
> returned to the right of the equals sign is not an integer we
> issue an error message and terminate the script.
> """
> try:
> sys.stdout.write(str(command) + "\n")
> sys.stdout.flush()
> Response = sys.stdin.readline()
> if Response[:11] <> '200 result=':
> #we didn't get a happy response for AGI
> raise "AgiError"
> #accumulate integer portion
> J = 11
> while J < len(Response) and (Response[J] in '0123456789'):
> J += 1
> res = Response[11:J]
> try:
> ResInt = int(res)
> except ValueError:
> #there is no integer immediatly to the right of the
> equal sign
> raise "AgiError"
> except "AgiError":
> self.Error('Unexpected response to AGI command.')
> self.Error('Command: %s'%command)
> self.Error('Response: %s'%Response)
> self.Error('Script terminated.')
> sys.exit()
> return (Response,ResInt)
>
> def __init__(self):
> """
> Read until blank line to get the AGI environment.
> The lines read are used to build the dictionary 'env'.
> """
> self.env = {1:'moe',2:'joe'}
> while 1:
> line = string.strip(sys.stdin.readline())
> if line == '':
> #blank line signals end
> break
> key,data = string.split(line,':')
> key = string.strip(key)
> data = string.strip(data)
> if key <> '':
> self.env[key] = data
>
> =================
>
> however i think there is something wrong with initialization code, namely:
>
> def __init__(self):
> """
> Read until blank line to get the AGI environment.
> The lines read are used to build the dictionary 'env'.
> """
> self.env = {1:'moe',2:'joe'}
> while 1:
> line = string.strip(sys.stdin.readline())
> if line == '':
> #blank line signals end
> break
> key,data = string.split(line,':')
> key = string.strip(key)
> data = string.strip(data)
> if key <> '':
> self.env[key] = data
>
> because when i replace that piece of code with my own hardcode variables
> things work just fine.
>
> Now my question is does anybody have a clue why this code is not
> working? or alternatively how can i read the environment variables in
> python?
>
> thanks
> m.smadi
> _______________________________________________
> Asterisk-Users mailing list
> Asterisk-Users at lists.digium.com
> http://lists.digium.com/mailman/listinfo/asterisk-users
> To UNSUBSCRIBE or update options visit:
> http://lists.digium.com/mailman/listinfo/asterisk-users
>
--
"Su nombre es GNU/Linux, no solamente Linux, mas info en http://www.gnu.org"
More information about the asterisk-users
mailing list