mirror of
https://github.com/jorisvink/kore
synced 2025-03-09 04:29:02 -04:00
Simplify the echo example, kore can just run the file nowadays.
This commit is contained in:
parent
582e18d2ec
commit
5e84ebdab2
11
examples/python-echo/README.md
Normal file
11
examples/python-echo/README.md
Normal file
@ -0,0 +1,11 @@
|
||||
Example of using the asynchronous python api to create a simple
|
||||
echo server.
|
||||
|
||||
Kore must have been built with PYTHON=1.
|
||||
|
||||
On the command-line run the following
|
||||
|
||||
$ kore echo.py
|
||||
|
||||
Then connect to 127.0.0.1 port 6969 using netcat or so and you'll
|
||||
see it echo back everything you send it.
|
@ -1,34 +0,0 @@
|
||||
# python-echo 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.
|
||||
single_binary=yes
|
||||
kore_source=../../
|
||||
kore_flavor=NOTLS=1 PYTHON=1
|
||||
|
||||
# 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.
|
||||
#}
|
@ -1,3 +0,0 @@
|
||||
# python-echo configuration
|
||||
|
||||
python_import src/echo.py
|
@ -1,67 +0,0 @@
|
||||
#
|
||||
# Copyright (c) 2013-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.
|
||||
#
|
||||
|
||||
import kore
|
||||
import socket
|
||||
|
||||
class EchoServer:
|
||||
# Setup socket + wrap it inside of a kore socket so we can use it.
|
||||
def __init__(self):
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.setblocking(False)
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
sock.bind(("127.0.0.1", 6969))
|
||||
sock.listen()
|
||||
|
||||
self.conn = kore.socket_wrap(sock)
|
||||
|
||||
# Wait for a new client to connect, then create a new task
|
||||
# that calls handle_client with the ocnnected client as
|
||||
# the argument.
|
||||
async def run(self):
|
||||
while True:
|
||||
try:
|
||||
client = await self.conn.accept()
|
||||
kore.task_create(self.handle_client(client))
|
||||
client = None
|
||||
except Exception as e:
|
||||
kore.fatal("exception %s" % e)
|
||||
|
||||
# Each client will run as this co-routine.
|
||||
# In this case we pass a timeout of 1 second to the recv() call
|
||||
# which will throw a TimeoutError exception in case the timeout
|
||||
# is hit before data is read from the socket.
|
||||
#
|
||||
# This timeout argument is optional. If none is specified the call
|
||||
# will wait until data becomes available.
|
||||
async def handle_client(self, client):
|
||||
while True:
|
||||
try:
|
||||
data = await client.recv(1024, 1000)
|
||||
if data is None:
|
||||
break
|
||||
await client.send(data)
|
||||
except TimeoutError as e:
|
||||
print("timed out reading (%s)" % e)
|
||||
except Exception as e:
|
||||
print("client got exception %s" % e)
|
||||
client.close()
|
||||
|
||||
# Setup the server object.
|
||||
server = EchoServer()
|
||||
|
||||
# Create a task that will execute inside of Kore as a co-routine.
|
||||
kore.task_create(server.run())
|
@ -1,25 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020 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/hooks.h>
|
||||
|
||||
/* Let kore handle the default option parsing. */
|
||||
void
|
||||
kore_parent_configure(int argc, char **argv)
|
||||
{
|
||||
kore_default_getopt(argc, argv);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user