Sign inSign up

pss146/websockets_echo

By pss146

Updated almost 8 years ago

Image
0

172

pss146/websockets_echo repository overview

Dockerfile

FROM python:alpine3.8 RUN pip install websockets COPY echo.py /src/echo.py EXPOSE 8080 CMD ["python", "/src/echo.py"]

WS server example

import asyncio import websockets import socket import sys import os

def get_lan_ip(): ip = socket.gethostbyname(socket.gethostname()) return ip

async def hello(websocket, path): print(f"Incoming request at path: {path}") if path == "/echo": name = await websocket.recv() print(f"< {name}")

  greeting = f"Hello {name}!"

  await websocket.send(greeting)
  print(f"> {greeting}")

hostname = ""

@asyncio.coroutine def prepare(): hostname = get_lan_ip() print(f"Starting server at: {hostname}")

asyncio.get_event_loop().run_until_complete(prepare())

start_server = websockets.serve(hello, hostname, 8080) asyncio.get_event_loop().run_until_complete(start_server) asyncio.get_event_loop().run_forever()

Tag summary

Content type

Image

Digest

Size

30 MB

Last updated

almost 8 years ago

docker pull pss146/websockets_echo