_twistw.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # -*- test-case-name: twisted.test.test_twistd -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. from __future__ import print_function
  5. from twisted.python import log
  6. from twisted.application import app, service, internet
  7. from twisted import copyright
  8. import sys, os
  9. class ServerOptions(app.ServerOptions):
  10. synopsis = "Usage: twistd [options]"
  11. optFlags = [['nodaemon','n', "(for backwards compatibility)."],
  12. ]
  13. def opt_version(self):
  14. """Print version information and exit.
  15. """
  16. print('twistd (the Twisted Windows runner) %s' % copyright.version)
  17. print(copyright.copyright)
  18. sys.exit()
  19. class WindowsApplicationRunner(app.ApplicationRunner):
  20. """
  21. An ApplicationRunner which avoids unix-specific things. No
  22. forking, no PID files, no privileges.
  23. """
  24. def preApplication(self):
  25. """
  26. Do pre-application-creation setup.
  27. """
  28. self.oldstdout = sys.stdout
  29. self.oldstderr = sys.stderr
  30. os.chdir(self.config['rundir'])
  31. def postApplication(self):
  32. """
  33. Start the application and run the reactor.
  34. """
  35. service.IService(self.application).privilegedStartService()
  36. app.startApplication(self.application, not self.config['no_save'])
  37. app.startApplication(internet.TimerService(0.1, lambda:None), 0)
  38. self.startReactor(None, self.oldstdout, self.oldstderr)
  39. log.msg("Server Shut Down.")