1

I'm having a hard time combining a python requests post request with a cherrypy server in order to upload a file.

Here's what the server side code inside the cherrypy server looks like:

@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)

And the client side (for now) is simply a python requests call: testfile = open('testfile', 'r') request = requests.post("http://:8088/upload/", files={'myFile': testfile})

Unfortunately I don't have too much experience with these two frameworks and I'm wondering where the miscommunication is happening. When this is executed, the myFile variable is not populated (not even sure if it's supposed to be) and I'm not exactly sure how cherrypy is supposed to receive the file. All help is appreciated!

PS. Errors I've received:

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"

So I tried doing this with the default example. Here's what I changed the upload method to:

@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)
"""

Very basic. Came straight out of cherrypy's documentation.

Here's what I'm doing on the client side:

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>

For some reason, the file is not actually getting sent over and is not being read. Anyone have any idea why this is happening?

4

1 回答 1

0

确保当您启动服务器时,cherrpy 代码具有访问/权限以写入您尝试保存文件的位置。

于 2013-03-26T21:19:46.177 回答