-1

I have currently a small gevent Python application serving HTML files. I would like to be able to upload a small file.

A form is made to send a file to the /file_upload path.

What should I do to receive the file on the Python side to save it on disk?


Currently I am sending a 200 OK response:

def __call__(self, environ, start_response):
    """function used to serve files following an http request"""
    path = environ['PATH_INFO'].strip('/') or 'index.html'
    if path == 'file_upload':
        start_response('200 OK',[('Content-Type', 'text/html')])
        return 'OK'
leszek.hanusz
  • 5,152
  • 2
  • 38
  • 56

1 Answers1

-1

It is possible to get the file contents with environ['wsgi.input'].read()

def __call__(self, environ, start_response):
    """function used to serve files following an http request"""
    path = environ['PATH_INFO'].strip('/') or 'index.html'
    if path == 'file_upload':
        data = environ['wsgi.input'].read(int(environ.get('CONTENT_LENGTH','0')))
        f = open('./uploaded_file', 'wb')
        f.write(data)
        f.close()
        start_response('200 OK',[('Content-Type', 'text/plain')])
        return 'OK'
leszek.hanusz
  • 5,152
  • 2
  • 38
  • 56