jfernandz / pyst2

A fork of the famous python pyst library for Asterisk.
139 stars 105 forks source link

Python3 AGI: write() argument must be str, not bytes #40

Open marrold opened 6 years ago

marrold commented 6 years ago

When using asterisk.agi with python 3.5, Asterisk 14 and Debian, I get the following exception

write() argument must be str, not bytes

I noticed if I replace lines 166-169:

https://github.com/rdegges/pyst2/blob/bd61a408412aef6119c750352ea2f38a71f6d596/asterisk/agi.py#L166-L169

With the following:

self.stdout.write(command)

It works fine.

Does anyone have any ideas how to fix this?

Thanks

evilscientress commented 5 years ago

I have the same error. Removing the if clause in lines 166-169 and replacing it with self.stdout.write(command) seams like the correct fix for me.

I had this working with Python 3.4, and TBH I don't know why those lines ever worked in the first place, because in no version of Python 3 sys.stdout.write() accepted bytes, unless you call sys.stdout.detach() first.

Refs:

angelelena commented 1 year ago

With debian 11 and python 3.9, may be:

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()

Basically:

self.stdout.write(command.encode('utf8'))