]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Include pared-down version of echo server in docs
authorMichael V. DePalatis <mike@depalatis.net>
Wed, 10 Aug 2016 20:04:20 +0000 (22:04 +0200)
committerMichael V. DePalatis <mike@depalatis.net>
Fri, 12 Aug 2016 06:27:04 +0000 (08:27 +0200)
tornado/tcpserver.py

index 0839d392374b1e0f0d8ae46d55f42c0c8ea368df..54837f7a65352a664b0c34fc7d655581d7c22445 100644 (file)
@@ -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