123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- #!/usr/bin/env python3
- # encoding:utf-8
- import json
- import ESL
- import threading
- import socketserver
- from http.server import BaseHTTPRequestHandler
- from http.server import HTTPServer
- class InboundClient:
- def __init__(self):
- self.is_stopping = False
- self.con = ESL.ESLconnection('localhost', '8021', '4918257983818884358')
- def start(self):
- if self.con.connected():
- self.con.events('plain', 'all')
- while not self.is_stopping:
- e = self.con.recvEvent()
- if e:
- print(e)
- # print(e.serialize('json'))
- def stop(self):
- self.is_stopping = True
- class OutboundClient:
- @staticmethod
- def ack(esl, con):
- info = con.getInfo()
- print(info.serialize('json'))
- event_name = info.getHeader("Event-Name")
- uuid = info.getHeader("unique-id")
- print(uuid, event_name)
- con.execute("answer", "", uuid)
- con.execute("playback", "/Users/davidliu/sip/pjproject/voip/test111.wav", uuid)
- con.disconnect()
- class ESLRequestHandler(socketserver.BaseRequestHandler):
- def setup(self):
- print(self.client_address, 'connected!')
- fd = self.request.fileno()
- print('0000', fd)
- con = ESL.ESLconnection(fd)
- print('Connected: ', con.connected())
- if con.connected():
- threading.Thread(target=self.ack, args=(con, )).start()
- else:
- print("Failed to connect to FreeSWITCH")
- def ack(self, con):
- OutboundClient.ack(self, con)
- def start(self, HOST= '0.0.0.0', PORT=8084):
- # HOST, PORT = "0.0.0.0", 8084
- # 创建一个 TCP 服务器
- with socketserver.ThreadingTCPServer((HOST, PORT), self.ESLRequestHandler) as server:
- print(f"ESL server listening on {HOST}:{PORT}")
- server.serve_forever()
- def main():
- on = InboundClient()
- out = OutboundClient()
- threading.Thread(target=out.start, args=()).start()
- threading.Thread(target=on.start, args=()).start()
- if __name__ == '__main__':
- main()
|