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
aiqier
V2EX  ›  Python

python json.dumps 时 包含签名的 dict 怎么办?

  •  
  •   aiqier · 2015-11-02 17:38:42 +08:00 · 2693 次点击
    这是一个创建于 3103 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我的服务器要返回给请求我的客户端一段 json 字符串(服务器代码是用 python 实现的)。格式如下:

    {
    “ sign ”: “ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ”,
    “ name ”: "王大锤”,
    “ age ”: 14
    }
    其中 sign 是用 openssl rsa 私钥签名” name=王大锤&age=14 ”后的结果。

    在 json.dumps 的时候,就会报错,(我估计是将这个 sign 转化成 unicode 时候报错的,毕竟其本身并不是标准汉字字节码),
    网上有一种写法是: sign.encode(‘ utf-8 ’, ‘ ignore')这样做成 unicode 。再去 dumps

    但是这样试了的结果是签名校验失败了,请问有什么好的解决办法?

    6 条回复    2015-11-02 22:41:55 +08:00
    janxin
        1
    janxin  
       2015-11-02 17:59:33 +08:00
    你能不能吧 sign 转化成 hex....这样就不会有问题了,是 0-9a-f 的范围内的字符
    zhicheng
        2
    zhicheng  
       2015-11-02 18:04:41 +08:00   ❤️ 1
    base64
    binux
        3
    binux  
       2015-11-02 18:18:26 +08:00
    json 不能编码 binary 的内容, 你可以 hexdigits 也可以 base64, 都蛮常用的.
    yongzhong
        4
    yongzhong  
       2015-11-02 18:29:45 +08:00
    试试 json.dumps(var, ensure_ascii=False)
    aiqier
        5
    aiqier  
    OP
       2015-11-02 18:41:11 +08:00
    @yongzhong 这个做法是不行的。
    tonic
        6
    tonic  
       2015-11-02 22:41:55 +08:00
    把 binary hexdigest 一下变字符串啊...
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2449 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 12:57 · PVG 20:57 · LAX 05:57 · JFK 08:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.