V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
eunha
V2EX  ›  Python

Python 如何向服务端提交 pickle.dumps 产生的 bytes(要保真)?

  •  
  •   eunha · 2019-03-31 02:07:17 +08:00 · 1452 次点击
    这是一个创建于 1854 天前的主题,其中的信息可能已经有所发展或是发生改变。

    服务端收到时 bytes 变成了字符串,而且有乱码,还原不回去了 pickle.dumps 返回的 bytes,不知道其使用了什么编码,如 pickle.dumps({})会返回 b'\x80\x03}q\x00.'

    这样的数据,用 utf-8 解码无法解码: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte

    而用 utf-16 解码则不可逆: in: b out: b'\x80\x03}q\x00.' in: b.decode(encoding='utf-16').encode(encoding='utf-16') out: b'\xff\xfe\x80\x03}q\x00.'

    求可逆的这种 bytes 转换为 str 的方法,如果能获取 post 的原始数据就更感激不尽了

    2 条回复
    forestLittleBear
        1
    forestLittleBear  
       2019-03-31 14:22:50 +08:00 via Android
    流弊。
    不知道使用什么编码可还行。。。
    不知道编码格式怎么解码。。。?
    lolizeppelin
        2
    lolizeppelin  
       2019-04-01 17:29:36 +08:00
    用 struct 谢谢
    pickle 是 python 自己用的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   950 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 20:44 · PVG 04:44 · LAX 13:44 · JFK 16:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.