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

Python 怎么解析 Multipart 数据

  •  
  •   Loneyer · 2019-09-17 14:22:36 +08:00 · 2683 次点击
    这是一个创建于 1676 天前的主题,其中的信息可能已经有所发展或是发生改变。

    例如:

    ------WebKitFormBoundaryHqeRA6k14YbNBQoh
    Content-Disposition: form-data; name="id"
    
    1
    ------WebKitFormBoundaryHqeRA6k14YbNBQoh
    Content-Disposition: form-data; name="text"
    
    test
    ------WebKitFormBoundaryHqeRA6k14YbNBQoh
    Content-Disposition: form-data; name="button"
    
    提交
    ------WebKitFormBoundaryHqeRA6k14YbNBQoh--
    

    怎么提取里面的参数值 id=1 text=test button=提交

    4 条回复    2019-09-18 11:32:31 +08:00
    l4mbda
        1
    l4mbda  
       2019-09-17 15:33:33 +08:00
    ```
    from flask import Flask,request

    app = Flask(__name__)

    @app.route('/upload', methods=['POST', 'GET'])
    def upload():
    if request.method == 'POST':
    #print(request.headers)
    #print(request.form)
    temp_info = request.form
    id_field = temp_info['id']
    text_field = temp_info['text']
    button_field = temp_info['button']
    return "OK"
    ```

    用 flask 的 request 试下。
    arrow8899
        2
    arrow8899  
       2019-09-17 15:37:38 +08:00
    web 服务器基本都实现了的,怎么取看你用的那个框架
    cabbage
        3
    cabbage  
       2019-09-17 22:13:46 +08:00
    aiohttp 有简单实现,可以参照这个 https://stackoverflow.com/a/57264428/7378423

    另吐槽下 V2EX 回复不能直接 markdown 挺不方便的,楼上 python 缩进全乱了,游标卡尺都救不了
    Loneyer
        4
    Loneyer  
    OP
       2019-09-18 11:32:31 +08:00
    @cabbage 我这里用 requests 抓包到了以下数据:

    ------WebKitFormBoundaryHqeRA6k14YbNBQoh
    Content-Disposition: form-data; name="id"

    1
    ------WebKitFormBoundaryHqeRA6k14YbNBQoh
    Content-Disposition: form-data; name="text"

    test
    ------WebKitFormBoundaryHqeRA6k14YbNBQoh
    Content-Disposition: form-data; name="button"

    提交
    ------WebKitFormBoundaryHqeRA6k14YbNBQoh--


    解析里面的参数。id=1 text=test button=提示 .
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1120 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 18:29 · PVG 02:29 · LAX 11:29 · JFK 14:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.