-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathcommand_shell.py
55 lines (47 loc) · 1.87 KB
/
command_shell.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib
from threading import Thread
import base64
import time
import urllib2
import logging
from includes.run_prog import run_program
class CommandShell(Thread):
def __init__(self, server, drone, key):
self.server = server
self.drone = drone
self.key = key
self.RUN = True
Thread.__init__(self)
def run(self):
"""Check every N seconds if a new command is required to be run"""
while self.RUN:
self.fetch_command()
time.sleep(5)
#TODO: 1. Use Posts, and probably JSON for commands. Currently using GETs.
# 2. Launch programs in a separate thread, to avoid blocking
def fetch_command(self):
"""Poll the server for new commands, execute, and return"""
base64string = base64.encodestring('%s:%s' % (self.drone, self.key)).replace('\n', '')
headers = {'content-type': 'application/json',
'Authorization':'Basic %s' % base64string}
checkForCommandURL = self.server + "/cmd/droneQuery"
sendCommandOutputURL = self.server + "/cmd/droneResponse"
try:
req = urllib2.Request(checkForCommandURL, headers=headers)
response = urllib2.urlopen(req)
if response:
command = response.read()
if command != "":
logging.debug("Running command '%s'" % command)
outcome = run_program(command)
response_data = urllib.urlencode({'command':command, 'output': outcome } )
req = urllib2.Request(sendCommandOutputURL + "?" + response_data, headers=headers)
response = urllib2.urlopen(req)
except Exception,e:
logging.error(e)
def stop(self):
self.RUN = False
if __name__ == "__main__":
CommandShell().start()