This topic created in 3757 days ago, the information mentioned may be changed or developed.
各位巨巨,最近在用 python 做一个分析流量包的活。抓到的流量 request.body 的内容为乱码,无法解码。存在 redis 里,然后在 redis 里面显示的是这种编码:
\u001d\xd6\xfa\xd1\\u0019\x9f1P\xb5\\u001e7\xe5c&\xaa\xbf
请问各位 这是什么编码方式 要怎么解码呢 ?
Supplement 1 · Mar 7, 2016
可能是我表述的不清楚。我说的是从交换机上镜像流量然后解析出来的 不是使用 requests 库产生的问题。另外目前这个问题我找到的原因是 body 本身是二进制文件。
4 replies • 2016-03-07 12:05:14 +08:00
 |
|
1
wdg8106 Mar 7, 2016
python 里面有个 chardet 模块,用来处理编码的问题很好用, 比如 : a = b'haha',调用 chardet.detect(a),就可以输出: {'confidence': 1.0, 'encoding': 'ascii'} 就可以清楚地看到是什么编码的了
|
 |
|
3
crayonyi Mar 7, 2016
使用 requests 的时候,注意 2 点,基本可以解决大部分的编码问题: 1. 设置编码为 apparent_encoding
``` rsp = requests.get(url) rsp.encoding = rsp.apparent_encoding ```
2. 返回使用 text , 而不是 content 或者 body
``` result = rsp.text ```
|
 |
|
4
crayonyi Mar 7, 2016
因为在 python 中都是采用 unicode 编码的,所以如果想存储到数据库、 redis 、文本文件中,需要先转成 utf-8 存储。通用的方法是:存储前先 josn.dumps()处理成字符串 , 取数据的时候,取出用 json.loads()处理一下就 ok
|