From: Michael V. DePalatis Date: Wed, 10 Aug 2016 20:04:20 +0000 (+0200) Subject: Include pared-down version of echo server in docs X-Git-Tag: v4.5.0~80^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=39c3d95ddcb19fd53a1e1226207ec396d284a5e4;p=thirdparty%2Ftornado.git Include pared-down version of echo server in docs --- diff --git a/tornado/tcpserver.py b/tornado/tcpserver.py index 0839d3923..54837f7a6 100644 --- a/tornado/tcpserver.py +++ b/tornado/tcpserver.py @@ -39,7 +39,21 @@ class TCPServer(object): r"""A non-blocking, single-threaded TCP server. To use `TCPServer`, define a subclass which overrides the `handle_stream` - method. + method. For example, a simple echo server could be defined like this:: + + from tornado.tcpserver import TCPServer + from tornado.iostream import StreamClosedError + from tornado import gen + + class EchoServer(TCPServer): + @gen.coroutine + def handle_stream(self, stream, address): + while True: + try: + data = yield stream.read_until(b"\n") + yield stream.write(data) + except StreamClosedError: + break To make this server serve SSL traffic, send the ``ssl_options`` keyword argument with an `ssl.SSLContext` object. For compatibility with older