我已经压缩了python发出的gzip数据(object->对象数组-> json-> gzip)发送给RabbitMQ。如果我是用python捕获的-我很容易被解码。
编码python函数:
def gzip_str(string_):
out = io.BytesIO()
with gzip.GzipFile(fileobj=out, mode='w') as fo:
fo.write(string_.encode())
bytes_obj = out.getvalue()
return bytes_obj
解码python函数:
def gunzip_bytes_obj(bytes_obj):
in_ = io.BytesIO()
in_.write(bytes_obj)
in_.seek(0)
with gzip.GzipFile(fileobj=in_, mode='rb') as fo:
gunzipped_bytes_obj = fo.read()
return gunzipped_bytes_obj.decode()
问题是-我必须在logstash上对其进行解码,因此应使用ruby完成。 尝试了不同的方法,所有方法都与此类似:
#!/usr/bin/env ruby
# encoding: utf-8
require 'bunny'
require 'base64'
require 'zlib'
require 'stringio'
connection = Bunny.new
connection.start
channel = connection.create_channel
queue = channel.queue('kit_logstash', :durable => true)
begin
queue.subscribe(block: true) do |_delivery_info, _properties, body|
puts body
puts body.encoding
puts Base64.decode64(body)
puts Base64.decode64(body).encoding
body_decoded = Base64.decode64(body.to_s)
sio = StringIO.new(body_decoded)
gz = Zlib::GzipReader.new(sio)
result = gz.read.to_s
end
rescue Interrupt => _
connection.close
exit(0)
end
关于RabbitMQ的消息如下:
H4sIAAuxGFwC/4uuVkrNTczMUbJSUMpyyNJLzs9V0lFQSs7PK0lMLgGJgrh5ibmpILYXkC4GCRRnZ+bkFAOFopW0DRXMzS0sLS0NDAzMlWKBkqkppUCZaqXS
dLCCgMqSjPw8sEwBRMQ5WCm2tlZHYeDsjgUA6yqlo/gAAAA=
它也可以使用一些在线解码器轻松解码。
但是红宝石没有以任何方式处理它 它会查看是否是这样的:
T\x93\x10D\xFA\xCAQ\x03\xC9\xF8\x82\xF2\x82\x06\xE3\xB4\xB0\xFD]Q\x9A
或者类似这样的东西:
��-,--VJ�M��Q�RP�r��K��U�QPJ��+IL.���y��� ��. gg������
当然,它说Zlib::GzipFile::Error: not in gzip format
难道我做错了什么?也许与编码有关?还是字符串字节的差异?