I'm trying to transfer a file to the CherryPy server from a client in Python 3.

Client part:

import requests url = 'http://127.0.0.1:8080/upload' files = {'file.zip': open('file.zip', 'rb')} r = requests.post(url, files=files) print(r.status_code == requests.codes.ok) 

Server part:

 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) 

Accordingly, the server returns: 127.0.0.1 - - [17 / Aug / 2016: 11: 38: 49] "POST / upload HTTP / 1.1" 400 2083 "" "python-requests / 2.10.0" And the client: False I can not understand why the file is not loading. Yes, the rights to the / home / uvv / upload folder are a + rwx (therefore, the problem is definitely not the permissions)

  • 1) The file is downloaded from the client to the test server, for example, in php. 2) The server accepts a file sent through a post request by the same curl for example? - Igor
  • @Igor what does php have to do with it? - FeroxTL
  • Check out what is in cherrypy.request.body. Class App not from the object should be inherited? And judging by the example of the file is not in the request is - FeroxTL
  • @FeroxTL php here despite the fact that people do not try to understand, without separate verification, to understand the reason is very difficult. On php there are no restrictions as for python, in python in most frameworks built-in protection. - Igor
  • @Igor let's more specifics. Your statement is very debatable. In order not to be unfounded, I declare: by default, protection is enabled only in django. Bottlepy — no, flask — no, cherrypy — no. Naked python (wsgi) - no. Where are your most protected frameworks? - FeroxTL

2 answers 2

Solution checked under Ubuntu Client part:

 import requests url = 'http://127.0.0.1:8080/upload' files = {'ufile': open('file.zip', 'rb')} r = requests.post(url, files=files) print(r.status_code == requests.codes.ok) print(r) print(r.text) 

Server part:

 import os 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(object): @cherrypy.expose def upload(self, ufile): upload_path = os.path.normpath('/home/user/upload') 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) 

The path '/ home / user / upload' needs to be changed to your own.

    1. Try to download the file with curl
    2. Look at what exactly the r.status_code code r.status_code (and not just that it is not equal to ok ).