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 21bf7f95837c44854767c1cc5ae2c1f111033087
parent 01e3ef6cd3efc0a58d4f21fc811ae99515098ed4
Author: Joris Vink <joris@coders.se>
Date:   Mon,  6 Feb 2017 12:21:40 +0100

rework base example a bit.

Diffstat:
examples/python/conf/python.conf | 1-
examples/python/src/index.py | 39++++++---------------------------------
examples/python/src/upload.py | 8+++-----
examples/python/src/websockets.py | 2--
4 files changed, 9 insertions(+), 41 deletions(-)

diff --git a/examples/python/conf/python.conf b/examples/python/conf/python.conf @@ -32,7 +32,6 @@ domain * { static /c cpage static /b minimal static /json json_parse - static /state state_test static /ws ws_connect static /upload upload diff --git a/examples/python/src/index.py b/examples/python/src/index.py @@ -97,7 +97,6 @@ def page(req): kore.log(kore.LOG_INFO, "got id of %s" % id) req.response_header("content-type", "text/plain") req.response(200, "hello 1234".encode("utf-8")) - return kore.RESULT_OK # # Handler that parses the incoming body as JSON and dumps out some things. @@ -105,42 +104,16 @@ def page(req): def json_parse(req): if req.method != kore.METHOD_PUT: req.response(400, b'') - return kore.RESULT_OK - - data = json.loads(req.body) - kore.log(kore.LOG_INFO, "loaded json %s" % data) - if data["hello"] == 123: - kore.log(kore.LOG_INFO, "hello is 123!") - - req.response(200, "ok".encode("utf-8")) - return kore.RESULT_OK - -# -# Handler that stores some python state in req.state that it reuses -# once the handler is called again by the event loop (after having -# returned RESULT_RETRY to the event loop). -# -def state_test(req): - # If we don't have a state this is the first time we're called. - if req.state is None: - kore.log(kore.LOG_INFO, "state_test: first time") - req.state = "hello world" - - # Tell Kore to call us again next event loop. - return kore.RESULT_RETRY - - # We have been called before. - kore.log(kore.LOG_INFO, "state_test: second time, with %s" % req.state) - req.response(200, req.state.encode("utf-8")) - - # We *MUST* reset state back to None before returning RESULT_OK - req.state = None; + else: + data = json.loads(req.body) + kore.log(kore.LOG_INFO, "loaded json %s" % data) + if data["hello"] == 123: + kore.log(kore.LOG_INFO, "hello is 123!") - return kore.RESULT_OK + req.response(200, "ok".encode("utf-8")) # # Small handler, returns 200 OK. # def minimal(req): req.response(200, b'') - return kore.RESULT_OK diff --git a/examples/python/src/upload.py b/examples/python/src/upload.py @@ -27,7 +27,7 @@ def upload(req): if req.method is not kore.METHOD_POST: req.response_header("allow", "post") req.response(400, b'') - return kore.RESULT_OK + return # Ask kore to parse incoming multipart data. req.populate_multi() @@ -36,7 +36,7 @@ def upload(req): file = req.file_lookup("file") if not file: req.response(400, b'') - return kore.RESULT_OK + return kore.log(kore.LOG_INFO, "%s (%s, filename=%s)" % (file, file.name, file.filename)) @@ -45,7 +45,7 @@ def upload(req): f = open(file.filename, "wb") if not f: req.response(500, b'') - return kore.RESULT_OK + return # Read all data from incoming file and write it to the output file. len = True @@ -56,5 +56,3 @@ def upload(req): f.close() req.response(200, b'') - - return kore.RESULT_OK diff --git a/examples/python/src/websockets.py b/examples/python/src/websockets.py @@ -62,5 +62,3 @@ def ws_connect(req): req.websocket_handshake("onconnect", "onmessage", "ondisconnect") except: req.response(500, b'') - - return kore.RESULT_OK