推荐学习书目
› 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
lxc404
V2EX  ›  Python

post 问题求教

  •  
  •   lxc404 · Dec 17, 2017 · 4279 views
    This topic created in 3242 days ago, the information mentioned may be changed or developed.
    用 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
    *****************结束 省略部分数据,格式一样******
    Supplement 1  ·  Dec 18, 2017
    感谢各位大佬的指点,问题已解决。附上代码:

    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 replies  •  2017-12-18 12:56:10 +08:00
    slixurd
    slixurd
    Dec 17, 2017
    1
    就是 multipart 而已,最简单的方式就是照抄.....
    记得标记一下 7e11614f10de 这个字符串是 multipart 分隔符
    Nioty
    Nioty
    Dec 18, 2017 via Android
    2
    直接用 request_toolkit 这个库 好像是这个名字 你去搜搜
    Kilerd
    Kilerd
    Dec 18, 2017
    3
    说了多少次。
    找资料用 google
    python 爬虫起码用 requests,不要用 urllib 了

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

    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
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1481 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 88ms · UTC 23:22 · PVG 07:22 · LAX 15:22 · JFK 18:22
    ♥ Do have faith in what you're doing.