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

post man 模拟上传一个文件改成 Python 报错 空文件

  •  
  •   bestehen · 2018-08-26 14:20:34 +08:00 · 1771 次点击
    这是一个创建于 2277 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import requests

    url = "http://yk-project.dev.ethercap.com/report/add"

    querystring = {"_":"1111111"}

    payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"_csrf\"\r\n\r\nBr0949bZlNP4o4iGtNkbE1uE6XU903CqrI9ZnZRQzJF_zEi0n-_g5Zmay_T7qkNYPbbdGVGGRtzA6Qn15Rae6A==\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"ReportForm[url]\"; filename=\"齐家网 COO 毛新勇:《未来平台赋能是必然,但要真刀真枪才有意义》-18 页.pdf\"\r\nContent-Type: application/pdf\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
    headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
    'Cookie': "u=400; PHPSESSID=dfc4ac95c5c485a49ec167febc0b2da6; _csrf=0226ebc29a0710864db49376b6c400a3895ce0dbfb7161dc3d08e5bc8d148386a%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%22aoSW7OAr9i2rwVb2H7Khty6qSimxBUMn%22%3B%7D",
    'Cache-Control': "no-cache",
    'Postman-Token': "64fd1e6e-e442-4161-b32e-110c6627ee23"
    }

    response = requests.request("POST", url, data=payload, headers=headers, params=querystring)

    print(response.text)

    自己复制后改成下列形式 我写绝对路径和相对路径都报错空文件,请指教,这是为啥? 完全复制 postman 的

    url = "http://yk-project.dev.ethercap.com/report/add"

    querystring = {"_": "1111111"}

    payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"_csrf\"\r\n\r\n{}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"ReportForm[url]\"; filename=\"{}\"\r\nContent-Type: application/pdf\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--".format(csrf_token,'麦肯锡中国银行业 CEO 季刊-2018 年冬季刊精简版-137 页.pdf')
    print(payload.encode('utf-8'))
    headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
    'Cookie': "u=400; PHPSESSID=dfc4ac95c5c485a49ec167febc0b2da6; _csrf=0226ebc29a0710864db49376b6c400a3895ce0dbfb7161dc3d08e5bc8d148386a%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%22aoSW7OAr9i2rwVb2H7Khty6qSimxBUMn%22%3B%7D",
    'Cache-Control': "no-cache",
    'Postman-Token': "64fd1e6e-e442-4161-b32e-110c6627ee23"
    }

    response = requests.request("POST", url, data=payload.encode('utf-8'), headers=headers, params=querystring)
    luozic
        1
    luozic  
       2018-08-26 17:11:11 +08:00 via iPhone
    报错日志在哪? 你这么问谁知道啥问题?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1321 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 18:00 · PVG 02:00 · LAX 10:00 · JFK 13:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.