pythonasteriskagifastagi

Setting up handlers in pyst2 fastagi code


I am trying to create a fastagi server for executing some agi scripts. I'm using pyst2 to setup fast agi server. the script running fast agi server is as follows:

#!/usr/bin/env python

"""
.. module:: fastagi
   :synopsis: FastAGI service for Asterisk

Requires modified pyst2 to support reading stdin/out/err

 Copyright 2011 VOICE1, LLC
 By: Ben Davis <ben@voice1-dot-me>

Specification
-------------
"""

import sys
import SocketServer
import asterisk.agi
# import pkg_resources
# PYST_VERSION = pkg_resources.get_distribution("pyst2").version

__verison__ = 0.1

#TODO: Read options from config file.
HOST, PORT = "127.0.0.1", 4573

class FastAGI(SocketServer.StreamRequestHandler):
    # Close connections not finished in 5seconds.
    timeout = 5
    def handle(self):
        try:
            agi=asterisk.agi.AGI(stdin=self.rfile, stdout=self.wfile, 
stderr=sys.stderr)
            agi.verbose("pyst2: FastAGI on: {}:{}".format(HOST, PORT))
        except TypeError as e:
            sys.stderr.write('Unable to connect to agi://{} 
{}\n'.format(self.client_address[0], str(e)))
        except SocketServer.socket.timeout as e:
            sys.stderr.write('Timeout receiving data from 
{}\n'.format(self.client_address))
        except SocketServer.socket.error as e:
            sys.stderr.write('Could not open the socket. Is someting else 
listening on this port?\n')
        except Exception as e:
            sys.stderr.write('An unknown error: {}\n'.format(str(e)))

if __name__ == "__main__":
    # server = SocketServer.TCPServer((HOST, PORT), FastAGI)
    server = SocketServer.ForkingTCPServer((HOST, PORT), FastAGI)

    # Keep server running until CTRL-C is pressed.
    server.serve_forever()

Its ok when I use the following context.

exten => 123,1,agi(agi://FASTAGI_IP_address)

but I want to have more than 1 scripts like exten => 123,1,agi(agi://FASTAGI_IP_address/handler_name)

I don't know how to use some handler names in fast agi server codes. I'm new to python so I will be very thankful if I can have some clear guidance on how to add extra handlers in fastagi code.


Solution

  • Found the solution. on the asterisk server I was contacting the Fastagi server using below dialplan:

    extension=> s,1,AGI(agi://server-address/handler)
    

    the "handler" showing in above code is returning to fastagi server as "agi.env['agi_network_script']".

    a simple example on how to use handler is like the below code:

    class FastAGI(SocketServer.StreamRequestHandler):
    def handle(self):
        try:
        agi=asterisk.agi.AGI(stdin=self.rfile, stdout=self.wfile, stderr=sys.stderr)
        handler = agi.env['agi_network_script']
    ###   Managing Handler Section   ###
    
            if handler == 'handler1':
                // Do whatever you wanna do with handler2
            elif handler == 'handler2':
                // Do whatever you wanna do with handler2
        except TypeError as e:
            sys.stderr.write('Unable to connect to agi://{} {}\n'.format(self.client_address[0], str(e)))
        except SocketServer.socket.timeout as e:
            sys.stderr.write('Timeout receiving data from {}\n'.format(self.client_address))
        except SocketServer.socket.error as e:
            sys.stderr.write('Could not open the socket. Is someting else listening on this port?\n')
        except Exception as e:
            sys.stderr.write('An unknown error: {}\n'.format(str(e)))
    if __name__ == "__main__":
        server = SocketServer.ForkingTCPServer((HOST, PORT), FastAGI)
        server.serve_forever()