flameserver.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. from __future__ import print_function
  13. import sys
  14. import os
  15. import warnings
  16. from Pyro4.configuration import config
  17. from Pyro4 import core
  18. from Pyro4.utils import flame
  19. def main(args=None, returnWithoutLooping=False):
  20. from optparse import OptionParser
  21. parser = OptionParser()
  22. parser.add_option("-H", "--host", default="localhost", help="hostname to bind server on (default=%default)")
  23. parser.add_option("-p", "--port", type="int", default=0, help="port to bind server on")
  24. parser.add_option("-u", "--unixsocket", help="Unix domain socket name to bind server on")
  25. parser.add_option("-q", "--quiet", action="store_true", default=False, help="don't output anything")
  26. parser.add_option("-k", "--key", help="the HMAC key to use (deprecated)")
  27. options, args = parser.parse_args(args)
  28. if options.key:
  29. warnings.warn("using -k to supply HMAC key on the command line is a security problem "
  30. "and is deprecated since Pyro 4.72. See the documentation for an alternative.")
  31. if "PYRO_HMAC_KEY" in os.environ:
  32. if options.key:
  33. raise SystemExit("error: don't use -k and PYRO_HMAC_KEY at the same time")
  34. options.key = os.environ["PYRO_HMAC_KEY"]
  35. if not options.quiet:
  36. print("Starting Pyro Flame server.")
  37. hmac = (options.key or "").encode("utf-8")
  38. if not hmac and not options.quiet:
  39. print("Warning: HMAC key not set. Anyone can connect to this server!")
  40. config.SERIALIZERS_ACCEPTED = {"pickle"} # flame requires pickle serializer, doesn't work with the others.
  41. daemon = core.Daemon(host=options.host, port=options.port, unixsocket=options.unixsocket)
  42. if hmac:
  43. daemon._pyroHmacKey = hmac
  44. uri = flame.start(daemon)
  45. if not options.quiet:
  46. print("server uri: %s" % uri)
  47. print("server is running.")
  48. if returnWithoutLooping:
  49. return daemon, uri # for unit testing
  50. else:
  51. daemon.requestLoop()
  52. daemon.close()
  53. return 0
  54. if __name__ == "__main__":
  55. sys.exit(main())