【问题标题】:cherrypy file upload combined with requests post request樱桃文件上传结合请求发布请求
【发布时间】:2013-03-15 14:54:17
【问题描述】:

我很难将 python 请求发布请求与cherrypy 服务器结合起来以上传文件。

cherrypy 服务器内部的服务器端代码如下所示:

@cherrypy.expose
def upload(self, myFile=None):
    out = """<html>
    <body>
    myFile length: %s<br />
    myFile filename: %s<br />
    myFile mime-type: %s
    </body>
    </html>"""

    size = 0
    allData=''
    logging.info('myfile: ' + str(myFile))
    while True:
            data = myFile.file.read(8192)
            allData+=data
            if not data:
                    break
            size += len(data)
    savedFile=open(myFile.filename, 'wb')
    logging.info('writing file: ' + myFile.filename)
    savedFile.write(allData)
    savedFile.close()
    return out % (size, myFile.filename, myFile.type)

而客户端(目前)只是一个 python 请求调用: testfile = open('testfile', 'r') request = requests.post("http://:8088/upload/", files={'myFile': testfile})

不幸的是,我对这两个框架没有太多经验,我想知道哪里发生了错误沟通。执行此操作时,未填充 myFile 变量(甚至不确定是否应该填充),并且我不确定cherrypy应该如何接收文件。感谢所有帮助!

附言。我收到的错误:

File "/usr/lib/pymodules/python2.7/cherrypy/_cprequest.py", line 656, in respond
response.body = self.handler()
File "/usr/lib/pymodules/python2.7/cherrypy/lib/encoding.py", line 188, in __call__
self.body = self.oldhandler(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/cherrypy/_cpdispatch.py", line 34, in __call__
return self.callable(*self.args, **self.kwargs)
File "/usr/bin/apt-repo", line 194, in upload
data = myFile.file.read(8192)
AttributeError: 'NoneType' object has no attribute 'file'

10.136.26.168 - - [25/Mar/2013:15:51:37] "POST /upload/ HTTP/1.1" 500 1369 "" "python-    requests/0.8.2"

所以我尝试使用默认示例执行此操作。这是我将上传方法更改为:

@cherrypy.expose
def upload(self, myFile=None):
    out = """<html>
    <body>
        myFile length: %s<br />
        myFile filename: %s<br />
        myFile mime-type: %s
    </body>
    </html>"""

    # Although this just counts the file length, it demonstrates
    # how to read large files in chunks instead of all at once.
    # CherryPy reads the uploaded file into a temporary file;
    # myFile.file.read reads from that.
    size = 0
    while True:
        data = myFile.file.read(8192)
        if not data:
            break
        size += len(data)
    print out % (size, myFile.filename, myFile.content_type)
    return out % (size, myFile.filename, myFile.content_type)
"""

非常基础。直接来自cherrypy的文档。

这是我在客户端所做的:

jlsookiki@justin1:~$ ls -lh bnt-beapi_1.9.6_amd64.deb 
-rw-r--r-- 1 jlsookiki users 30K Mar 26 11:20 bnt-beapi_1.9.6_amd64.deb
jlsookiki@justin1:~$ python
>>> import requests
>>> files = open('bnt-beapi_1.9.6_amd64.deb', 'rb')
>>> url = "http://0.0.0.0:8089/upload"
>>> r = requests.post(url, files={'myFile': files})
>>> print r.text
 <html>
        <body>
            myFile length: 0<br />
            myFile filename: bnt-beapi_1.9.6_amd64.deb<br />
            myFile mime-type: application/x-debian-package
        </body>
 </html>

由于某种原因,文件实际上并没有被发送过来,也没有被读取。有人知道为什么会这样吗?

【问题讨论】:

  • 你的代码非常适合我。
  • 有吗?您能够在服务器端正确保存文件吗?
  • 是的。对不起,我希望我遇到了错误。 :)
  • 我对原始问题进行了一些更新/编辑。结果我的传输了所有元数据,但我无法执行实际的文件传输。这可能是什么原因?
  • 代码运行正常,你使用的是哪个版本的 requests/cherrypy?

标签: python rest file-upload cherrypy python-requests


【解决方案1】:

确保在您启动服务器时,cherrpy 代码有权写入您尝试保存文件的位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    相关资源
    最近更新 更多