remove stale python example.

This commit is contained in:
Joris Vink 2019-02-25 10:00:59 +01:00
parent bf1e8e5ffb
commit 1e7ccc2adf
9 changed files with 0 additions and 476 deletions

View File

@ -1,6 +0,0 @@
*.o
.flavor
.objs
python.so
assets.h
cert

View File

@ -1,18 +0,0 @@
Kore python module example.
This application requires kore to be built with PYTHON=1.
It mixes native code (dso) with python code.
Run:
```
$ kodev run
```
Test:
```
$ curl -k https://127.0.0.1:8888
$ curl -k https://127.0.0.1:8888/state
$ curl -k https://127.0.0.1:8888/auth
$ curl -X PUT -d '{\"hello\": 123}' https://127.0.0.1:8888/json
```

View File

@ -1,60 +0,0 @@
<!DOCTYPE>
<html>
<head>
<script>
var socket = null;
var sent = 0;
var recv = 0;
var length = 65536;
function open(evt) {
var msg = "";
var alphabet = "abcdefghijklmnopqrstuvwxyz";
for (i = 0; i < length; i++)
msg += alphabet.charAt(Math.floor(Math.random() * alphabet.length));
message(msg);
}
function message(msg) {
socket.send(msg);
sent = sent + 1;
update();
}
function update() {
var cnt = document.getElementById("counter");
cnt.innerHTML = "Recv: " + recv + " Sent: " + sent;
}
function onmessage(evt) {
recv = recv + 1;
update();
message(evt.data);
}
function connect() {
socket = new WebSocket("wss://127.0.0.1:8888/ws");
socket.onopen = function(evt) { open(evt) };
socket.onclose = function(evt) { alert("closed"); };
socket.onmessage = function(evt) { onmessage(evt) };
socket.onerror = function(evt) { alert("onerror"); };
}
</script>
</head>
<body>
<form action="/" onsubmit="connect(); return false;">
<input type="submit" value="connect">
</form>
<div id="counter">
</div>
</body>
</html>

View File

@ -1,35 +0,0 @@
# python build config
# You can switch flavors using: kodev flavor [newflavor]
# Set to yes if you wish to produce a single binary instead
# of a dynamic library. If you set this to yes you must also
# set kore_source together with kore_flavor and update ldflags
# to include the appropriate libraries you will be linking with.
#single_binary=no
#kore_source=/home/joris/src/kore
#kore_flavor=
# The flags below are shared between flavors
cflags=-Wall -Wmissing-declarations -Wshadow
cflags=-Wstrict-prototypes -Wmissing-prototypes
cflags=-Wpointer-arith -Wcast-qual -Wsign-compare
cxxflags=-Wall -Wmissing-declarations -Wshadow
cxxflags=-Wpointer-arith -Wcast-qual -Wsign-compare
# Mime types for assets served via the builtin asset_serve_*
#mime_add=txt:text/plain; charset=utf-8
#mime_add=png:image/png
mime_add=html:text/html; charset=utf-8
dev {
# These flags are added to the shared ones when
# you build the "dev" flavor.
cflags=-g
cxxflags=-g
}
#prod {
# You can specify additional flags here which are only
# included if you build with the "prod" flavor.
#}

View File

@ -1,57 +0,0 @@
# python configuration
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
tls_dhparam dh2048.pem
validator v_id function c_validator
validator v_p_id function python_validator
validator v_auth function python_auth
websocket_maxframe 65536
websocket_timeout 20
authentication auth {
authentication_type request
authentication_validator v_auth
}
domain * {
certfile cert/server.pem
certkey cert/key.pem
# Mix page handlers between native and python.
static / page
static /c cpage
static /b minimal
static /json json_parse
static /ws ws_connect
static /upload upload
static /kaka kaka
# Use the builtin asset_serve_* to serve frontend HTML.
static /wspage asset_serve_frontend_html
static /auth page auth
#
# On the native page handler, use a python validator.
#
params qs:get /c {
validate id v_p_id
}
#
# On the python page handler, use a native validator.
#
params qs:get / {
validate id v_id
}
}

View File

