我在解压缩之前压缩的字节流时遇到了问题。基本上,我尝试使用函数 bytes.NewReader() 创建一个阅读器,然后使用 gzip.NewReader() 函数解压流。最后,我想以字符串或字节格式返回实际值。
我知道 gzip.NewReader 需要 io.Reader 作为输入,但是,据我所知,类型 Reader 实现了接口(interface)io.Reader。我认为这不会导致任何错误,但我想知道在这种情况下可能是什么问题。如果你帮我解决这个问题,我将不胜感激!
如果您想知道这段文字是什么,
"amZzRUR2NHVtcVpiZHNROHJiTTNYeGdUSndGTlVDZC9jaElSK1lXcFlJOD0="
这是从我的客户端 python 脚本发送的示例输入。它使用 gzip 压缩,使用 AES128 加密,最后以 base64 编码,按此顺序进行。
客户端代码:
import time
import json
import requests
import random
import gzip
import base64
from Crypto.Cipher import AES
baseurl = 'http://0.0.0.0:80'
key = 'TfvY7I358yospfWKcoviZizOShpm5hyH'
iv = 'mb13KcoviZizvYhp'
MODE = AES.MODE_CFB
BLOCK_SIZE = 16
SEGMENT_SIZE = 128
def http_post(url, data):
print('Going to make a request to {} with the following data: {}'.format(url, data))
r = requests.post(url,
data=data,
headers={'Content-type': 'application/json; charset=utf-8',
'Connection': 'keep-alive'}, )
if r.status_code != 200:
print('Server returned unexpected response code {}, and content: {}'.format(r.status_code, r.content))
return False
else:
data = r.json()
return data
def post_sample_data(key, iv):
fake_device_id = "MB88"
Load_VA_Total_Mean = random.randint(1000, 100000)
print('Data that should come back: {}'.format(Load_VA_Total_Mean))
data = {'i': fake_device_id, 'p': [
{'d': [54.3, 0, 99, 49.35, 3, 99, 51.533, 1, 98, 28964, 7348, 43590, Load_VA_Total_Mean, 10350, 55200, 49.7],
't': time.time(), 'dt': 'p'}]}
url = baseurl + '/realtimedata'
encryption_key_reference = 1
payload = '{}\n{}'.format(convert_pack(data, key, iv), encryption_key_reference)
return http_post(url, payload)
def convert_pack(inputdict, key, iv):
jsonpayload = json.dumps(inputdict) # encode dict to json string
gzippayload = gzip.compress(jsonpayload.encode('utf-8')) # compress with gzip
encryptedpayload = base64.b64encode(encrypt(key, iv, message))
encoded = base64.b64encode(encryptedpayload)
print('encoded: {}'.format(encoded))
return str(encoded, encoding='utf-8')
def _pad_string(value):
length = len(value)
pad_size = BLOCK_SIZE - (length % BLOCK_SIZE)
return value.ljust(length + pad_size, '\x00')
def encrypt(key, iv, plaintext):
aes = AES.new(key, MODE, iv, segment_size=SEGMENT_SIZE)
plaintext = _pad_string(plaintext)
encrypted_text = aes.encrypt(plaintext)
return encrypted_text
post_sample_data(key, iv)
服务器代码:
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"compress/gzip"
"io/ioutil"
"bytes"
)
func main() {
receivedText := "amZzRUR2NHVtcVpiZHNROHJiTTNYeGdUSndGTlVDZC9jaElSK1lXcFlJOD0="
fmt.Println("encrypted + encoded + gzipped: ", originalText)
key := []byte("TfvY7I358yospfWKcoviZizOShpm5hyH")
text := decrypt(key, originalText)
fmt.Println("decrypted: ", string(text))
reader := bytes.NewReader(text)
gzReader, err1 := gzip.NewReader(reader)
fmt.Println(gzReader)
if err1 != nil {
fmt.Println("error1")
}
content, err2 := ioutil.ReadAll(gzReader)
if err2 != nil {
fmt.Println("error2")
}
fmt.Println(string(content))
}
func decrypt(key []byte, cryptoText string) []byte {
ciphertext, _ := base64.StdEncoding.DecodeString(cryptoText)
fmt.Println("decoded: ", string(ciphertext))
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
iv := []byte("mb13KcoviZizvYhp")
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(ciphertext, ciphertext)
return ciphertext
}
输出:
<nil>
error1
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x288 pc=0x10a9167]
goroutine 1 [running]:
io/ioutil.readAll.func1(0xc420037dd0)
/usr/local/go/src/io/ioutil/ioutil.go:30 +0x119
panic(0x10c4580, 0x1154d00)
/usr/local/go/src/runtime/panic.go:489 +0x2cf
compress/gzip.(*Reader).Read(0x0, 0xc420092000, 0x200, 0x200, 0x1024ade, 0xc400000008, 0xc4200120c0)
/usr/local/go/src/compress/gzip/gunzip.go:247 +0x37
bytes.(*Buffer).ReadFrom(0xc420037d28, 0x11451a0, 0x0, 0xc420092000, 0x0, 0x200)
/usr/local/go/src/bytes/buffer.go:179 +0x160
io/ioutil.readAll(0x11451a0, 0x0, 0x200, 0x0, 0x0, 0x0, 0x0, 0x0)
/usr/local/go/src/io/ioutil/ioutil.go:33 +0x150
io/ioutil.ReadAll(0x11451a0, 0x0, 0x1, 0x7, 0x0, 0x0, 0x20)
/usr/local/go/src/io/ioutil/ioutil.go:42 +0x3e
main.main()
/Users/bkaankuguoglu/Desktop/Go-dev/tool-backend/tester.go:43
+0x404
exit status 2
最佳答案
问题是您传递给 gz.NewReader() 的阅读器生成的“内容”不是有效的 gzip 流(数据)。如果 gzip.NewReader() 返回一个非 nil 错误(就像您的情况一样),则返回的 gzReader 可能是 无(通常是)。
如果你传递一个有效的gzip流给gzip.NewReader(),返回的gzReader不会是nil,解码就会成功.
例如文本 "hello" 的 gzip 编码形式是:
[31 139 8 0 0 0 0 0 0 255 203 72 205 201 201 7 0 134 166 16 54 5 0 0 0]
使用这个作为输入:
data := []byte{31, 139, 8, 0, 0, 0, 0, 0, 0,
255, 203, 72, 205, 201, 201, 7, 0, 134, 166, 16, 54, 5, 0, 0, 0}
reader := bytes.NewReader(data)
// The rest is unchanged
它有效,输出是(在 Go Playground 上尝试):
&{[31 139 8 0 0 0 0 0 0 255 203 72 205 201 201 7 0 134 166 16 54 5 0 0 0] 0 -1}
&{{ [] 0001-01-01 00:00:00 +0000 UTC 255} 0x10440260 0x10462000 0 0 [31 139 8 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] <nil> true}
hello
关于python - 当 Reader 类型作为输入时,gzip.NewReader() 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44884766/
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
似乎无法为此找到有效的答案。我正在阅读Rails教程的第10章第10.1.2节,但似乎无法使邮件程序预览正常工作。我发现处理错误的所有答案都与教程的不同部分相关,我假设我犯的错误正盯着我的脸。我已经完成并将教程中的代码复制/粘贴到相关文件中,但到目前为止,我还看不出我输入的内容与教程中的内容有什么区别。到目前为止,建议是在函数定义中添加或删除参数user,但这并没有解决问题。触发错误的url是http://localhost:3000/rails/mailers/user_mailer/account_activation.http://localhost:3000/rails/mai
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳
我正在检查一个Rails项目。在ERubyHTML模板页面上,我看到了这样几行:我不明白为什么不这样写:在这种情况下,||=和ifnil?有什么区别? 最佳答案 在这种特殊情况下没有区别,但可能是出于习惯。每当我看到nil?被使用时,它几乎总是使用不当。在Ruby中,很少有东西在逻辑上是假的,只有文字false和nil是。这意味着像if(!x.nil?)这样的代码几乎总是更好地表示为if(x)除非期望x可能是文字false。我会将其切换为||=false,因为它具有相同的结果,但这在很大程度上取决于偏好。唯一的缺点是赋值会在每次运行