Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import daemon
- import tempfile
- import tornado.web
- import tornado.websocket
- import tornado.ioloop
- import subprocess
- from subprocess import call
- import os
- class MainHandler(tornado.web.RequestHandler):
- def get(self):
- self.render("index.html")
- class WebSocketHandler(tornado.websocket.WebSocketHandler):
- def allow_draft76(self):
- # older protocol
- # for iOS 5.0 Safari,WebKit
- return True
- def on_message(self, message):
- #need to create a temporary file that is removed later
- aptfile = tempfile.NamedTemporaryFile(suffix='.apt')
- #logfile = tempfile.NamedTemporaryFile(suffix='.log')
- splitfile = aptfile.name.partition('.')
- print splitfile
- secsplit = splitfile[0]
- thirdsplit= secsplit.split('/')
- tapfile = thirdsplit[-1]+'.tap'
- FILE = open(aptfile.name,"w",1)
- FILE.writelines(message)
- call(["apt", aptfile.name])
- #capture terminal output
- output = subprocess.Popen(['apt', aptfile.name], stdout=subprocess.PIPE).communicate()[0]
- #LOGFILE = open(logfile.name,"w",1)
- #LOGFILE = open("test.log","w",1)
- #LOGFILE.writelines(output)
- #need to figure out how to get logfile back to html client side
- #delete the temporary apt file
- aptfile.close()
- #logfile.close()
- try:
- f = open(tapfile, 'r',0)
- for line in f.readlines():
- self.write_message(str(line))
- f.close()
- # remove old *.tap files
- os.system("rm *.tap")
- except IOError as e:
- self.write_message('Error Log from APT360: '+ str(output) )
- application = tornado.web.Application([
- (r"/", MainHandler),
- (r"/websocket", WebSocketHandler),
- ])
- if __name__ == "__main__":
- port = 8888
- log = open('tornado.' + str(port) + '.log', 'a+')
- ctx = daemon.DaemonContext( stderr=log, working_directory='.')
- ctx.open()
- application.listen(port)
- tornado.ioloop.IOLoop.instance().start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement