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

post 问题求教

  •  
  •   olaloong · 2017-12-17 22:59:25 +08:00 · 2997 次点击
    这是一个创建于 2541 天前的主题,其中的信息可能已经有所发展或是发生改变。
    用 Fiddler 抓的 post 包中有如下数据,这种数据该怎么用 post 提交?也就是 urllib.request.Request(url, data,header)的 data 该怎么构造?求大佬解答~

    ************从以下开始*********
    -----------------------------7e11614f10de
    Content-Disposition: form-data; name="token"

    f91aa2948c4ab9bf80b17f9c6a3984ef
    -----------------------------7e11614f10de
    Content-Disposition: form-data; name="cbi.submit"

    1
    -----------------------------7e11614f10de
    Content-Disposition: form-data; name="tab.network.TEST"

    advanced
    -----------------------------7e11614f10de
    Content-Disposition: form-data; name="cbid.network.TEST._fwzone"

    -
    -----------------------------7e11614f10de
    Content-Disposition: form-data; name="cbid.network.TEST._fwzone.newzone"


    -----------------------------7e11614f10de
    Content-Disposition: form-data; name="cbi.cbe.network.TEST.type"

    1
    -----------------------------7e11614f10de
    Content-Disposition: form-data; name="cbi.cbe.network.TEST.ifname_single"

    1
    -----------------------------7e11614f10de
    Content-Disposition: form-data; name="cbid.network.TEST.ifname_single"

    eth0.3
    -----------------------------7e11614f10de
    Content-Disposition: form-data; name="cbid.network.TEST.proto"

    dhcp
    *****************结束 省略部分数据,格式一样******
    第 1 条附言  ·  2017-12-18 13:10:29 +08:00
    感谢各位大佬的指点,问题已解决。附上代码:

    def modTEST():
    url = 'http://192.168.1.1/cgi-bin/luci/admin/network/network/TEST'
    data = {'token':(None,getToken()),
    'cbi.submit':(None,'1'),
    'tab.network.TEST':(None,'advanced'),
    'cbid.network.TEST._fwzone':(None,'-'),
    'cbid.network.TEST._fwzone.newzone':(None,''),
    'cbi.cbe.network.TEST.type':(None,'1'),
    'cbi.cbe.network.TEST.ifname_single':(None,'1'),
    'cbid.network.TEST.ifname_single':(None,'eth0.3'),
    'cbid.network.TEST.proto':(None,'dhcp'),
    'cbid.network.TEST.hostname':(None,''),
    'cbi.cbe.network.TEST.auto':(None,'1'),
    'cbi.cbe.network.TEST.delegate':(None,'1'),
    'cbid.network.TEST.delegate':(None,'1'),
    'cbi.cbe.network.TEST.broadcast':(None,'1'),
    'cbi.cbe.network.TEST.defaultroute':(None,'1'),
    'cbid.network.TEST.defaultroute':(None,'1'),
    'cbi.cbe.network.TEST.peerdns':(None,'1'),
    'cbid.network.TEST.peerdns':(None,'1'),
    'cbid.network.TEST.metric':(None,''),
    'cbid.network.TEST.clientid':(None,''),
    'cbid.network.TEST.vendorid':(None,''),
    'cbid.network.TEST.macaddr':(None,'00:e0:4c:36:11:a1'),
    'cbid.network.TEST.mtu':(None,''),
    'cbi.apply':(None,'保存&应用')}
    print(data)
    res = requests.post(url, files=data, cookies={'sysauth': cookiesValue})
    print(res.request.body)
    print(res.request.headers)
    res.encoding = 'utf8'
    print(res.text)
    restartFireWall()
    return
    有问题欢迎指正。
    11 条回复    2017-12-18 12:56:10 +08:00
    slixurd
        1
    slixurd  
       2017-12-17 23:46:11 +08:00
    就是 multipart 而已,最简单的方式就是照抄.....
    记得标记一下 7e11614f10de 这个字符串是 multipart 分隔符
    Nioty
        2
    Nioty  
       2017-12-18 00:27:59 +08:00 via Android
    直接用 request_toolkit 这个库 好像是这个名字 你去搜搜
    Kilerd
        3
    Kilerd  
       2017-12-18 00:36:24 +08:00
    说了多少次。
    找资料用 google
    python 爬虫起码用 requests,不要用 urllib 了

    简直要命啊,你们
    swordspoet
        4
    swordspoet  
       2017-12-18 00:41:28 +08:00 via iPhone
    @Kilerd 啊?我今天还用的 urllib,踩了好多个坑,难道跟库有关系?
    just1
        5
    just1  
       2017-12-18 02:35:55 +08:00 via Android
    同#3,完全不懂的新手还是老老实实用 requests 吧,用 urllib 真的要命
    jtsai
        6
    jtsai  
       2017-12-18 06:59:57 +08:00 via Android
    上传文件,就自动转这种
    goofool
        7
    goofool  
       2017-12-18 08:43:10 +08:00 via Android
    用 postman,可以自动给你转成 python 源码
    chen2016
        8
    chen2016  
       2017-12-18 09:07:59 +08:00 via Android
    用 requests
    xlui
        9
    xlui  
       2017-12-18 09:52:03 +08:00 via Android
    为什么不先自己试试再来发帖呢?
    vicvinc
        10
    vicvinc  
       2017-12-18 12:03:45 +08:00
    form data append 前端这么做的,python 应该也有 form data 的生成库吧
    olaloong
        11
    olaloong  
    OP
       2017-12-18 12:56:10 +08:00
    感谢各位大佬的指点,问题解决了,附上我的代码:

    def modTEST():
    url = 'http://192.168.1.1/cgi-bin/luci/admin/network/network/TEST'
    data = {'token':(None,getToken()),
    'cbi.submit':(None,'1'),
    'tab.network.TEST':(None,'advanced'),
    'cbid.network.TEST._fwzone':(None,'-'),
    'cbid.network.TEST._fwzone.newzone':(None,''),
    'cbi.cbe.network.TEST.type':(None,'1'),
    'cbi.cbe.network.TEST.ifname_single':(None,'1'),
    'cbid.network.TEST.ifname_single':(None,'eth0.3'),
    'cbid.network.TEST.proto':(None,'dhcp'),
    'cbid.network.TEST.hostname':(None,''),
    'cbi.cbe.network.TEST.auto':(None,'1'),
    'cbi.cbe.network.TEST.delegate':(None,'1'),
    'cbid.network.TEST.delegate':(None,'1'),
    'cbi.cbe.network.TEST.broadcast':(None,'1'),
    'cbi.cbe.network.TEST.defaultroute':(None,'1'),
    'cbid.network.TEST.defaultroute':(None,'1'),
    'cbi.cbe.network.TEST.peerdns':(None,'1'),
    'cbid.network.TEST.peerdns':(None,'1'),
    'cbid.network.TEST.metric':(None,''),
    'cbid.network.TEST.clientid':(None,''),
    'cbid.network.TEST.vendorid':(None,''),
    'cbid.network.TEST.macaddr':(None,'00:e0:4c:36:11:a1'),
    'cbid.network.TEST.mtu':(None,''),
    'cbi.apply':(None,'保存&应用')}
    print(data)
    res = requests.post(url, files=data, cookies={'sysauth': cookiesValue})
    print(res.request.body)
    print(res.request.headers)
    res.encoding = 'utf8'
    print(res.text)
    restartFireWall()
    return
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3444 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 11:49 · PVG 19:49 · LAX 03:49 · JFK 06:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.