daily pastebin goal
11%
SHARE
TWEET

Server

a guest Sep 15th, 2014 2 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3.  
  4. """Node Server Example
  5.  
  6. This example demonstrates how to create a very simple node server
  7. that supports bi-diractional messaging between server and connected
  8. clients forming a cluster of nodes.
  9. """
  10.  
  11.  
  12. from __future__ import print_function
  13.  
  14. from os import getpid
  15. from optparse import OptionParser
  16.  
  17.  
  18. from circuits.node import Node
  19. from circuits import Component, Debugger, Event
  20.  
  21.  
  22. __version__ = "0.0.1"
  23.  
  24. USAGE = "%prog [options]"
  25. VERSION = "%prog v" + __version__
  26.  
  27.  
  28. def parse_options():
  29.     parser = OptionParser(usage=USAGE, version=VERSION)
  30.  
  31.     parser.add_option(
  32.         "-b", "--bind",
  33.         action="store", type="string",
  34.         default="0.0.0.0:8000", dest="bind",
  35.         help="Bind to address:[port]"
  36.     )
  37.  
  38.     parser.add_option(
  39.         "-d", "--debug",
  40.         action="store_true",
  41.         default=False, dest="debug",
  42.         help="Enable debug mode"
  43.     )
  44.  
  45.     opts, args = parser.parse_args()
  46.  
  47.     return opts, args
  48.  
  49.  
  50. class NodeServer(Component):
  51.  
  52.     def init(self, args, opts):
  53.         """Initialize our ``ChatServer`` Component.
  54.  
  55.        This uses the convenience ``init`` method which is called after the
  56.        component is proeprly constructed and initialized and passed the
  57.        same args and kwargs that were passed during construction.
  58.        """
  59.  
  60.         self.args = args
  61.         self.opts = opts
  62.  
  63.         self.clients = {}
  64.  
  65.         if opts.debug:
  66.             Debugger().register(self)
  67.  
  68.         if ":" in opts.bind:
  69.             address, port = opts.bind.split(":")
  70.             port = int(port)
  71.         else:
  72.             address, port = opts.bind, 8000
  73.  
  74.         bind = (address, port)
  75.  
  76.         Node(bind).register(self)
  77.  
  78.     def connect(self, sock, host, port):
  79.         """Connect Event -- Triggered for new connecting clients"""
  80.         self.clients[sock] = {
  81.             "host": sock,
  82.             "port": port,
  83.         }
  84.  
  85.     def disconnect(self, sock):
  86.         """Disconnect Event -- Triggered for disconnecting clients"""
  87.  
  88.         if sock not in self.clients:
  89.             return
  90.  
  91.         del self.clients[sock]
  92.  
  93.     def ready(self, server, bind):
  94.         print("Ready! Listening on {}:{}".format(*bind))
  95.         print("Waiting for remote events...")
  96.  
  97.     def hello(self):
  98.         return "Hello World! ({0:d})".format(getpid())
  99.  
  100.  
  101. def main():
  102.     opts, args = parse_options()
  103.  
  104.     # Configure and "run" the System.
  105.     NodeServer(args, opts).run()
  106.  
  107.  
  108. if __name__ == "__main__":
  109.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top