python请求post文件返回null

时间:2017-05-24 08:11:28

标签: python python-requests

我需要有关python请求的帮助。我想将文件发布到服务器。这是我发布文件的示例代码

import requests

with open('try.txt', 'rb') as myfile:
    url = <myurl>
    files = {'value':myfile}
    response = requests.post(url, files=files)

try.txt不是空白文件。它包含一些句子

在服务器端,这是接收文件的代码段。

from flask_restful import Api, Resource, reqparse

class MyThing(Resource):
    def get(self):
        pass

    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('value')
        args = parser.parse_args()

        value = args['value']

        if value.endswith(".txt") or value.endswith(".csv"):
            ## do something

        else:
            ## do something

但是,当我运行服务器并尝试将文件发布到服务器时,它会返回错误,说明

    if value.endswith(".txt") or value.endswith(".csv"):
AttributeError: 'NoneType' object has no attribute 'endswith'

我按value检查print files并按此返回

{'value': <open file 'try.txt', mode 'rb' at 0x7f041db8e420>}

这是否意味着valuenull?或者我在服务器端代码上遗漏了什么?谢谢你的帮助。

0 个答案:

没有答案