【问题标题】:Not able to upload a file through python无法通过python上传文件
【发布时间】:2013-09-09 10:30:07
【问题描述】:

经过多次尝试和反复失败,我在此处发布我的代码摘录。我不断收到身份验证失败。有人能指出我在这里做错了什么吗?

import requests

fileToUpload = {'file': open('/home/pinku/Desktop/Test_Upload.odt', 'rb')}
res = requests.post('https://upload.backupgrid.net/add', fileToUpload)
print res.headers
cookie = {'PHPSESSID': 'tobfr5f31voqmtdul11nu6n9q1'}
requests.post('https://upload.backupgrid.net/add', cookie, fileToUpload)

通过打印 res.headers,我得到以下信息:

CaseInsensitiveDict({'content-length': '67', '访问控制允许方法':'选项,头,GET,POST,PUT, 删除','x-content-type-options':'nosniff','content-encoding': 'gzip', 'set-cookie': 'PHPSESSID=ou8eijalgpss204thu7ht532g1;路径=/, B100Serverpoolcookie=4281246842.1.973348976.502419456;路径=/', 'expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'vary': 'Accept-Encoding', 'server': 'Apache/2.2.15 (CentOS)', 'pragma': 'no-cache', '缓存控制':'无存储,无缓存,必须重新验证','日期':'星期一, 2013 年 9 月 9 日 09:13:08 GMT','access-control-allow-origin':'*', 'access-control-allow-headers': 'X-File-Name, X-File-Type, X-File-Size', 'content-type': 'text/html; charset=UTF-8'})

它还包含 cookie。我是否正确传递了 cookie?请帮忙!

【问题讨论】:

    标签: python file-upload http-post multipartform-data


    【解决方案1】:

    您没有正确传递 cookie,应该是:

    requests.post('https://upload.backupgrid.net/add', 
                  files=fileToUpload, 
                  cookies=cookie)
    

    另见文档:

    【讨论】:

    • 谢谢,但我仍然收到以下错误[{"error":true,"error_msg":"Authentication Failed"}]
    猜你喜欢
    • 1970-01-01
    • 2016-06-07
    • 2020-02-10
    • 2016-02-19
    • 1970-01-01
    • 2019-04-19
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多