commit 20d74610ce5f3731acdd41ac8566baf0c6625b2a Author: Alexander Mahr Date: Sun Sep 29 09:05:27 2024 +0200 create websocket-docket test repo diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c03b4bc --- /dev/null +++ b/LICENSE @@ -0,0 +1,50 @@ +Copyright (c) Aymeric Augustin and contributors + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of the copyright holder nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +Modifications are +Copyright (c) Alexander Mahr 2024 Berlin + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of the copyright holder nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.DAMAGE diff --git a/README.md b/README.md new file mode 100644 index 0000000..e22fa73 --- /dev/null +++ b/README.md @@ -0,0 +1,81 @@ +# A compose containerized test of websockets + +* based on containers +* can run rootless, in docker, nerdctl or podman +* uses alpine:edge +* uses python3 py3-websockets (i.e. version 13+) + +## Adopted code + +[https://websockets.readthedocs.io/en/13.1/howto/quickstart.html](https://websockets.readthedocs.io/en/13.1/howto/quickstart.html) + +## Usage + +clone the repo +``` +docker compose up +``` + +![Screenshot](images/screenshot1.png "Screenshot" ) + +## Copyright and LICENSES + +* client/entrypoint.py and server/entrypoint.py are copyrighted and licensed under +``` +Copyright (c) Aymeric Augustin and contributors + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of the copyright holder nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +``` +* all modificatios are Copyright 2024 Alexander Mahr and licensed as: +``` +Copyright (c) Alexander Mahr Berlin + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of the copyright holder nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +``` + + + diff --git a/client/Dockerfile b/client/Dockerfile new file mode 100644 index 0000000..2c7dbb9 --- /dev/null +++ b/client/Dockerfile @@ -0,0 +1,5 @@ +FROM alpine:edge +RUN apk update && apk add py3-websockets +COPY --chmod=0555 entrypoint.sh /entrypoint.sh +COPY --chmod=0555 entrypoint.py /entrypoint.py +ENTRYPOINT ["/entrypoint.sh"] diff --git a/client/entrypoint.py b/client/entrypoint.py new file mode 100755 index 0000000..22b5a48 --- /dev/null +++ b/client/entrypoint.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python + +import asyncio +import os + +from websockets.asyncio.client import connect + +async def hello(): + uri = "ws://server:1080" + async with connect(uri) as websocket: + print(f"Get username via:\n name = os.environ.get('USER')") + name = os.environ.get('USER') + # name = name == NULL ? std::string() : std::string(name); + await websocket.send(name) + print(f">>> {name}") + + greeting = await websocket.recv() + print(f"<<< {greeting}") + +if __name__ == "__main__": + asyncio.run(hello()) diff --git a/client/entrypoint.sh b/client/entrypoint.sh new file mode 100755 index 0000000..e8651af --- /dev/null +++ b/client/entrypoint.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +exec /entrypoint.py diff --git a/compose.yml b/compose.yml new file mode 100644 index 0000000..41c95a4 --- /dev/null +++ b/compose.yml @@ -0,0 +1,19 @@ +services: + # the server + server: + stop_grace_period: 1s + environment: + PYTHONUNBUFFERED: true + build: 'server' + expose: + - 1080 + # the client + client: + stop_grace_period: 1s + environment: + USER: ${USER} + build: 'client' + restart: on-failure + depends_on: + server: + condition: service_started diff --git a/images/screenshot1.png b/images/screenshot1.png new file mode 100644 index 0000000..de9b085 Binary files /dev/null and b/images/screenshot1.png differ diff --git a/server/Dockerfile b/server/Dockerfile new file mode 100644 index 0000000..2c7dbb9 --- /dev/null +++ b/server/Dockerfile @@ -0,0 +1,5 @@ +FROM alpine:edge +RUN apk update && apk add py3-websockets +COPY --chmod=0555 entrypoint.sh /entrypoint.sh +COPY --chmod=0555 entrypoint.py /entrypoint.py +ENTRYPOINT ["/entrypoint.sh"] diff --git a/server/entrypoint.py b/server/entrypoint.py new file mode 100755 index 0000000..133fe3a --- /dev/null +++ b/server/entrypoint.py @@ -0,0 +1,23 @@ +#!/usr/bin/python +##!/usr/bin/env python + +import asyncio + +from websockets.asyncio.server import serve + +async def hello(websocket): + name = await websocket.recv() + print(f"<<< {name}") + + greeting = f"Hello {name}!" + + await websocket.send(greeting) + print(f">>> {greeting}") + +async def main(): + async with serve(hello, "0.0.0.0", 1080): + await asyncio.get_running_loop().create_future() # run forever + +if __name__ == "__main__": + asyncio.run(main()) + diff --git a/server/entrypoint.sh b/server/entrypoint.sh new file mode 100755 index 0000000..a55b7bb --- /dev/null +++ b/server/entrypoint.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +test "" = "$*" || { exec "$@"; } +echo "running /entrypoint.py" +exec /entrypoint.py