kore

Kore is a web application platform for writing scalable, concurrent web based processes in C or Python.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit 7fb47da5866dea3cbfe206343f7c4c49cc29b2a8
parent 6578fc65894026cb3fdbb49adf7f4a3f225c2aae
Author: Joris Vink <joris@coders.se>
Date:   Wed,  1 Feb 2017 21:35:44 +0100

add http_file example to python example.

Diffstat:
examples/python/conf/python.conf | 2++
examples/python/src/upload.py | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 61 insertions(+), 0 deletions(-)

diff --git a/examples/python/conf/python.conf b/examples/python/conf/python.conf @@ -5,6 +5,7 @@ load ./python.so onload # import both python modules. python_import src/index.py onload python_import src/websockets.py +python_import src/upload.py bind 127.0.0.1 8888 @@ -33,6 +34,7 @@ domain * { static /json json_parse static /state state_test static /ws ws_connect + static /upload upload # Use the builtin asset_serve_* to serve frontend HTML. static /wspage asset_serve_frontend_html diff --git a/examples/python/src/upload.py b/examples/python/src/upload.py @@ -0,0 +1,59 @@ +# +# Copyright (c) 2017 Joris Vink <joris@coders.se> +# +# Permission to use, copy, modify, and distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +# + +# Processing incoming files in a multipart form. + +import sys +import kore + +# +# This handler receives a POST with a multipart data. +# It extracts the file called "file" and writes it to a new file. +# +def upload(req): + # We only allow POST's. + if req.method is not kore.METHOD_POST: + req.response_header("allow", "post") + req.response(400, b'') + return kore.RESULT_OK + + # Ask kore to parse incoming multipart data. + req.populate_multi() + + # Lookup the file called "file". + file = req.file_lookup("file") + if not file: + req.response(400, b'') + return kore.RESULT_OK + + kore.log(kore.LOG_INFO, + "%s (%s, filename=%s)" % (file, file.name, file.filename)) + + # Open target file. + f = open(file.filename, "wb") + if not f: + req.response(500, b'') + return kore.RESULT_OK + + # Read all data from incoming file and write it to the output file. + len = True + while len: + len, bytes = file.read(1024) + kore.log(kore.LOG_INFO, "got %d bytes of data" % len) + f.write(bytes) + + req.response(200, b'') + return kore.RESULT_OK