Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. #-*- encoding: utf-8 -*-
  2. # Now works in python 3 aswell as 2
  3.  
  4. import sys, threading, time, os, datetime, time, inspect, subprocess, socket
  5. from subprocess import PIPE, STDOUT
  6. from wsgiref.simple_server import make_server
  7.  
  8. class Webshite():
  9.  
  10. def __init__(self):
  11. self.hostname = socket.getfqdn()
  12. self.header = """
  13. <html>
  14. <header>
  15. <style>
  16. body {
  17. background-color:#6C7A89;
  18. }
  19. p {
  20. color:white;
  21. font-family:Consolas;
  22. }
  23. </style>
  24. </header>
  25. <body>
  26. """
  27. self.shite = "<p>"+str(self.hostname)+"</p>"
  28.  
  29. self.footer = """
  30. </body>
  31. </html>
  32. """
  33.  
  34. def grains(self, environ, start_response):
  35. self.environ = environ
  36. self.start = start_response
  37. status = '200 OK'
  38. response_headers = [('Content-type','text/html; charset=utf-8')]
  39. self.start(status, response_headers)
  40. fullsite = self.header + self.shite + self.footer
  41. fullsite = [fullsite.encode('utf-8')] # in python 3, this needed to be a list, and encoded
  42. return fullsite
  43.  
  44. def run(self):
  45. srv = make_server('127.0.0.1', 8080, self.grains)
  46.  
  47. while True:
  48. try:
  49. threading.Thread(target=srv.handle_request()).start()
  50. except KeyboardInterrupt:
  51. exit()
  52.  
  53.  
  54.  
  55. # ------------------ terrible daemon code for windows -------------------
  56. if __name__ == '__main__':
  57. webshite = Webshite()
  58.  
  59. Windows = sys.platform == 'win32'
  60. ProcessFileName = os.path.realpath(__file__)
  61. pidName = ProcessFileName.split('\\')[-1].replace('.py','')
  62.  
  63. if Windows:
  64. pidFile = 'c:\\Windows\\Temp\\'+pidName+'.pid'
  65. else:
  66. pidFile = '/tmp'+pidName+'.pid'
  67.  
  68.  
  69. def start(pidfile, pidname):
  70. """ Create process file, and save process ID of detached process """
  71. pid = ""
  72. if Windows:
  73. #start child process in detached state
  74. DETACHED_PROCESS = 0x00000008
  75. p = subprocess.Popen([sys.executable, ProcessFileName, "child"],
  76. creationflags=DETACHED_PROCESS)
  77. pid = p.pid
  78.  
  79. else:
  80. p = subprocess.Popen([sys.executable, ProcessFileName, "child"],
  81. stdout = PIPE, stderr = PIPE)
  82. pid = p.pid
  83.  
  84.  
  85. print("Service", pidname, pid, "started")
  86. # create processfile to signify process has started
  87. with open(pidfile, 'w') as f:
  88. f.write(str(pid))
  89. f.close()
  90. os._exit(0)
  91.  
  92.  
  93. def stop(pidfile, pidname):
  94. """ Kill the process and delete the process file """
  95. procID = ""
  96. try:
  97. with open(pidfile, "r") as f:
  98. procID = f.readline()
  99. f.close()
  100. except IOError:
  101. print("process file does not exist, but that's ok <3 I still love you")
  102.  
  103. if procID:
  104. if Windows:
  105. try:
  106. killprocess = subprocess.Popen(['taskkill.exe','/PID',procID,'/F'],
  107. stdout = PIPE, stderr = PIPE)
  108. killoutput = killprocess.communicate()
  109.  
  110. except Exception as e:
  111. print(e)
  112. print ("could not kill ",procID)
  113. else:
  114. print("Service", pidname, procID, "stopped")
  115.  
  116. else:
  117. try:
  118. subprocess.Popen(['kill','-SIGTERM',procID])
  119. except Exception as e:
  120. print(e)
  121. print("could not kill "+procID)
  122. else:
  123. print("Service "+procID + " stopped")
  124.  
  125. #remove the pid file to signify the process has ended
  126. os.remove(pidfile)
  127.  
  128. if len(sys.argv) == 2:
  129.  
  130. if sys.argv[1] == "start":
  131.  
  132. if os.path.isfile(pidFile) == False:
  133. start(pidFile, pidName)
  134. else:
  135. print("process is already started")
  136.  
  137. elif sys.argv[1] == "stop":
  138.  
  139. if os.path.isfile(pidFile) == True:
  140. stop(pidFile, pidName)
  141. else:
  142. print("process is already stopped")
  143.  
  144. elif sys.argv[1] == "restart":
  145. stop(pidFile, pidName)
  146. start(pidFile, pidName)
  147.  
  148. # This is only run on windows in the detached child process
  149. elif sys.argv[1] == "child":
  150. webshite.run()
  151. else:
  152. print("usage: python "+pidName+".py start|stop|restart")
  153.  
  154. #kill main
  155. os._exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement