2

我正在尝试创建一个 fastagi 服务器来执行一些 agi 脚本。我正在使用 pyst2 来设置快速 agi 服务器。运行fast agi server的脚本如下:

#!/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()

当我使用以下上下文时,它可以。

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

但我想要超过 1 个脚本,例如 exten => 123,1,agi(agi://FASTAGI_IP_address/handler_name)

我不知道如何在快速 agi 服务器代码中使用一些处理程序名称。我是 python 新手,所以如果我能对如何在 fastagi 代码中添加额外的处理程序有一些明确的指导,我将非常感激。

4

1 回答 1

1

找到了解决方案。在星号服务器上,我使用以下拨号方案联系 Fastagi 服务器:

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

上面代码中显示的“处理程序”作为“agi.env ['agi_network_script']”返回到fastagi服务器。

一个关于如何使用处理程序的简单示例就像下面的代码:

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()
于 2018-11-04T20:06:04.613 回答