_twistw.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. """
  15. Print version information and exit.
  16. """
  17. print('twistd (the Twisted Windows runner) {}'.format(copyright.version),
  18. file=self.stdout)
  19. print(copyright.copyright, file=self.stdout)
  20. sys.exit()
  21. class WindowsApplicationRunner(app.ApplicationRunner):
  22. """
  23. An ApplicationRunner which avoids unix-specific things. No
  24. forking, no PID files, no privileges.
  25. """
  26. def preApplication(self):
  27. """
  28. Do pre-application-creation setup.
  29. """
  30. self.oldstdout = sys.stdout
  31. self.oldstderr = sys.stderr
  32. os.chdir(self.config['rundir'])
  33. def postApplication(self):
  34. """
  35. Start the application and run the reactor.
  36. """
  37. service.IService(self.application).privilegedStartService()
  38. app.startApplication(self.application, not self.config['no_save'])
  39. app.startApplication(internet.TimerService(0.1, lambda:None), 0)
  40. self.startReactor(None, self.oldstdout, self.oldstderr)
  41. log.msg("Server Shut Down.")