V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
ecloud
V2EX  ›  Python

Requests 库能不能自定义 boundary

  •  
  •   ecloud ·
    yecloud · Aug 5, 2016 · 2887 views
    This topic created in 3552 days ago, the information mentioned may be changed or developed.
    Requests 库默认发送 multipart 是自动在头和消息体中加入 boundary 的
    但是它自动生成的头里没有 chareset=utf-8 ,因此造成中文编码问题
    虽然 Requests 库支持自定义头,可以加入 chareset 和 boundary ,但是在消息体里面加 boundary 怎么实现?找了半天网上也米有相关信息……
    4 replies    2016-08-06 13:26:13 +08:00
    pc10201
        1
    pc10201  
       Aug 5, 2016
    用 python 3 支持中文附件传送,将 Python35\Lib\site-packages\requests\packages\urllib3\fields.py 中的
    value = email.utils.encode_rfc2231(value, 'utf-8')
    改为
    value = '%s="%s"' % (name, value)
    即可
    ecloud
        2
    ecloud  
    OP
       Aug 5, 2016
    @pc10201 这都哪跟哪?我说的跟发邮件有半毛钱关系?
    aec4d
        3
    aec4d  
       Aug 6, 2016
    requests 为了自身的代码简洁是不支持自定义分隔符的 不过你可以用第三方辅助库 requests_toolbelt
    见 github 上的讨论 https://github.com/kennethreitz/requests/issues/1997
    julyclyde
        4
    julyclyde  
       Aug 6, 2016
    @ecloud 他回答的也跟邮件没关系啊
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5810 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 110ms · UTC 03:39 · PVG 11:39 · LAX 20:39 · JFK 23:39
    ♥ Do have faith in what you're doing.