PageRenderTime 278ms CodeModel.GetById 11ms RepoModel.GetById 4ms app.codeStats 0ms

/docs/source/guides/server.rst

https://bitbucket.org/prologic/circuits/
ReStructuredText | 109 lines | 68 code | 41 blank | 0 comment | 0 complexity | daaf5d16fc4cd6f272b52f54c889aaab MD5 | raw file
  1. How to build a Simple Server
  2. ============================
  3. Overview
  4. --------
  5. In this guide we're going to walk through the steps required to build a
  6. simple chat server. Users will connect using a standard telnet client and
  7. start chatting with other users that are connected.
  8. Prerequisites
  9. .............
  10. - `Python <http://www.python.org>`_
  11. - `circuits <http://pypi.python.org/circuits>`_
  12. Components
  13. ..........
  14. - :py:class:`~circuits.core.components.Component`
  15. - :py:class:`~circuits.net.sockets.TCPServer`
  16. Step 1 - Setting up
  17. -------------------
  18. Let's start off by importing the components we need:
  19. .. code-block:: python
  20. #!/usr/bin/env python
  21. from circuits import Component
  22. from circuits.net.sockets import TCPServer, Write
  23. Step 2 - Building the Server
  24. ----------------------------
  25. Next let's define our ``Server`` Component with a simple event handler that
  26. broadcats all incoming messages to every connected client.
  27. .. code-block:: python
  28. class Server(Component):
  29. def __init__(self, host, port=8000):
  30. super(Server, self).__init__()
  31. self._clients = []
  32. TCPServer((host, port)).register(self)
  33. def connect(self, sock, host, port):
  34. self._clients.append(sock)
  35. def disconnect(self, sock):
  36. self._clients.remove(sock)
  37. def read(self, sock, data):
  38. for client in self._clients:
  39. if not client == sock:
  40. self.fire(Write(client, data.strip()))
  41. Server("localhost").run()
  42. Let's walk through this in details:
  43. 1. Create a new Component called ``Server``
  44. 2. Define its initialization arguments as ``(host, port=8000)``
  45. 3. Call the super constructor of the underlying Component
  46. (*This is important as all components need to be initialized properly*)
  47. 4. Register a ``TCPServer`` Component and configure it.
  48. 5. Create Event Handlers for:
  49. - Dealgin with new connecting clients.
  50. - Dealing with clients whom have disconnected
  51. - Dealing with messages from connected clients
  52. Step 3 - Running the Server
  53. ---------------------------
  54. The last step is simply to create an instance of the ``Server`` Component
  55. and run it (*making sure to configure it with a host and port*).
  56. .. code-block:: python
  57. Server("localhost").run()
  58. That's it!
  59. Using a standard telnet client try connecting to localhost on port 8000.
  60. Try connecting a second client and watch what happens in the 2nd client
  61. when you type text into the 1st.
  62. Enjoy!
  63. Source Code
  64. -----------
  65. .. literalinclude:: server.py
  66. :language: python
  67. :linenos:
  68. :download:`Download server.py <server.py>`