无法使用Python请求将文件上载到服务器

2024-05-03 09:03:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Requests和python2.7来填充表单中的一些值,并将图像上载(提交)到服务器。在

实际上,我从服务器执行指向需要上载的文件的脚本。该文件位于/home目录中,我已确保它具有完全权限。在

虽然我收到了200条回复,但什么都没有上传。这是我代码的一部分:

import requests
try:
    headers = {
        "Referer": 'url_for_upload_form',
        "sessionid": sessionid # retrieved earlier
    }
    files = {'file': ('doc_file', open('/home/test.png', 'rb'))}
    payload = {
        'title': 'test',
        'source': 'source',
        'date': '2016-10-26 02:13',
        'csrfmiddlewaretoken': csrftoken # retrieved earlier - 
     }
    r = c.post(upload_url, files=files, data=payload, headers=headers)
    print r.headers
    print r.status_code
except:
    print "Error uploading file"

正如我说的,我得到了200个响应,返回的标题是:

^{pr2}$

有人知道我做错了什么吗?我是不是错过了一些基本的东西?在


Tags: 文件test服务器urlsourcehomefilesfile