def __init__(self, bus, frequency=600, server=None): self.lock = Lock() self.server = server self.bus = bus Monitor.__init__(self, bus, lambda: self.run(server), frequency=frequency) self.subscribe() self.nruns = 0
def __init__(self, bus, name, compiler): Monitor.__init__(self, bus, self.run, frequency=.5) self.name = name self.compiler = compiler self._files = {} self.enable = False
def init_monitors(self): try: self.add_log_entry('CHERRYPY', 'Initializing Monitors') Monitor(cherrypy.engine, self.new_sample, frequency=self.SAMPLE_INTERVAL).subscribe() Monitor(cherrypy.engine, self.update_graphs, frequency=self.UPDATE_INTERVAL).subscribe() except Exception as error: self.add_log_entry('CHERRYPY', str(error))
def start(self): Monitor.start(self) self.enable = cherrypy.config['opmuse'].get('%s.enable' % self.name) if self.enable is None: self.enable = True if not self.enable: return self.compiler.compile()
def __init_tasks__(self): if self.VERBOSE: self.pretty_print('CHERRYPY', 'Initializing Monitors ...') try: Monitor(cherrypy.engine, self.listen, frequency=self.CHERRYPY_LISTEN_INTERVAL).subscribe() Monitor(cherrypy.engine, self.refresh, frequency=self.CHERRYPY_REFRESH_INTERVAL).subscribe() except Exception as error: self.pretty_print('CHERRYPY', str(error))
def stop(self): Monitor.stop(self) if self._stopped: return self._stopped = True for name, write_handler in write_handlers.items(): write_handlers[name] = None write_handler.stopped = True write_handler.commit()
def main(): global webConfigServer global periodicTask try: webConfigServer = ConfigServer() periodicTask = Monitor(cherrypy.engine, updateHvac, frequency=1) periodicTask.start() webConfigServer.start() return 0 except: # Dump callstack to log and exit with -1 log.exception('Unexpected exception occured.') return -1
def init_tasks(self): pretty_print('CHERRYPY', 'Initializing Monitors') try: Monitor(cherrypy.engine, self.listen, frequency=self.CHERRYPY_LISTEN_INTERVAL).subscribe() Monitor(cherrypy.engine, self.backup, frequency=self.CHERRYPY_BACKUP_INTERVAL).subscribe() Monitor(cherrypy.engine, self.check, frequency=self.CHERRYPY_CHECK_INTERVAL).subscribe() except Exception as error: pretty_print('ERROR', str(error))
def __init__(self): # set general and visitor databases self.database = Database() self.visitors = DatabaseVisits() self.own_ip = OwnIP() # save database contents when CherryPy stops cherrypy.engine.subscribe('stop', self.database.save_data) cherrypy.engine.subscribe('stop', self.visitors.save_data) # update and save databases and own IP periodically Monitor(cherrypy.engine, self.database.update_data, frequency=DBUPDATE_FREQ).subscribe() Monitor(cherrypy.engine, self.visitors.save_data, frequency=DBUPDATE_FREQ + 1).subscribe() Monitor(cherrypy.engine, self.own_ip.update_ip, frequency=IPUPDATE_FREQ).subscribe() # favicon with open(os.path.join("content", "favicon.ico"), "rb") as file: self.favicon = file.read() # 'static' files self.webpages = { INDEX: { "type": "text/html" }, ERROR: { "type": "text/html" }, "robots.txt": { "type": "text/plain" }, "stylesheet.css": { "type": "text/css" }, "content.js": { "type": "text/javascript" }, } for i in self.webpages: with open(os.path.join("content", i), "rb") as file: data = file.read() if not DEBUG: # optimize by removing some unneeded whitespace # this will break with single line comments though data = data.replace(b'\t', b'') data = data.replace(b'\r', b'') data = data.replace(b'\n', b'') self.webpages[i]["data"] = data
def __init__(self, config=None, log='errors.txt'): try: self.config = config self.workspace = os.path.dirname(os.path.abspath(__file__)) self.logs_directory = os.path.join(self.workspace, self.config['cherrypy_path'], 'logs') self.logfile = os.path.join(self.logs_directory, log) with open(self.logfile, 'w') as log: pass self.log_msg('CANBUS', 'NOTE: Initializing Log-file ...') self.poll_bad_counter = 0 self.poll_ok_counter = 0 self.poll_nodata_counter = 0 self.active_nodes = [] except Exception as error: self.log_msg('ENGINE', 'ERROR: %s' % str(error)) try: self.log_msg('CANBUS', 'NOTE: Initializing CANBUS ...') self.gateway = CAN.Gateway(baud=self.config['gateway_baud'], device=self.config['gateway_device'], use_checksum=self.config['gateway_use_checksum']) except Exception as error: self.gateway = None self.log_msg('CANBUS', 'ERROR: %s' % str(error)) if self.gateway is None: self.log_msg('CANBUS', 'ERROR: No CAN Gateway found! Check the connection in the Manager!') try: self.log_msg('DB ', 'NOTE: Initializing Database ...') self.database = DB.NodeDB(port=self.config['db_port'], address=self.config['db_address'], name=self.config['db_name']) except Exception as error: self.database = None self.log_msg('DB ', 'ERROR: %s' % str(error)) if self.database is None: self.log_msg('DB ', 'Error: Database failed to attached!') # Initialize Webapp self.log_msg('HTTP ', 'NOTE: Initializing Run-Time Tasks ...') try: self.log_msg('CANBUS', 'NOTE: Polling frequency set to: %d Hz' % float(self.config['poll_freq_hz'])) self.poll_task = Monitor(cherrypy.engine, self.poll, frequency=1/float(self.config['poll_freq_hz'])).subscribe() self.log_msg('CANBUS', 'NOTE: Auto-clean interval set to: %2.1f hours' % float(self.config['clean_freq_hours'])) self.clean_task = Monitor(cherrypy.engine, self.clean, frequency=float(3600 * self.config['clean_freq_hours'])).subscribe() self.log_msg('CANBUS', 'NOTE: Auto-clean cut-off period set to: %d days' % self.config['db_cutoff_days']) """ TODO: Additional scheduled tasks for network maintenance? """ except Exception as error: self.log_msg('HTTP ', 'ERROR: %s' % str(error))
def main(): # do some preliminary stuff GameZero.MY_FULLNAME = os.path.normpath(os.path.abspath(__file__)) GameZero.MY_NAME = os.path.split(os.path.dirname(GameZero.MY_FULLNAME))[1] GameZero.PROG_DIR = os.path.dirname(GameZero.MY_FULLNAME) GameZero.DATABASE_PATH = os.path.join(GameZero.PROG_DIR, GameZero.MY_NAME, "DATABASE.DB") # Make sure we can write to the data dir if not os.access(GameZero.PROG_DIR, os.W_OK): sys.exit("Data directory: " + GameZero.PROG_DIR + " must be writable (write permissions). Exiting.") #CANT FIND DATABASE - RUN INSTALL if not os.path.exists(GameZero.DATABASE_PATH): install.run(GameZero.DATABASE_PATH)#run the install #Load the configs db.LogMessage(GameZero.DATABASE_PATH, "Loading Configurations to memory", "Info") LoadConfigs(db.config(GameZero.DATABASE_PATH)) #get cherrypy ready db.LogMessage(GameZero.DATABASE_PATH, "Registering Background Processes", "Info") GameZero.THEMEPATH = os.path.join(GameZero.PROG_DIR, GameZero.MY_NAME,"resources","interface",GameZero.THEME) cherrypy.config.update({'server.socket_port': int(GameZero.SERVERPORT), 'server.socket_host': GameZero.HOST, 'response.timeout':900}) appConfig = {'/static': {'tools.staticdir.on': True, 'tools.staticdir.dir': GameZero.THEMEPATH}} cherrypy.tree.mount(site.site(), '/', appConfig) Monitor(cherrypy.engine, tasks.Task().CheckUpdates, frequency=86400).subscribe() Monitor(cherrypy.engine, tasks.Task().PostProcess, frequency=86400).subscribe() #Monitor(cherrypy.engine, Task.Task().PostProcess, frequency=86400).subscribe() db.LogMessage(GameZero.DATABASE_PATH, "Starting Service", "Info") cherrypy.engine.start() if(GameZero.BROWSER == '1'): webbrowser.open("http://" + cherrypy.server.socket_host + ":" + str(cherrypy.server.socket_port) + '/') cherrypy.engine.block()
def init_webapp(self): self.log_msg('HTTP', 'Initializing Webapp Tasks') try: self.run_task = Monitor( cherrypy.engine, self.run, frequency=1 / float(self.config['CAMERA_FPS'])).subscribe() if self.config['GPS_ENABLED']: self.gps_task = Monitor( cherrypy.engine, self.update_gps, frequency=1 / float(self.config['GPS_HZ'])).subscribe() self.cameras_task = Monitor(cherrypy.engine, self.check_cameras, frequency=5).subscribe() self.controller_task = Monitor(cherrypy.engine, self.check_controller, frequency=5).subscribe() self.save_task = Monitor(cherrypy.engine, self.save_image, frequency=1).subscribe() except Exception as error: self.log_msg('ENGINE', 'Error: %s' % str(error), important=True)
def startWebServer(): global manager if database.verifyDatabaseExistence() and database.tableIntegrityCheck(): try: manager = threading.Thread(target=queueManager) # Don't wait for thread to close on cherrypy exit stop method will # set the serverShuttingDown global to True # then the thread will exit manager.daemon = True manager.start() # Register cherrypy monitor - runs every 30 seconds # The monitor will check to make sure the download manager # Is still active. If it is not and a shutdown has been requested # It will terminate the daemon, else it will start a manager. EventScheduler = Monitor(cherrypy.engine, checkManager, 30, 'EventScheduler') EventScheduler.start() cherrypy.quickstart(webServer(), config=conf) except Exception, err: for error in err: log("Unable to start Web Server - " + str(error))
def Start(self,configPath): rootPath = os.path.dirname(configPath) GamezServer.Service.DBPATH = os.path.join(rootPath, "GamezServer.db") GamezServer.Service.APPPATH = rootPath dao = DAO() dao.VerifyStructue() staticContentPath = os.path.join(rootPath, os.path.join("GamezServer","static-web")) appConfig = {'/static': {'tools.staticdir.on': True, 'tools.staticdir.dir': staticContentPath}} cherrypy.config.update(configPath) cherrypy.tree.mount(WebServe.WebServe(), '/', appConfig) dao.LogMessage("Registering Background Processes", "Info") Monitor(cherrypy.engine, GamezServer.Task.Task().UpdateMasterPlatforms, frequency=86400).subscribe() Monitor(cherrypy.engine, GamezServer.Task.Task().UpdateMasterGames, frequency=86400).subscribe() Monitor(cherrypy.engine, GamezServer.Task.Task().WantedGameSearch, frequency=86400).subscribe() dao.LogMessage("Starting Service", "Info") cherrypy.engine.start() launchBrowser = dao.GetSiteMasterData("launchBrowser") if(launchBrowser == "true"): webbrowser.open("http://" + cherrypy.server.socket_host + ":" + str(cherrypy.server.socket_port) + '/') cherrypy.engine.block()
def initialise(config_file_path=None): global cfg cfg = load_config(config_file_path) global stat_logger stat_logger = setup_logging() global push_stats push_stats = create_output_fn() global stats_package_template stats_package_template = {'metadata': cfg['metadata'], 'type': 'default_type', 'stats': []} if cfg['functions']: from function_profiler import decorate_functions # call this now and later, that way if imports overwrite our wraps # then we re-wrap them again at engine start. decorate_functions() cherrypy.engine.subscribe('start', decorate_functions, 0) if cfg['handlers']: from handler_profiler import decorate_handlers # no point wrapping these now as they won't be active before # engine start. cherrypy.engine.subscribe('start', decorate_handlers, 0) if cfg['sql']['sql_enabled']: from sql_profiler import decorate_connections # call this now and later, that way if imports overwrite our wraps # then we re-wrap them again at engine start. decorate_connections() cherrypy.engine.subscribe('start', decorate_connections, 0) if cfg['files']['files_enabled']: from file_profiler import decorate_open # this is very unlikely to be overwritten, call asap. decorate_open() from stats_flushers import flush_stats # create a monitor to periodically flush the stats buffers at the flush_interval Monitor(cherrypy.engine, flush_stats, frequency=int(cfg['output']['flush_interval']), name='Flush stats buffers').subscribe()
def __init__(self, metadata_file, metadata_update_frequency, signing_keys=None): self.update_frequency = metadata_update_frequency if signing_keys is None: signing_keys = {} self.signing_keys = signing_keys.values() self.signing_algs_supported = signing_keys.keys() self.signing_algs_supported.append( "none") # Force support for Unsecured JWS self.validator = RequestValidator(MIME_TYPES_SUPPORTED, self.signing_algs_supported) self.metadata_store = MetadataStore() md_update = MetadataUpdate(metadata_file, self.metadata_store) # Force populate the metadata store with initial data md_update() # Start updates in the background Monitor(cherrypy.engine, md_update, frequency=metadata_update_frequency).subscribe()
def report(self, username, password, location='1', ip='202.36.244.10', port=listen_port): # TODO: change ip = back to listen_ip hashedPassword = encrypt.hash( password) # call hash function for SHA256 encryption auth = self.authoriseUserLogin(username, hashedPassword, location, ip, port) error_code, error_message = auth.split(",") if (error_code == '0'): # successful login, populate session variables self.logged_on = 1 cherrypy.session['username'] = username cherrypy.session['password'] = hashedPassword cherrypy.session['location'] = location cherrypy.session['ip'] = ip cherrypy.session['port'] = port t = Monitor( cherrypy.engine, MainApp().reportThreaded, frequency=30).start() # start threading function, 30 seconds c = sqlite3.connect(DB_STRING) cur = c.cursor() cur.execute( "SELECT username FROM user_credentials WHERE username=?", [username]) credentials = cur.fetchone() if not credentials: # couldn't find, thus user has never logged on before and needs 2FA QR #store username and password in user_credentials table; logoffForced (on application exit/crash), threaded /report and 2FA uses this raise cherrypy.HTTPRedirect('/twoFA') else: # a returning user is back, so go to /twoFAcode to get them just to enter their TOTP again raise cherrypy.HTTPRedirect('/twoFAcode') else: print("ERROR: " + error_code) self.logged_on = 2 raise cherrypy.HTTPRedirect( '/') # set flag to change /index function
def __init__(self, config_file): try: with open(config_file) as cfg: self.config = json.loads(cfg.read()) self.session_key = binascii.b2a_hex(os.urandom(self.config['SESSION_KEY_LENGTH'])) self.latest_data = {} # Mongo try: self.mongo_client = MongoClient() self.db = self.mongo_client[self.config['MONGO_NAME']] self.session = self.db[self.session_key] except: self.print_error('MONGO', e) # OBD try: self.gateway = Gateway() self.gateway.attach() self.gateway.get_device() except Exception as e: self.print_error('Gateway failed', e) # Logger try: self.log = Logger(self.session_key) except Exception as e: self.print_error('LOGGER', e) # Scheduled Tasks try: Monitor(cherrypy.engine, self.listen, frequency=self.config["POLLING_FREQ"]).subscribe() except Exception as e: raise e except Exception as e: self.print_error('SYSTEM', e)
DB_STRING = "users.db" reload(sys) sys.setdefaultencoding('utf8') listen_ip = '172.23.68.189' # socket.gethostbyname(socket.getfqdn()) listen_port = 10002 api_calls = 0 def resetTimer(): # resets the api_calls counter every minute print("API Calls timer reset") global api_calls api_calls = 0 t = Monitor(cherrypy.engine, resetTimer, frequency=60).start() # start threading function, 30 seconds def LimitReached( ): # this function will return True if limit reached/user has been blacklisted # firstly check if IP address has been blacklisted ip = cherrypy.request.headers[ "Remote-Addr"] # this info from header has caller IP information c = sqlite3.connect(DB_STRING) cur = c.cursor() cur.execute("SELECT ip FROM blacklist") blacklist = cur.fetchone( ) # blackListing/rate-limiting has similar use cases, might as well if blacklist: # if users were found on the blacklist for i in range(0, len(blacklist)): # search through the list if ip == blacklist[
import os.path from timetable import Table class WebInterface(object): @cherrypy.expose def index(self): return file("index.html") @cherrypy.expose def zones(self): return table.zone_states() if __name__ == "__main__": table = Table() conf = { '/': { 'tools.sessions.on': True, 'tools.staticdir.root': './' }, '/static': { 'tools.staticdir.on': True, 'tools.staticdir.dir': os.getcwd() + '/public' } } cherrypy.server.socket_host = '0.0.0.0' Monitor(cherrypy.engine, table.current, frequency=60).subscribe() cherrypy.quickstart(WebInterface(), '/', conf)
def __init__(self, bus): Monitor.__init__(self, bus, self.run, frequency=30) self._running = False self._stopped = False
def __init__(self, bus): Monitor.__init__(self, bus, self.deamon_run, frequency=60, name=self.__class__.__name__)
def timeremaining(self): self.curr_time = time.time() self.diff = self.curr_time - Clockwork.last_served self.delta = Clockwork.idle_timeout - self.diff td = datetime.timedelta(seconds=self.delta) seconds = td.seconds minutes = (seconds % 3600) // 60 secs = (seconds % 60) timeserved = time.localtime(Clockwork.last_served) return """ Last served: {}<P> Time until shutdown: {} min(s) {} seconds """.format(time.strftime("%X", timeserved), minutes, secs) Monitor(cherrypy.engine, Clockwork.ticktock, frequency=1).subscribe() def timeout_reset(): Clockwork.last_served = time.time() def report(): return str(Clockwork.diff) class StringGenerator(object): @cherrypy.expose def index(self): return "Hello world!"
def start_deamon(p): Monitor(cherrypy.engine, p.deamon_run, frequency=p.deamon_frequency, name=p.__class__.__name__).subscribe()
class MainApp(object): msg = " " chat_error = "" chat = "" conversation = "" profile_html = "" global db db = connectDatabse(db_file) global cursor cursor = db.cursor() # Make user list db createTable(db, """CREATE TABLE IF NOT EXISTS user_list ( id INTEGER PRIMARY KEY, username TEXT, location INTEGER, ip TEXT, port INTEGER, login_time TEXT, status TEXT);""") # Make messages db createTable(db, """CREATE TABLE IF NOT EXISTS messages ( id INTEGER PRIMARY KEY, sender TEXT, recipient TEXT, message TEXT, stamp INTEGER, mime TEXT);""") # Make profiles db createTable(db, """CREATE TABLE IF NOT EXISTS profiles ( id INTEGER PRIMARY KEY, username TEXT, fullname TEXT, position TEXT, description TEXT, location TEXT, picture TEXT);""") # Init chat panels initUsers(db) people = initPeople(db) conv = initChat(db) @cherrypy.expose def index(self): # Landing page; entry point of application (login page) page = open('main.html', 'r').read().format(message=self.msg) return page @cherrypy.expose # Index page def home(self): try: self.getList() self.people = initPeople(db) page = open('loggedin.html', 'r').read().format(username=cherrypy.session['username'], chat_error=self.chat_error, chat_messages=self.chat, conversation=self.conversation, people=self.people, chat=self.conv, profile_html=self.profile_html) except KeyError: self.msg = "Session expired, please login again" raise cherrypy.HTTPRedirect('/') return page @cherrypy.expose def signin(self, username=None, password=None): """Sign-in function. Called when user submits login form Reports user to login server. Starts thread for continual reporting to login server """ hash_pw = hashlib.sha256(str(password+salt)).hexdigest() # hash password error = self.report(username, hash_pw) # report to login server print error if (int(error) == 0): # if successfully reported, start session and report thread global upi global pw upi = username pw = hash_pw cherrypy.session['username'] = username cherrypy.session['password'] = hash_pw self.report_thread.start() raise cherrypy.HTTPRedirect('/home') else: print "login failed!2" self.msg = "Incorrect credentials, please try again" # prompt user to log in again raise cherrypy.HTTPRedirect('/') # redirect to login page @cherrypy.expose def report(self, username, hash_pw): # Report to login server try: url = 'http://cs302.pythonanywhere.com/report?username='******'&password='******'&location=' + '0' + '&ip=' + ext_ip url += '&port=' + str(port) + '&enc=0' print "logged in as " + username except: self.msg = 'Login failed!' print "login failed!" raise cherrypy.HTTPRedirect('/') # Getting the error code from the server response_message = (urllib2.urlopen(url)).read() response = str(response_message)[0] # Display response message from the server print "Server response: " + str(response_message) return response @cherrypy.expose # Thread to report to report to login server continually def reportThread(): print 'reporting' try: url = 'http://cs302.pythonanywhere.com/report?username='******'&password='******'&location=' + '0' + '&ip=' + ext_ip url += '&port=' + str(port) + '&enc=0' print url except: print 'could not report!' response_message = (urllib2.urlopen(url)).read() response = str(response_message)[0] # Display response message from the server print "Server response: " + str(response_message) return # Thread to report to login server regularly # Will report once every 60 seconds report_thread = Monitor(cherrypy.engine, reportThread, frequency=60) @cherrypy.expose def signout(self): # Log user out from login server url = 'http://cs302.pythonanywhere.com/logoff?username='******'username']) + '&password='******'password']) + '&enc=0' self.report_thread.stop() # stop regularly reporting to login server response = (urllib2.urlopen(url)).read() error = str(response)[0] if (int(error) == 0): self.msg = 'Logout successful!' cherrypy.session.clear() # clear user session raise cherrypy.HTTPRedirect('/') def getList(self): # Get list of users currently online try: url = 'http://cs302.pythonanywhere.com/getList?username='******'username']) + '&password='******'password']) + '&enc=0' except: print 'getList failed!' raise cherrypy.HTTPRedirect('/') response = str((urllib2.urlopen(url)).read()) error = int(response[0]) if (error == 0): usernames = [] # Format user list user_details = response.replace("0, Online user list returned", "") user_details = user_details.split() for i in range (len(user_details)): if (',' in user_details[i]): split_details = user_details[i].split(',') if (split_details[0] != cherrypy.session['username']): usernames.append(split_details[0]) insertUser(split_details, db, cursor) initProfile(split_details, db, cursor) return ", ".join(usernames) @cherrypy.expose def ping(self, sender=None): """Implements ping API. Always returns 0 to indicate server is online """ return '0' @cherrypy.expose def listAPI(self): """Implements listAPI API Returns APIs supported by this client """ return '/ping [sender] /listAPI /receiveMessage [sender] [destination] [message] [stamp] /receiveFile [sender] [destination] [file] [filename] [content_type] [stamp] /getProfile [profile_username] [sender] /receiveFile [sender] [destination] [file] [filename] [content_type] [stamp]' @cherrypy.expose @cherrypy.tools.json_in() def receiveMessage(self): """Implements receiveMessage API """ data = cherrypy.request.json # Retrieve json input # Put into db cursor.execute('''INSERT INTO messages (sender, recipient, message, stamp) VALUES (?, ?, ?, ?)''', (data['sender'], data['destination'], data['message'], data['stamp'])) db.commit() self.chat_error = 'Someone sent you a message!: ' + data['message'] print self.chat_error return '0' @cherrypy.expose def sendMessage(self, recipient, message): """Sends a message Calls recipient's receiveMessage """ current_time = time.time() # get timestamp curs = db.execute("""SELECT id, username, location, ip, port, login_time, status from user_list""") for row in curs: if (recipient == row[1]): recipient_ip = row[3] recipient_port = row[4] if (row[6] != 'Online'): # If user is not online, message cannot be sent return 'Your message could not be delivered!' post_data = {"sender": cherrypy.session['username'], "destination": recipient, "message": message, "stamp": int(current_time)} post_data = json.dumps(post_data) # json encode url = 'http://' + str(recipient_ip) + ":" + str(recipient_port) + '/receiveMessage?' try: req = urllib2.Request(url, post_data, {'Content-Type': 'application/json'}) response = urllib2.urlopen(req).read() except: return 'Your message could not be delivered!' print response if (str(response[0]) == '0'): # check if message was successfully sent self.chat = 'Message sent!' # only insert into database if successfully sent cursor.execute('''INSERT INTO messages (sender, recipient, message, stamp) VALUES (?, ?, ?, ?)''', (cherrypy.session['username'], recipient, message, current_time)) db.commit() return 'Your message has been sent!' # display on page successful message receipt else: error = 'Your message could not be delivered!' print error self.chat = error return error break cherrypy.HTTPRedirect('/home') @cherrypy.expose def updateConversation(self, username): """Formats the chat panel (chat bubbles) Responsible for in-line media content display i.e. plaintext, image, video, audio Periodically called via jQuery Returns HTML of the current conversation Dynamically updates conversation """ conversation = "" # query database for messages curs = db.execute("""SELECT id, sender, recipient, message, stamp, mime from messages""") # format messages for row in curs: # only insert messages which were sent or received by this user if ((cherrypy.session['username'] == row[1]) or (cherrypy.session['username'] == row[2])): if (username == row[1]): # recipient conversation += '<div class="bubble you">' if row[5] is None: # plaintext message conversation += row[3] + '</div>' elif 'image' in row[5]: conversation += '<img src="data:' + row[5] + ';base64,' conversation += row[3] + '"/></div>' elif 'audio' in row[5]: conversation += '<audio controls src="data:' + row[5] + ';base64,' conversation += row[3] + '"/></div>' elif 'video' in row[5]: conversation += '<video controls><source type="video/webm" src="data:video/webm;base64,' conversation += row[3] + '">' conversation += '<source type=' + row[5] + 'src="' + row[5] conversation += ';base64,' + row[3] + '"></video></div>' elif 'application' in row[5]: conversation += 'Sent you a file!</div>' elif (username == row[2]): # sender conversation += '<div class="bubble me">' if row[5] is None: conversation += row[3] + '</div>' elif 'image' in row[5]: conversation += '<img src="data:' + row[5] + ';base64, ' conversation += row[3] + '"/></div>' elif 'audio' in row[5]: conversation += '<audio controls src="data:' + row[5] + ';base64,' conversation += row[3] + '"/></div>' elif 'video' in row[5]: conversation += '<video controls><source type="video/webm" src="data:video/webm;base64,' conversation += row[3] + '">' conversation += '<source type=' + row[5] + 'src="' + row[5] conversation += ';base64,' + row[3] + '"></video></div>' elif 'application' in row[5]: conversation += 'Sent you a file!</div>' return conversation @cherrypy.expose @cherrypy.tools.json_in() def receiveFile(self): """Implements receiveFile API Writes file to local disk and stores in database """ print 'Someone sent you a file! ' data = cherrypy.request.json sender = data['sender'] recipient = data['destination'] file = data['file'] filename = data['filename'] mime = data['content_type'] print filename stamp = data['stamp'] # Write to local disk with open(filename, "wb") as fh: fh.write(file.decode('base64')) # Insert in database try: cursor.execute('''INSERT INTO messages (sender, recipient, message, stamp, mime) VALUES (?, ?, ?, ?, ?)''', (sender, recipient, file, stamp, mime)) db.commit() except: print 'failed to put file in db!' return '0' @cherrypy.expose @cherrypy.tools.json_out() def sendFile(self, send_file, recipient): """Sends files Calls recipient's receiveFile API """ stamp = int(time.time()) enc_file = base64.b64encode(send_file.file.read()) # encode as base64 string post_data = {"sender": cherrypy.session['username'], "destination": recipient, "file": enc_file, "stamp": stamp, "filename": send_file.filename, "content_type": str(send_file.content_type)} post_data = json.dumps(post_data) # send request curs = db.execute("""SELECT id, username, location, ip, port, login_time from user_list""") for row in curs: if (recipient == row[1]): recipient_ip = row[3] recipient_port = row[4] url = 'http://' + str(recipient_ip) + ":" + str(recipient_port) + '/receiveFile?' print url req = urllib2.Request(url, post_data, {'Content-Type': 'application/json'}) response = urllib2.urlopen(req).read() print response break if (str(response[0]) == '0'): # check if file was successfully sent # put file in db cursor.execute('''INSERT INTO messages (sender, recipient, message, stamp, mime) VALUES (?, ?, ?, ?, ?)''', (cherrypy.session['username'], recipient, enc_file, stamp, str(send_file.content_type))) db.commit() raise cherrypy.HTTPRedirect('/home') @cherrypy.expose @cherrypy.tools.json_in() @cherrypy.tools.json_out() def getProfile(self, user=None): """Implements getProfile API """ if user is None: # viewing own profile data = cherrypy.request.json username = data['profile_username'] else: username = user # In order to output as dict, need to utilise row_factory db_row = sqlite3.connect(db_file, check_same_thread=False) db_row.row_factory = sqlite3.Row c = db_row.cursor() c.execute('''SELECT * FROM profiles WHERE username=?''', (username,)) profile_data = c.fetchone() return dict(profile_data) # format as dict @cherrypy.expose @cherrypy.tools.json_out() def retrieveProfile(self, user=None): """Get other users' profiles Calls recipient's getProfile """ try: cursor.execute('''SELECT * FROM user_list WHERE username=?''', (user,)) row = cursor.fetchone() ip = row[3] port = row[4] url = 'http://' + str(ip) + ':' +str(port) + '/' post_data = {"profile_username": user, "sender": cherrypy.session['username']} post_data = json.dumps(post_data) getProfile_url = url + 'getProfile?' req = urllib2.Request(getProfile_url, post_data, {'Content-Type': 'application/json'}) response = urllib2.urlopen(req).read() data = json.loads(response) # json encode request try: # update db cursor.execute('''SELECT * FROM profiles WHERE username=?''', (user,)) cursor.execute('''UPDATE profiles SET fullname=?, position=?, description=?, location=?, picture=? WHERE username=?''', (data['fullname'], data['position'], data['description'], data['location'], data['picture'], user)) except: print 'user does not exist in db!' except: print 'user does not exist!' db.commit() @cherrypy.expose def viewProfile(self, user=None): """View someone's profile """ try: if user is None: # view own profile username = cherrypy.session['username'] else: username = user try: cursor.execute('''SELECT * FROM user_list WHERE username=?''', (user,)) row = cursor.fetchone() if (row[6] != 'Offline'): # Try to call their getProfile if they are not offline self.retrieveProfile(user=username) except: print 'could not retrieve profile!' # retrieve profile from own db cursor.execute('''SELECT * FROM profiles WHERE username=?''', (username,)) row = cursor.fetchone() # Format profile profile_html = '<img src="' + row[6] + '">' + '<br><br>' profile_html += 'Username: '******'<br>' profile_html += 'Full name: ' + row[2] + '<br>' profile_html += 'Position: ' + row[3] + '<br>' profile_html += 'Description: ' + row[4] + '<br>' profile_html += 'Location: ' + row[5] + '<br>' return profile_html except: self.msg = 'Session expired, please login again' raise cherrypy.HTTPRedirect('/') @cherrypy.expose def myProfile(self, user=None): """Page showing my (current logged in user) profile """ try: if user is None: username = cherrypy.session['username'] else: username = user cursor.execute('''SELECT * FROM profiles WHERE username=?''', (username,)) row = cursor.fetchone() print row profile_data = '<img src="' + row[6] + '">' + '<br><br>' profile_data += 'Username: '******'<br>' profile_data += 'Full name: ' + row[2] + '<br>' profile_data += 'Position: ' + row[3] + '<br>' profile_data += 'Description: ' + row[4] + '<br>' profile_data += 'Location: ' + row[5] + '<br>' page = open('profile.html', 'r').read().format(profile_data=str(profile_data)) return page except: self.msg = 'Session expired, please login again' raise cherrypy.HTTPRedirect('/') @cherrypy.expose def editProfile(self, parameter, changes, user=None): """Make changes to my (current logged in user) profile """ if user is None: username = cherrypy.session['username'] else: username = user if (parameter == 'fullname'): cursor.execute('''UPDATE profiles SET fullname=? WHERE username=?''', (changes, username,)) elif (parameter == 'position'): cursor.execute('''UPDATE profiles SET position=? WHERE username=?''', (changes, username,)) elif (parameter == 'desc'): cursor.execute('''UPDATE profiles SET description=? WHERE username=?''', (changes, username,)) elif (parameter == 'location'): cursor.execute('''UPDATE profiles SET location=? WHERE username=?''', (changes, username,)) elif (parameter == 'picture'): cursor.execute('''UPDATE profiles SET location=? WHERE username=?''', (changes, username,)) else: print "invalid parameter!" db.commit() raise cherrypy.HTTPRedirect('/myProfile') webbrowser.open_new('http://%s:%d/' % (ext_ip, port))
def update_prices(): print SCRIPS print len(SCRIPS) scrips_data = get_stock.get_stock_price(SCRIPS) print 'Starting thread at %s' % (datetime.datetime.now()) for scrip, data in scrips_data.iteritems(): REDIS_OBJ.set('%s:ltp' % (scrip), data['price']) REDIS_OBJ.set('%s:chg' % (scrip), data['change_inr']) REDIS_OBJ.set('%s:pc_chg' % (scrip), data['change_pc']) print scrips_data if __name__ == '__main__': cherrypy.config.update({'server.socket_host': '0.0.0.0'}) current_dir = os.path.dirname(os.path.abspath(__file__)) Monitor(cherrypy.engine, update_prices, frequency=300).subscribe() cherrypy.tree.mount( NSECards(), '/api/cards', { '/': { 'request.dispatch': cherrypy.dispatch.MethodDispatcher(), 'tools.sessions.on': True, 'tools.response_headers.on': True, 'tools.response_headers.headers': [('Content-Type', 'text/plain')], } }) static_conf = { '/static': { 'tools.staticdir.on': True, 'tools.staticdir.dir': os.path.join(current_dir, 'public') }
def __init__(self, bus): Monitor.__init__(self, bus, self.run, frequency=CachePlugin.FREQUENCY) self.index = 0
def start(self): self._gc() self._unserialize() Monitor.start(self)
def __init__(self, config): print('[Setting Configuration]') self.HIVE_ID = socket.gethostname() print('[Loading Config File]') with open(config) as config_file: settings = json.loads(config_file.read()) for key in settings: try: getattr(self, key) except AttributeError as error: print('\t' + key + ' : ' + str(settings[key])) setattr(self, key, settings[key]) print('[Initializing Monitor]') try: Monitor(cherrypy.engine, self.update, frequency=self.CHERRYPY_INTERVAL).subscribe() print('\tOKAY') except Exception as error: print('\tERROR: %s' % str(error)) if self.CSV_ENABLED: print('[Initializing CSV Logs]') for param in self.PARAMS: try: open('data/' + param + '.csv', 'a') print('\tUSING EXISTING FILE: ' + param) except Exception: print('\tCREATING NEW FILE: ' + param) with open('data/' + param + '.csv', 'w') as csv_file: csv_file.write('date,val,\n') # no spaces! if self.ZMQ_ENABLED: print('[Initializing ZMQ]') try: self.context = zmq.Context() self.socket = self.context.socket(zmq.REQ) self.socket.connect(self.ZMQ_SERVER) self.poller = zmq.Poller() self.poller.register(self.socket, zmq.POLLIN) print('\tOKAY') except Exception as error: print('\tERROR: %s' % str(error)) if self.LOG_ENABLED: print('[Initializing Log File]') try: logging.basicConfig(filename=self.LOG_FILE,level=logging.DEBUG) except Exception as error: print('\tERROR: %s' % str(error)) if self.ARDUINO_ENABLED: print('[Initializing Arduino]') try: self.arduino = Serial(self.ARDUINO_DEV, self.ARDUINO_BAUD, timeout=self.ARDUINO_TIMEOUT) print('\tOKAY') except Exception as error: print('\tERROR: %s' % str(error)) if self.MICROPHONE_ENABLED: print('[Initializing Microphone]') try: asound = cdll.LoadLibrary('libasound.so') asound.snd_lib_error_set_handler(C_ERROR_HANDLER) # Set error handler mic = pyaudio.PyAudio() self.microphone = mic.open( format=self.MICROPHONE_FORMAT, channels=self.MICROPHONE_CHANNELS, rate=self.MICROPHONE_RATE, input=True, frames_per_buffer=self.MICROPHONE_CHUNK ) self.microphone.stop_stream() print('\tOKAY') except Exception as error: print('\tERROR: %s' % str(error))
zone_control.exposed = True zone_table.exposed = True zone_history.exposed = True # Have to use stptime to ensure it's available in threads datetime.strptime("Sun Aug 31 20:44:50 2012", "%a %b %d %H:%M:%S %Y") # GPIO on the RaspberryPy can be addressed as board resources or # something else RPi.GPIO.setmode(RPi.GPIO.BOARD) # Not sure why I need this cherrypy.config.update({ 'server.socket_host': '192.168.1.3', 'server.socket_port': 80, }) # Create zones and assign IDs for them drippy = Drippy([Green(), Red()]) # This calls the run() method on drippy once a second. I have no idea # how/why this works, but it does. # # http://stackoverflow.com/questions/9207591/cherrypy-with-additional-threads-for-custom-jobs # Monitor(cherrypy.engine, drippy.run, frequency=1).subscribe() # Finally, start the web server cherrypy.quickstart(drippy)
def start(self): self._stopped = False write_handlers.init_indexes() Monitor.start(self)
}, ) }) kitana = Kitana(prefix=prefix, proxy_assets=args.shadow_assets, timeout=args.timeout, plextv_timeout=args.plextv_timeout, plugin_identifier=args.plugin_identifier, language=args.plugin_language, only_owned=not args.allow_not_owned, running_as=Kitana.get_running_as()) cherrypy.tree.mount(kitana, prefix, conf) cherrypy.engine.start() cherrypy.engine.publish('compile_sass') env.globals['url'] = kitana.template_url env.globals['static'] = kitana.static_url env.globals["render_messages"] = render_messages if kitana.running_as != "git": Monitor(cherrypy.engine, lambda: update_check(kitana), frequency=3600 * 6, name="UpdateCheck").subscribe() update_check(kitana) cherrypy.engine.signals.subscribe() cherrypy.engine.block()
def initialise(config_file_path=None, config_overwrites = None, start_now = False): global cfg config, cfg = load_config(config_file_path) cfg['active'] = True #the config file contains default application monitoring, which can be shared by all instances of the same application #ie, endpoints to monitor / ignore #config overwrites can be specified by the appication afterwards for things that may change between rutimes #ie, cherrypyformance server to log to # frequency of logging if config_overwrites and type(config_overwrites) is dict: for k,v in config_overwrites.iteritems(): cfg[k] = v global stat_logger stat_logger = setup_logging() global push_stats push_stats = create_output_fn() global stats_package_template stats_package_template = {'metadata': cfg['metadata'], 'type': 'default_type', 'stats': []} if cfg['functions']: from function_profiler import decorate_functions # call this now and later, that way if imports overwrite our wraps # then we re-wrap them again at engine start. if start_now: decorate_functions() else: cherrypy.engine.subscribe('start', decorate_functions, 0) if cfg['handlers']: from handler_profiler import decorate_handlers # no point wrapping these now as they won't be active before # engine start. if start_now: decorate_handlers() else: cherrypy.engine.subscribe('start', decorate_handlers, 0) if config.getboolean('sql', 'sql_enabled'): from sql_profiler import decorate_connections # call this now and later, that way if imports overwrite our wraps # then we re-wrap them again at engine start. if start_now: decorate_connections() else: cherrypy.engine.subscribe('start', decorate_connections, 0) if cfg['files']['files_enabled']: from file_profiler import decorate_open # this is very unlikely to be overwritten, call asap. decorate_open() from stats_flushers import flush_stats # create a monitor to periodically flush the stats buffers at the flush_interval flush_mon = Monitor(cherrypy.engine, flush_stats, frequency=int(cfg['output']['flush_interval']), name='Flush stats buffers') flush_mon.subscribe() if start_now: flush_mon.start()
def initialise(config_file_path=None, config_overwrites=None, start_now=False): global cfg config, cfg = load_config(config_file_path) cfg['active'] = True #the config file contains default application monitoring, which can be shared by all instances of the same application #ie, endpoints to monitor / ignore #config overwrites can be specified by the appication afterwards for things that may change between rutimes #ie, cherrypyformance server to log to # frequency of logging if config_overwrites and type(config_overwrites) is dict: for k, v in config_overwrites.iteritems(): cfg[k] = v global stat_logger stat_logger = setup_logging() global push_stats push_stats = create_output_fn() global stats_package_template stats_package_template = { 'metadata': cfg['metadata'], 'type': 'default_type', 'stats': [] } if cfg['functions']: from function_profiler import decorate_functions # call this now and later, that way if imports overwrite our wraps # then we re-wrap them again at engine start. if start_now: decorate_functions() else: cherrypy.engine.subscribe('start', decorate_functions, 0) if cfg['handlers']: from handler_profiler import decorate_handlers # no point wrapping these now as they won't be active before # engine start. if start_now: decorate_handlers() else: cherrypy.engine.subscribe('start', decorate_handlers, 0) if config.getboolean('sql', 'sql_enabled'): from sql_profiler import decorate_connections # call this now and later, that way if imports overwrite our wraps # then we re-wrap them again at engine start. if start_now: decorate_connections() else: cherrypy.engine.subscribe('start', decorate_connections, 0) if cfg['files']['files_enabled']: from file_profiler import decorate_open # this is very unlikely to be overwritten, call asap. decorate_open() from stats_flushers import flush_stats # create a monitor to periodically flush the stats buffers at the flush_interval flush_mon = Monitor(cherrypy.engine, flush_stats, frequency=int(cfg['output']['flush_interval']), name='Flush stats buffers') flush_mon.subscribe() if start_now: flush_mon.start()
def stop(self): self._serialize() Monitor.stop(self)
def __init__(self): self.objRedis = redis.StrictRedis(host='127.0.0.1', port=6379, db=0) self.browser = webdriver.PhantomJS() Monitor(cherrypy.engine, self.scrape_data, frequency=300).subscribe()
"Access-Control-Allow-Methods"] = "GET, POST, HEAD, PUT, DELETE" cherrypy.response.headers[ "Access-Control-Allow-Headers"] = "Cache-Control, X-Proxy-Authorization, X-Requested-With" cherrypy.response.headers["Access-Control-Max-Age"] = "604800" cherrypy.tools.CORS = cherrypy.Tool('before_handler', CORS) cherrypy.tree.mount(Busca(), '/busca', config={ '/': { 'request.dispatch': cherrypy.dispatch.MethodDispatcher(), 'tools.response_headers.on': True, 'tools.CORS.on': True, 'tools.response_headers.headers': [('Content-Type', 'text/plain')], } }) cherrypy.config.update({'server.socket_host': '0.0.0.0'}) Monitor(cherrypy.engine, consultaTransitivaBackground, frequency=1).subscribe() Monitor(cherrypy.engine, cleanFilesBySize, frequency=10000).subscribe() cherrypy.engine.start() cherrypy.engine.block()