PageRenderTime 34ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Pyro4/test/echoserver.py

https://github.com/frankladen/Orion-avec-cheveux
Python | 128 lines | 90 code | 20 blank | 18 comment | 21 complexity | f4cac42e2d6b7d1bd1af63dd976af09c MD5 | raw file
  1. """
  2. Echo server for test purposes.
  3. This is usually invoked by starting this module as a script:
  4. :command:`python -m Pyro4.test.echoserver`
  5. It is also possible to use the :class:`EchoServer` in user code
  6. but that is not terribly useful.
  7. Pyro - Python Remote Objects. Copyright by Irmen de Jong (irmen@razorvine.net).
  8. """
  9. import sys, os, time
  10. from Pyro4 import threadutil
  11. from Pyro4 import naming
  12. import Pyro4
  13. __all__=["EchoServer"]
  14. class EchoServer(object):
  15. """
  16. The echo server object that is provided as a Pyro object by this module.
  17. If its :attr:`verbose` attribute is set to ``True``, it will print messages as it receives calls.
  18. """
  19. verbose=False
  20. must_shutdown=False
  21. def echo(self, args):
  22. """return the args"""
  23. if self.verbose:
  24. print("%s - echo: %s" % (time.asctime(), args))
  25. return args
  26. def error(self):
  27. """generates a simple exception (division by zero)"""
  28. if self.verbose:
  29. print("%s - error: generating exception" % time.asctime())
  30. return 1//0 # division by zero error
  31. def shutdown(self):
  32. """called to signal the echo server to shut down"""
  33. if self.verbose:
  34. print("%s - shutting down" % time.asctime())
  35. self.must_shutdown=True
  36. class NameServer(threadutil.Thread):
  37. def __init__(self, hostname):
  38. super(NameServer,self).__init__()
  39. self.setDaemon(1)
  40. self.hostname=hostname
  41. self.started=threadutil.Event()
  42. def run(self):
  43. self.uri, self.ns_daemon, self.bc_server = naming.startNS(self.hostname)
  44. self.started.set()
  45. self.ns_daemon.requestLoop()
  46. def startNameServer(host):
  47. ns=NameServer(host)
  48. ns.start()
  49. ns.started.wait()
  50. return ns
  51. def main(args, returnWithoutLooping=False):
  52. from optparse import OptionParser
  53. parser=OptionParser()
  54. parser.add_option("-H","--host", default="localhost", help="hostname to bind server on (default=localhost)")
  55. parser.add_option("-p","--port", type="int", default=0, help="port to bind server on")
  56. parser.add_option("-u","--unixsocket", help="Unix domain socket name to bind server on")
  57. parser.add_option("-n","--naming", action="store_true", default=False, help="register with nameserver")
  58. parser.add_option("-N","--nameserver", action="store_true", default=False, help="also start a nameserver")
  59. parser.add_option("-v","--verbose", action="store_true", default=False, help="verbose output")
  60. parser.add_option("-q","--quiet", action="store_true", default=False, help="don't output anything")
  61. parser.add_option("-k","--key", help="the HMAC key to use")
  62. options,args = parser.parse_args(args)
  63. if options.verbose:
  64. options.quiet=False
  65. if not options.quiet:
  66. print("Starting Pyro's built-in test echo server.")
  67. if os.name!="java":
  68. Pyro4.config.SERVERTYPE="multiplex"
  69. hmac=options.key
  70. if hmac and sys.version_info>=(3,0):
  71. hmac=bytes(hmac,"utf-8")
  72. Pyro4.config.HMAC_KEY=hmac or Pyro4.config.HMAC_KEY
  73. if not options.quiet and Pyro4.config.HMAC_KEY:
  74. print("HMAC_KEY set to: %s" % Pyro4.config.HMAC_KEY)
  75. nameserver=None
  76. if options.nameserver:
  77. options.naming=True
  78. nameserver=startNameServer(options.host)
  79. d=Pyro4.Daemon(host=options.host, port=options.port, unixsocket=options.unixsocket)
  80. echo=EchoServer()
  81. echo.verbose=options.verbose
  82. objectName="test.echoserver"
  83. uri=d.register(echo, objectName)
  84. if options.naming:
  85. host,port=None,None
  86. if nameserver is not None:
  87. host,port=nameserver.uri.host, nameserver.uri.port
  88. ns=naming.locateNS(host,port)
  89. ns.register(objectName, uri)
  90. if options.verbose:
  91. print("using name server at %s" % ns._pyroUri)
  92. else:
  93. if options.verbose:
  94. print("not using a name server.")
  95. if not options.quiet:
  96. print("object name: %s" % objectName)
  97. print("echo uri: %s" % uri)
  98. print("echoserver running.")
  99. if returnWithoutLooping:
  100. return d,echo,uri # for unit testing
  101. else:
  102. d.requestLoop(loopCondition=lambda:not echo.must_shutdown)
  103. d.close()
  104. if __name__=="__main__":
  105. main(sys.argv[1:])