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

python3 UPD 传输如何发送字典类型数据?

  •  
  •   dossec · 2016-06-24 18:37:13 +08:00 · 3878 次点击
    这是一个创建于 2853 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在 python3 使用 UDP 传输的时候如果发送字典类型的数据,会提示: TypeError: a bytes-like object is required, not 'str'

    如果不使用 json 格式,会提示: TypeError: a bytes-like object is required, not 'dict'

    一定要是 a bytes-like object 这种类型才能传输,那字典如何才能转成 a bytes-like object 类型呢?谢谢大家了

    下面是部分发送代码: sent_data = { 'ip': ip, 'id': '021', 'name': 'name' }

    json_string = json.dumps(sent_data) s.sendto(json_string, host)

    7 条回复    2016-06-24 21:12:53 +08:00
    icedx
        1
    icedx  
       2016-06-24 18:43:15 +08:00 via Android
    bytes(json_string, encoding = "utf8")
    Kilerd
        2
    Kilerd  
       2016-06-24 19:14:42 +08:00
    @icedx 抓。
    shyling
        3
    shyling  
       2016-06-24 19:22:51 +08:00
    str({'name':'123'}).encode('utf-8')
    dossec
        4
    dossec  
    OP
       2016-06-24 19:27:45 +08:00
    @icedx 谢谢按你的方法搞定了
    dossec
        5
    dossec  
    OP
       2016-06-24 19:34:29 +08:00
    @shyling 谢谢,你的方法也可以。
    v2014
        6
    v2014  
       2016-06-24 20:46:47 +08:00
    我也是醉了,明明早上就告诉你了
    dossec
        7
    dossec  
    OP
       2016-06-24 21:12:53 +08:00
    @v2014 我想起来了,我在群里问过了,不过到现在我都没上 QQ ,不好意思哈~~还是要谢谢你
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1674 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:46 · PVG 00:46 · LAX 09:46 · JFK 12:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.