cherrypy File upload with CherryPy


Example

This example consists of three parts:

  • server.py - CherryPy application that can receive and save a file.
  • webpage.html - Example how to upload a file to server.py from a webpage.
  • cli.py - Example how to upload a file to server.py from a command line tool.
  • Bonus - upload.txt - file that you will upload.

server.py

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

import os
import cherrypy

config = {
    'global' : {
        'server.socket_host' : '127.0.0.1',
        'server.socket_port' : 8080
    }
}


class App:

    @cherrypy.expose
    def upload(self, ufile):
        # Either save the file to the directory where server.py is
        # or save the file to a given path:
        # upload_path = '/path/to/project/data/'
        upload_path = os.path.dirname(__file__)

        # Save the file to a predefined filename
        # or use the filename sent by the client:
        # upload_filename = ufile.filename
        upload_filename = 'saved.txt'

        upload_file = os.path.normpath(
            os.path.join(upload_path, upload_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 = '''
File received.
Filename: {}
Length: {}
Mime-type: {}
''' .format(ufile.filename, size, ufile.content_type, data)
        return out


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

webpage.html

<form method="post" action="http://127.0.0.1:8080/upload" enctype="multipart/form-data">
    <input type="file" name="ufile" />
    <input type="submit" />
</form>

cli.py

This example requires Python requests package, however file can be sent to server in plain Python.

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

import requests

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

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

print(r)
print(r.text)

upload.txt

Hello! This file was uploaded to CherryPy.

Upload from browser

  • Run $ server.py
  • Open webpage.html in your web browser.
  • After you select file from your drive and submit it, it will be saved as saved.txt.

Upload from command line

  • Open one console and run $ server.py
  • Open another console and run $ cli.py
    • Note: Test file upload.txt should be in the same directory with cli.py
  • File upload.txt should be uploaded and saved as saved.txt.