ASYNC AGI client/Server Framework(ASYNCO)
aioagi的Python项目详细描述
艾奥吉
异步agi客户机/服务器框架。 该项目基于“aiohttp”框架。
主要功能
- 支持AGI协议的客户端和服务器端。
- agi服务器有中间件和可插拔路由。
开始
服务器
简单的AGI服务器:
fromaioagiimportrunnerfromaioagi.appimportAGIApplicationfromaioagi.logimportagi_server_loggerfromaioagi.urldispathcerimportAGIViewasyncdefhello(request):message=awaitrequest.agi.stream_file('hello-world')awaitrequest.agi.verbose('Hello handler: {}.'.format(request.rel_url.query))agi_server_logger.debug(message)classHelloView(AGIView):asyncdefsip(self):message=awaitself.request.agi.stream_file('hello-world')awaitself.request.agi.verbose('HelloView handler: {}.'.format(self.request.rel_url.query))agi_server_logger.debug(message)if__name__=='__main__':logging.config.dictConfig(LOGGING)app=AGIApplication()app.router.add_route('SIP','/',hello)runner.run_app(app)
客户
将AGI连接设置为星号:
importasyncioimportlogging.configfromaioagi.logimportagi_client_loggerfromaioagi.clientimportAGIClientSessionfromaioagi.parserimportAGIMessage,AGICodeasyncdeftest_request(loop):headers={'agi_channel':'SIP/100-00000001','agi_language':'ru','agi_uniqueid':'1532375920.8','agi_version':'14.0.1','agi_callerid':'100','agi_calleridname':'test','agi_callingpres':'0','agi_callingani2':'0','agi_callington':'0','agi_callingtns':'0','agi_dnid':'101','agi_rdnis':'unknown','agi_context':'from-internal','agi_extension':'101','agi_priority':'1','agi_enhanced':'0.0','agi_accountcode':'','agi_threadid':'139689736754944',}asyncwithAGIClientSession(headers=headers,loop=loop)assession:asyncwithsession.sip('agi://localhost:8080/hello/?a=test1&b=var1')asresponse:asyncformessageinresponse:client_logger.debug(message)awaitresponse.send(AGIMessage(AGICode.OK,'0',{}))asyncwithsession.sip('agi://localhost:8080/hello-view/?a=test2&b=var2')asresponse:asyncformessageinresponse:client_logger.debug(message)awaitresponse.send(AGIMessage(AGICode.OK,'0',{}))
注意
自动为session.sip('agi://localhost:8080/hello/?a=test1&b=var1')请求设置会话请求头:
agi_type: SIP agi_network: yes agi_network_script: hello/ agi_request: agi://localhost:8080/hello/
安装
pip install aioagi