@ -1,129 +0,0 @@
#
# Copyright (c) 2017-2018 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.
#
# This is a simple python module that can be loaded into Kore.
# It demonstrates some basic abilities to deal with HTTP requests.
# Pull in the kore stuff.
import kore
# Pull in python JSON parsing.
import json
#
# A validator that the configuration for this application uses to determine
# if a request fulfills the requirements to pass an authentication block.
#
# See the configuration for more.
#
def python_auth(req, data):
kore.log(kore.LOG_NOTICE, "python auth called %s" % data)
return kore.RESULT_OK
#
# Define a validator that kore can use via the configuration to validate
# something before allowing access to it.
#
def python_validator(req, data):
kore.log(kore.LOG_NOTICE, "python validator called %s" % data)
return kore.RESULT_OK
#
# This function is called when our python module is loaded/unloaded.
# The action param is kore.MODULE_LOAD or kore.MODULE_UNLOAD respectively.
#
def onload(action):
kore.log(kore.LOG_INFO, "python module onload called with %d!" % action)
return kore.RESULT_OK
# Called by Kore when the parent is starting.
def kore_parent_configure():
# Listen on an additional interface and port.
kore.listen("127.0.0.1", "8889", "")
kore.log(kore.LOG_INFO, "kore_parent_configure called!")
# Called by Kore when the worker is starting.
def kore_worker_configure():
kore.log(kore.LOG_INFO, "kore_worker_configure called!")
#
# Test page handler that displays some debug information as well as
# fetches the "xframe" header from the request and logs it if present.
#
# If the request is a POST then we read the body up to 1024 bytes in
# one go and display the result and bytes read in the log.
#
# If it's a GET request attempts to find the "id" argument and presents
# it to the user.
#
def page(req):
kore.log(kore.LOG_INFO,
"%s path is %s - host is %s" % (req, req.path, req.host))
kore.log(kore.LOG_INFO, "connection is %s" % req.connection)
xframe = req.request_header("xframe")
if xframe != None:
kore.log(kore.LOG_INFO, "xframe header present: '%s'" % xframe)
if req.method == kore.HTTP_METHOD_POST:
try:
length, body = req.body_read(1024)
kore.log(kore.LOG_INFO, "POST and got %d bytes! (%s)" %
(length, body.decode("utf-8")))
except RuntimeError as r:
kore.log(kore.LOG_INFO, "oops runtime error %s" % r)
req.response(500, b'')
except:
kore.log(kore.LOG_INFO, "oops other error")
req.response(500, b'')
else:
req.response_header("content-type", "text/plain")
req.response(200, body)
else:
req.populate_get()
id = req.argument("id")
if id != None:
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"))
#
# Handler that parses the incoming body as JSON and dumps out some things.
#
def json_parse(req):
if req.method != kore.HTTP_METHOD_PUT:
req.response(400, b'')
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!")
req.response(200, "ok".encode("utf-8"))
#
# Small handler, returns 200 OK.
#
def minimal(req):
req.response(200, b'')
#
# Small handler that grabs a cookie if set.
#
def kaka(req):
req.populate_cookies()
cookie = req.cookie("hello")
if cookie is not None:
kore.log(kore.LOG_INFO, "got hello with value %s" % cookie)
req.response(200, b'')

View File

@ -1,49 +0,0 @@
/*
* Copyright (c) 2017-2018 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.
*/
#include <kore/kore.h>
#include <kore/http.h>
/*
* Just some examples of things that can be mixed with python modules.
*/
int onload(int);
int cpage(struct http_request *);
int c_validator(struct http_request *, void *);
int
c_validator(struct http_request *req, void *data)
{
kore_log(LOG_NOTICE, "c_validator(): called!");
return (KORE_RESULT_OK);
}
int
onload(int action)
{
kore_log(LOG_NOTICE, "onload called from native");
return (KORE_RESULT_OK);
}
int
cpage(struct http_request *req)
{
http_populate_get(req);
http_response(req, 200, "native", 6);
return (KORE_RESULT_OK);
}

View File

@ -1,58 +0,0 @@
#
# Copyright (c) 2017-2018 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 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.HTTP_METHOD_POST:
req.response_header("allow", "post")
req.response(400, b'')
return
# 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.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
# 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)
f.close()
req.response(200, b'')

View File

@ -1,64 +0,0 @@
#
# Copyright (c) 2018 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.
#
# Using kore websockets via python.
import kore
#
# Our connection callback, gets called for each new websocket connection.
#
def onconnect(c):
kore.log(kore.LOG_INFO, "%s: py connected" % c)
#
# Each websocket arriving on a connection triggers this function.
#
# It receives the connection object, the opcode (TEXT/BINARY) and the
# actual data received.
#
# In this example we use the websocket_broadcast() method from kore to
# simply relay the message to all other connection clients.
#
# If you want to send data directly back to the connection you can
# use kore.websocket_send(connection, op, data)
#
def onmessage(c, op, data):
kore.websocket_broadcast(c, op, data, kore.WEBSOCKET_BROADCAST_GLOBAL)
#c.websocket_send(op, data)
#
# Called for every connection that goes byebye.
#
def ondisconnect(c):
kore.log(kore.LOG_INFO, "%s: py disconnecting" % c)
#
# The /ws connection handler. It establishes the websocket connection
# after a request was made for it.
#
# Note that the websocket_handshake() method for the request takes 3
# parameters which are the connection callback, message callback and
# disconnect callback.
#
# These are given as strings to Kore which will then resolve them
# in all modules which means you can give native callbacks here as well.
#
def ws_connect(req):
try:
req.websocket_handshake("onconnect", "onmessage", "ondisconnect")
except:
req.response(500, b'')