Advertisement
danfalck

apt360_server.py

Apr 13th, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.10 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import daemon
  4. import tempfile
  5. import tornado.web
  6. import tornado.websocket
  7. import tornado.ioloop
  8. import subprocess
  9. from subprocess import call
  10. import os
  11. class MainHandler(tornado.web.RequestHandler):
  12.     def get(self):
  13.         self.render("index.html")
  14.  
  15. class WebSocketHandler(tornado.websocket.WebSocketHandler):
  16.     def allow_draft76(self):
  17.         # older protocol
  18.         # for iOS 5.0 Safari,WebKit
  19.         return True
  20.  
  21.  
  22.     def on_message(self, message):
  23.         #need to create a temporary file that is removed later
  24.         aptfile = tempfile.NamedTemporaryFile(suffix='.apt')
  25.         #logfile =  tempfile.NamedTemporaryFile(suffix='.log')
  26.        
  27.         splitfile = aptfile.name.partition('.')
  28.         print splitfile
  29.         secsplit = splitfile[0]
  30.         thirdsplit= secsplit.split('/')
  31.         tapfile = thirdsplit[-1]+'.tap'
  32.  
  33.         FILE = open(aptfile.name,"w",1)
  34.         FILE.writelines(message)
  35.  
  36.         call(["apt", aptfile.name])
  37.         #capture terminal output
  38.         output = subprocess.Popen(['apt', aptfile.name], stdout=subprocess.PIPE).communicate()[0]
  39.         #LOGFILE = open(logfile.name,"w",1)
  40.         #LOGFILE = open("test.log","w",1)
  41.         #LOGFILE.writelines(output)
  42.         #need to figure out how to get logfile back to html client side
  43.         #delete the temporary apt file
  44.         aptfile.close()
  45.         #logfile.close()
  46.         try:
  47.             f = open(tapfile, 'r',0)
  48.             for line in f.readlines():
  49.                 self.write_message(str(line))
  50.             f.close()
  51.             # remove old *.tap files
  52.             os.system("rm *.tap")
  53.         except IOError as e:
  54.             self.write_message('Error Log from APT360: '+ str(output) )
  55.  
  56.  
  57.  
  58. application = tornado.web.Application([
  59.     (r"/", MainHandler),
  60.     (r"/websocket", WebSocketHandler),
  61. ])
  62.  
  63. if __name__ == "__main__":
  64.     port = 8888
  65.     log = open('tornado.' + str(port) + '.log', 'a+')
  66.     ctx = daemon.DaemonContext( stderr=log, working_directory='.')
  67.     ctx.open()
  68.     application.listen(port)
  69.     tornado.ioloop.IOLoop.instance().start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement