Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CherryPy upload file

I'm want to POST a file from a python3 client to cherrypy. I'm using the requests library. My client code:

import requests

url = 'http://127.0.0.1:8080/upload'
files = {'file.zip': open('file.zip', 'rb')}

r = requests.post(url, files=files)

My server code:

import os
import tempfile
import shutil

import cherrypy


config = {
    'global' : {
        'server.socket_host' : '127.0.0.1',
        'server.socket_port' : 8080,
        'server.thread_pool' : 8,
        'server.max_request_body_size' : 0,
        'server.socket_timeout' : 60
  }
}


class App:
    @cherrypy.config(**{'response.timeout': 3600})
    @cherrypy.expose()
    def upload(self):
        '''Handle non-multipart upload'''

        destination = os.path.join('/home/uvv/upload')
        with open(destination, 'wb') as f:
            shutil.copyfileobj(cherrypy.request.body, f)

        return 'Okay'


if __name__ == '__main__':
        cherrypy.quickstart(App(), '/', config)

The server returns an error:

127.0.0.1 - - [17/Aug/2016:11:38:49] "POST /upload HTTP/1.1" 400 2083 "" "python-requests/2.10.0"
like image 703
Ulybin Vitaliy Avatar asked May 05 '26 08:05

Ulybin Vitaliy


1 Answers

It's usefull to get information from response. When you send a request, you receive a response. From this response you can get information about HTTP code where 200 means OK and 400 means bad request. That's the text you can see in your cherrypy log: POST /upload HTTP/1.1" 400. To get more information, print the response text using print(r.text)

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import requests

url = 'http://127.0.0.1:9090/upload'
files = {'ufile': open('file.txt', 'rb')}

r = requests.post(url, files=files)

print(r)
print(r.text)

If you use the code above with the code bellow, it's working example of uploading file to cherrypy server.

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
import cherrypy

config = {
    'global' : {
        'server.socket_host' : '127.0.0.1',
        'server.socket_port' : 9090,
        'server.thread_pool' : 8,
        'server.max_request_body_size' : 0,
        'server.socket_timeout' : 60
    }
}


class App:

    @cherrypy.expose
    def upload(self, ufile):
        upload_path = os.path.normpath('/path/to/project/data/')
        upload_file = os.path.join(upload_path, ufile.filename)
        size = 0
        with open(upload_file, 'wb') as out:
            while True:
                data = ufile.file.read(8192)
                if not data:
                    break
                out.write(data)
                size += len(data)
        out = '''
length: {}
filename: {}
mime-type: {}
''' .format(size, ufile.filename, ufile.content_type, data)
        return out


if __name__ == '__main__':
    cherrypy.quickstart(App(), '/', config)

Replace the path /path/to/project/data/ to path that fits your project.

like image 177
dwich Avatar answered May 08 '26 01:05

dwich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!