def do_POST(self): ctype, pdict = cgi.parse_header(self.headers.getheader("content-type")) if ctype == "multipart/form-data": postvars = cgi.parse_multipart(self.rfile, pdict) elif ctype == "application/x-www-form-urlencoded": length = int(self.headers.getheader("content-length")) postvars = cgi.parse_qs(self.rfile.read(length), keep_blank_values=1) else: self.wfile.write("no data posted") return if "action" not in postvars or postvars["action"][0] not in ["register", "unregister"]: self.send_response(500) self.send_header("Content-type", "text/html") self.end_headers() self.wfile.write("ctype=%s length=%s" % (ctype, length)) self.wfile.write("what? %s" % postvars) return db = clientdb.getDb() try: ip = self.client_address[0] mac = GetMacForIp(ip) self.wfile.write("You are ") if postvars["action"][0] == "register": db[mac] = ip else: if mac in db: del db[mac] self.wfile.write("un") self.wfile.write("registered.") except CannotGetIp as e: self.wfile.write("failed to lookup your mac: %s" % e) db.close()
def handleLookup(self, mac, request): """Displays the form to register or unregister a mac address. Called when the mac address lookup subprocess completes. Args: mac: mac address as string request: request object """ request.write("Your MAC address is: %s<p>" % mac) db = None try: db = clientdb.getDb() request.write('<form action="%s" method="post">' % self._form_action) if mac in db: request.write("You are registered.") request.write('<input type="hidden" name="action" value="unregister">') request.write('<input type="submit" value="unregister">') else: request.write("You are not registered.") request.write('<input type="hidden" name="action" value="register">') request.write('<input type="submit" value="register">') request.write("</form>") finally: if db is not None: db.close()
def main(): flags = setupFlags().parse_args() setupLogging() db = clientdb.getDb() presence_mon = PresenceMonitor(flags.airport, db, toggle) while True: presence_mon.check() log.msg('sleeping %d seconds' % flags.sleep) time.sleep(flags.sleep)
def handleLookup(self, mac, request): """Register or unregister a mac address. Called when the mac address lookup subprocess completes. """ postvars = request.args if "action" not in postvars or postvars["action"][0] not in ["register", "unregister"]: request.setResponseCode(500) request.write("missing args") return db = clientdb.getDb() request.write("Your device (%s) is " % mac) if postvars["action"][0] == "register": db[mac] = request.getClientIP() else: if mac in db: del db[mac] request.write("un") request.write("registered.")
def do_GET(self): self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() try: mac = GetMacForIp(self.client_address[0]) self.wfile.write("FYI your MAC address is: %s" % mac) db = clientdb.getDb() self.wfile.write('<form action="/register" method="post">') if mac in db: self.wfile.write("You are registered.") self.wfile.write('<input type="hidden" name="action" value="unregister">') self.wfile.write('<input type="submit" value="unregister">') else: self.wfile.write("You are not registered.") self.wfile.write('<input type="hidden" name="action" value="register">') self.wfile.write('<input type="submit" value="register">') self.wfile.write("</form>") except CannotGetIp as e: self.wfile.write("failed to lookup your mac: %s" % e)
# Setup the state machine and pass it the door controller and xmpp service. door_controller = maestro.DoorControl(maestro.PololuMicroMaestro()) door_open_timeout_secs = int(config.get(APP_NAME, 'door_open_timeout_secs')) alert_timeout_secs = int(config.get(APP_NAME, 'alert_timeout_secs')) sm = statemach.StateMachine(broadcaster, door_controller, doorOpenTimeoutSecs=door_open_timeout_secs, alertTimeoutSecs=alert_timeout_secs) bot_passwd = config.get(APP_NAME, 'bot_passwd') commander = xmpp.ChatCommandReceiverProtocol(sm, subscribers, bot_passwd) commander.setHandlerParent(xmppclient) # Setup a service to poll the airport, and pass it the state machine. airport_hostname = config.get(APP_NAME, 'airport_hostname') airport_polling_secs = int(config.get(APP_NAME, 'airport_polling_secs')) clients = clientdb.getDb() presence_toggle = airport_clientmonitor.StatemachToggle(sm) monitor = airport_clientmonitor.PresenceMonitor( airport_hostname, clients, presence_toggle) presence_service = internet.TimerService(airport_polling_secs, monitor.check) presence_service.setServiceParent(sc) # Setup a service to poll the door sensor, and pass it the state machine. arduino_polling_secs = int(config.get(APP_NAME, 'arduino_polling_secs')) arduino_hostname = config.get(APP_NAME, 'arduino_hostname') threshold_cm = int(config.get(APP_NAME, 'arduino_threshold_cm')) toggle = arduino_client.StatemachToggle(sm) sensor = arduino_client.DoorSensor(toggle, hostname=arduino_hostname, threshold=threshold_cm) sensor_service = internet.TimerService(arduino_polling_secs, sensor.check) sensor_service.setServiceParent(sc)
else: print "\n".join(d.keys()) def Add(d, key): # TODO: specify a value d[key] = 1 print "added %s" % key def Del(d, key): del d[key] print "deleted %s" % key if __name__ == "__main__": COMMANDS = {"add": Add, "list": List, "del": Del} parser = argparse.ArgumentParser(description="Manipulate a client db") parser.add_argument("--command", help="list, add, or del", required=True) parser.add_argument("--key", help="key to list, add, or del") # TODO: this flag should come from clientdb parser.add_argument("--db", help="db path", default=clientdb.DEFAULT_DB_PATH) flags = parser.parse_args() if flags.command not in COMMANDS: logging.log(logging.FATAL, "unknown command: %s", flags.command) d = clientdb.getDb(flags.db) COMMANDS[flags.command](d, flags.key)