• python模拟文件上传(multipart/formdata形式)


    我们通过抓包工具,抓到上传文件的时候下面的post传输的数据,就是浏览器控制器中显示的Request Payload

    ------WebKitFormBoundarynZb9BK3DBujba6Z2
    Content-Disposition: form-data; name="__VIEWSTATE"
     
    /wEPDwUJNDc2MzM2OTk3ZGS0sDY2lwhw3cK6BpS7DP4LL/pVS1fwACEm67roAq7xaw==
    ------WebKitFormBoundarynZb9BK3DBujba6Z2
    Content-Disposition: form-data; name="__VIEWSTATEGENERATOR"
     
    8AA0AEB0
    ------WebKitFormBoundarynZb9BK3DBujba6Z2
    Content-Disposition: form-data; name="__EVENTVALIDATION"
     
    /wEdABdwEIM+9ljNRxmpNzgN6IJ6GXe/BpiiCcppl3PRVXGLJvVDL1IC8mSY8LCpQhBijMUZUJ8KdHr4AJMS3YNTToqIEIwp6IVONiIdALVRMBxa5csxmd4JIMyklOPCI7iUcwardrzAsF0DsWlP6tF4+5klq0s3naV7Y7FpPV3sbg/8DQh/VRLTESOi0oG2UGbE0JgkNjNt/bZ7hRmry31cW8qsCyNFZFpqymkaMzNK9YGKf8H4SARlUFnLprbLaPwS8nsOO7Qt+va79Co+MIU8gR/jajFt+6qdHWWO2PdeWThPhfXUX4Z8bTZ2FDr79dS5BVz9hBcwb1RQNKN/AwvztWy6giKpHXcdvIWBTSfk71PbvIUsDfd9PVqStNlR+JBwc8zPAKZTvP5rCl1CiqfLwzopnTq+BNiCqezsolefUoaJj7E5jBBkTeha0O922YbyQuS9cjYLEQ5UlQjhyXmaUwj5PIuWZGgzO17dB322+QNkOgeeYaMSG0L26ZIS/0LwGvKAbddqe0EhiJ3WCcDK5gdS
    ------WebKitFormBoundarynZb9BK3DBujba6Z2
    Content-Disposition: form-data; name="File"; filename="pp.pdf"
    Content-Type: application/pdf
     
     
    ------WebKitFormBoundarynZb9BK3DBujba6Z2
    Content-Disposition: form-data; name="ctl00$content$ddlLanguage"
     
    2052
    ------WebKitFormBoundarynZb9BK3DBujba6Z2
    Content-Disposition: form-data; name="ctl00$content$cmdSaveAttachment"
     
    图片转文字
    ------WebKitFormBoundarynZb9BK3DBujba6Z2--

    这是我们通过python的requests模块上传文件,如同我们浏览器的multipart/form-data形式上传文件,代码如下。
    替换files里面的键值对就可以为你所用了。

    #coding:utf-8
    import requests
    import sys
    reload(sys)
    sys.setdefaultencoding('utf8')
     
     
    files={'__VIEWSTATE':(None,'/wEPDwUJNDc2MzM2OTk3ZGS0sDY2lwhw3cK6BpS7DP4LL/pVS1fwACEm67roAq7xaw=='),
        '__VIEWSTATEGENERATOR':(None,'8AA0AEB0'),
        '__EVENTVALIDATION':(None,'/wEdABdwEIM+9ljNRxmpNzgN6IJ6GXe/BpiiCcppl3PRVXGLJvVDL1IC8mSY8LCpQhBijMUZUJ8KdHr4AJMS3YNTToqIEIwp6IVONiIdALVRMBxa5csxmd4JIMyklOPCI7iUcwardrzAsF0DsWlP6tF4+5klq0s3naV7Y7FpPV3sbg/8DQh/VRLTESOi0oG2UGbE0JgkNjNt/bZ7hRmry31cW8qsCyNFZFpqymkaMzNK9YGKf8H4SARlUFnLprbLaPwS8nsOO7Qt+va79Co+MIU8gR/jajFt+6qdHWWO2PdeWThPhfXUX4Z8bTZ2FDr79dS5BVz9hBcwb1RQNKN/AwvztWy6giKpHXcdvIWBTSfk71PbvIUsDfd9PVqStNlR+JBwc8zPAKZTvP5rCl1CiqfLwzopnTq+BNiCqezsolefUoaJj7E5jBBkTeha0O922YbyQuS9cjYLEQ5UlQjhyXmaUwj5PIuWZGgzO17dB322+QNkOgeeYaMSG0L26ZIS/0LwGvKAbddqe0EhiJ3WCcDK5gdS'),
        'File':('pp.pdf',open('C:\Administrator\pp.pdf','rb'),'application/pdf'),
        'ctl00$content$ddlLanguage':(None,'2052'),
        'ctl00$content$cmdSaveAttachment':(None,'图片转文字')
     }
     
     
    response=requests.post(url,files=files)
    html = response.text
    print html

    在官方网站上,requests模拟一个表单数据的格式如下:

    files = {'name': (<filename>, <file object>,<content type>, <per-part headers>)}

    这一行模拟出来的post数据为:

    Content-Disposition: form-data; name=’name’;filename=<filename>
    Content-Type: <content type>
     
     
    <file object>
    --boundary

    如果filename 和 content-Type不写,那么响应模拟post的数据就不会有二者。

  • 相关阅读:
    二读《活着》有感
    Linux系统上安装JDK和Tomcat服务器
    在阿里云服务器上安装完成并启动Tomcat后,通过http不能访问--解决办法
    安装JDK出现错误:-bash: /usr/java/jdk1.7.0_71/bin/java: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory解决办法
    python结巴(jieba)分词
    mysql数据库优化
    ansible常见模块
    CentOS6.5 64位下安装部署Ansible
    [Python] 利用commands模块执行Linux shell命令
    python迭代器
  • 原文地址:https://www.cnblogs.com/hushaojun/p/16124770.html
Copyright © 2020-2023  润新知