Add async socket example.

This commit is contained in:
Joris Vink 2018-11-23 22:34:09 +01:00
parent a030a6fd38
commit 6d78ae04b4
4 changed files with 67 additions and 9 deletions

View File

@ -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
```

View File

@ -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

View File

@ -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
}

View File

@ -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')