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 6d78ae04b4f3b9cd7eeba77b7ec7f69303c140d1
parent a030a6fd38f4398fc9898b97d0257bc00347243c
Author: Joris Vink <joris@coders.se>
Date:   Fri, 23 Nov 2018 22:34:09 +0100

Add async socket example.

Diffstat:
examples/python-async/README.md | 7++++---
examples/python-async/conf/build.conf | 2+-
examples/python-async/conf/python-async.conf | 9++++-----
examples/python-async/src/async_socket.py | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 67 insertions(+), 9 deletions(-)

diff --git a/examples/python-async/README.md b/examples/python-async/README.md @@ -7,7 +7,8 @@ Run: Test: ``` - $ curl -k https://127.0.0.1:8888/queue - $ curl -k https://127.0.0.1:8888/lock - $ curl -k https://127.0.0.1:8888/proc + $ curl -k http://127.0.0.1:8888/queue + $ curl -k http://127.0.0.1:8888/lock + $ curl -k http://127.0.0.1:8888/proc + $ curl -k http://127.0.0.1:8888/socket ``` diff --git a/examples/python-async/conf/build.conf b/examples/python-async/conf/build.conf @@ -6,7 +6,7 @@ # set kore_source together with kore_flavor. single_binary=yes kore_source=../../ -kore_flavor=PYTHON=1 +kore_flavor=PYTHON=1 NOTLS=1 # The flags below are shared between flavors cflags=-Wall -Wmissing-declarations -Wshadow diff --git a/examples/python-async/conf/python-async.conf b/examples/python-async/conf/python-async.conf @@ -2,20 +2,19 @@ bind 127.0.0.1 8888 -tls_dhparam dh2048.pem - python_path src python_import ./src/setup.py python_import ./src/async_lock.py python_import ./src/async_queue.py python_import ./src/async_process.py +python_import ./src/async_socket.py domain * { - certfile cert/server.pem - certkey cert/key.pem - static /queue async_queue static /lock async_lock static /proc async_proc + + static /socket async_socket + static /socket-test socket_test } diff --git a/examples/python-async/src/async_socket.py b/examples/python-async/src/async_socket.py @@ -0,0 +1,58 @@ +# +# 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. +# + +# +# Simple socket example. +# +# The handler will asynchronously connect to the kore app itself and +# send an GET request to /socket-test and read the response. + +import kore +import socket + +async def async_socket(req): + # Create the socket using Pythons built-in socket class. + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + + # Set it to nonblocking. + sock.setblocking(False) + + # Create a kore.socket with kore.socket_wrap(). + conn = kore.socket_wrap(sock) + + # Asynchronously connect to 127.0.0.1 port 8888 + await conn.connect("127.0.0.1", 8888) + kore.log(kore.LOG_INFO, "connected!") + + # Now send the GET request + msg = "GET /socket-test HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n" + await conn.send(msg.encode()) + kore.log(kore.LOG_INFO, "request sent!") + + # Read the response. + data = await conn.recv(8192) + kore.log(kore.LOG_INFO, "got response!") + + # Respond with the response from /socket-test. + req.response(200, data) + + # Close the underlying socket, no need to close the wrapped kore.socket + sock.close() + +async def socket_test(req): + # Delay response a bit, just cause we can. + await kore.suspend(5000) + req.response(200, b'response from /socket-test')