Python3 compatibility
Closed this issue · 12 comments
ConfigParse change in python3 and StringIO is not a module anymore.
But still have error of compatibility:
File "/usr/local/lib/python3.4/dist-packages/zabbix/sender.py", line 221, in send
packet = self.__create_packet(request)
File "/usr/local/lib/python3.4/dist-packages/zabbix/sender.py", line 170, in __create_packet
packet = 'ZBXD\x01' + data_len + request
TypeError: Can't convert 'bytes' object to str implicitly
If use decode
method to convert bytes to string it might help:
data_len = struct.pack('<Q', len(request))
packet = 'ZBXD\x01' + data_len.decode() + request
Need to verify on 2 and 3 version of python
Decode was not applied yet. But I will test it soon
@blacked your advice works on python 2 and 3 but I got this new error:
File "/usr/local/lib/python3.4/dist-packages/zabbix/sender.py", line 225, in send
packet = self.__create_packet(request)
File "/usr/local/lib/python3.4/dist-packages/zabbix/sender.py", line 175, in __create_packet
':'.join(x.encode('hex') for x in packet))
File "/usr/local/lib/python3.4/dist-packages/zabbix/sender.py", line 175, in
':'.join(x.encode('hex') for x in packet))
LookupError: 'hex' is not a text encoding; use codecs.encode() to handle arbitrary codecs
What is objective of this code?
logger.debug('%s.__create_packet (hex): %s', self.cn,
':'.join(x.encode('hex') for x in packet))
This is just output debug info about packet in HEX.
I think binascii
will help add python2\3 compatibility:
import binascii
logger.debug('%s.__create_packet (hex): %s', self.cn,
':'.join(binascii.hexlify(x.encode()) for x in packet))
Python3 don't works :(
TypeError: sequence item 0: expected str instance, bytes found
I tried put:
':'.join(binascii.hexlify(x) for x in packet))
But I got this error:
TypeError: 'str' does not support the buffer interface
I am confuse :(
This actually broke the functionality on Python 2.7.10 (Mac OS X).
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/zabbix/sender.py", line 177, in __create_packet
packet = 'ZBXD\x01' + data_len.decode() + request
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd9 in position 0: ordinal not in range(128)
I haven't tested other architectures.
@danielbenzvi can you try with?
packet = 'ZBXD\x01' + data_len.decode('utf-8') + request
Actually I tested on 2.7.9, and it works. Also what metric you try to send?