I'm making a fastagi app with python3 and pyst2 library. The code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
######################################################
import asterisk, sys
import asterisk.agi
import asterisk.manager
import asterisk.config
import sys
import socketserver
HOST, PORT = "127.0.0.1", 4573
def custom_codes():
agi.verbose("....... custom codes ..............")
class FastAGI(socketserver.StreamRequestHandler):
def handle(self):
try:
global agi
agi = asterisk.agi.AGI(stdin=self.rfile, stdout=self.wfile, stderr=sys.stderr)
global ddi_destino
ddi_destino = agi.env['agi_extension']
global ddi_origen
ddi_origen = agi.env['agi_callerid']
global agi_uniqueid
agi_uniqueid = agi.env['agi_uniqueid']
global agi_channel
agi_channel = agi.env['agi_channel']
global agi_type
agi_type = agi.env['agi_type']
handler = agi.env['agi_network_script']
if handler == 'custom_codes':
custom_codes()
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_fastagi = socketserver.ForkingTCPServer((HOST, PORT), FastAGI)
server_fastagi.serve_forever()
And, in asterisk, call with:
exten => _*X,1,NoOP(.......... custom codes ...........)
exten => _*X,n,Answer()
exten => _*X,n,AGI(agi://127.0.0.1/custom_codes)
exten => _*X,n,Hangup()
The app starts Ok, but, when I call the script, I view this error in console:
'agi_version': '18.18.0'}
COMMAND: VERBOSE "....... custom codes .............." 1
Unable to connect to agi://127.0.0.1 a bytes-like object is required, not 'str'
Searching a solution with Mr. Google I view this post:
TypeError: a bytes-like object is required, not 'str'
But i don't view any part to apply this.
any idea ?
Thanks
I don't view any part to apply the issue from python2 to python3 function
Solved.
I've changed the code of original llibrary from :
def send_command(self, command, *args):
"""Send a command to Asterisk"""
command = command.strip()
command = '%s %s' % (command, ' '.join(map(str, args)))
command = command.strip()
if command[-1] != '\n':
command += '\n'
self.stderr.write(' COMMAND: %s' % command)
self.stdout.write(command)
self.stdout.flush()
To:
def send_command(self, command, *args):
"""Send a command to Asterisk"""
command = command.strip()
command = '%s %s' % (command, ' '.join(map(str, args)))
command = command.strip()
if command[-1] != '\n':
command += '\n'
self.stderr.write(' COMMAND: %s' % command)
self.stdout.write(command.encode('utf8'))
self.stdout.flush()