flameserver.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """
  2. Pyro FLAME: Foreign Location Automatic Module Exposer.
  3. Easy but potentially very dangerous way of exposing remote modules and builtins.
  4. This is the commandline server.
  5. You can start this module as a script from the command line, to easily get a
  6. flame server running:
  7. :command:`python -m Pyro4.utils.flameserver`
  8. or simply: :command:`pyro4-flameserver`
  9. You have to explicitly enable Flame first though by setting the FLAME_ENABLED config item.
  10. Pyro - Python Remote Objects. Copyright by Irmen de Jong (irmen@razorvine.net).
  11. """
  12. import sys
  13. import Pyro4.utils.flame
  14. import Pyro4.core
  15. def main(args=None, returnWithoutLooping=False):
  16. from optparse import OptionParser
  17. parser = OptionParser()
  18. parser.add_option("-H", "--host", default="localhost", help="hostname to bind server on (default=%default)")
  19. parser.add_option("-p", "--port", type="int", default=0, help="port to bind server on")
  20. parser.add_option("-u", "--unixsocket", help="Unix domain socket name to bind server on")
  21. parser.add_option("-q", "--quiet", action="store_true", default=False, help="don't output anything")
  22. parser.add_option("-k", "--key", help="the HMAC key to use")
  23. options, args = parser.parse_args(args)
  24. if not options.quiet:
  25. print("Starting Pyro Flame server.")
  26. hmac = (options.key or "").encode("utf-8")
  27. if not hmac and not options.quiet:
  28. print("Warning: HMAC key not set. Anyone can connect to this server!")
  29. Pyro4.config.SERIALIZERS_ACCEPTED = {"pickle"} # flame requires pickle serializer, doesn't work with the others.
  30. daemon = Pyro4.core.Daemon(host=options.host, port=options.port, unixsocket=options.unixsocket)
  31. if hmac:
  32. daemon._pyroHmacKey = hmac
  33. uri = Pyro4.utils.flame.start(daemon)
  34. if not options.quiet:
  35. print("server uri: %s" % uri)
  36. print("server is running.")
  37. if returnWithoutLooping:
  38. return daemon, uri # for unit testing
  39. else:
  40. daemon.requestLoop()
  41. daemon.close()
  42. return 0
  43. if __name__ == "__main__":
  44. sys.exit(main())