def simplehttpsend(JSON): #return -1 #used for TESTING and simulate failure.. TO BE REMOVED try: timeoutlen = 10 + int(len(JSON) / 100000) headers = {'Content-Type': 'application/json'} resp = requests.post(datahandler.POSTURL, headers=headers, data=JSON, auth=HTTPBasicAuth(loginhandler.MY_MAC, loginhandler.MY_TOKEN), verify=loginhandler.certpath, timeout=timeoutlen) if resp.status_code == 200: log_handler.DBG("Json Successfully sent!") return 0 elif resp.status_code == 400: log_handler.ERR( "The Json Was wrong...I will delete it.. next log line is not correct." ) return 0 elif resp.status_code == 413: log_handler.ERR( "The Json was TOO BIG to be handled by the server and will be lost...reduce the stored file size or reduce the sleeping time for the thread!" ) return 0 #we return 0 so we delete the data... the data will be lost else: log_handler.WARN("JSON failed to send: " + str(resp.status_code)) return -1 except Exception as e: log_handler.WARN("File Send Failed" + str(e)) return -1 #Simulate failure! --->0 Success, -1= failure
def httplogsent(logjson): #return -1 #used for TESTING and simulate failure.. TO BE REMOVED try: headers = {'Content-Type': 'application/json'} resp = requests.post(POSTLOGURLS, data=logjson, headers=headers, auth=HTTPBasicAuth(loginhandler.MY_MAC, loginhandler.MY_TOKEN),) if resp.status_code==200: log_handler.INFO("LOGs Successfully sent!") return 0 else: log_handler.WARN("LOGs failed to send: "+ str(resp.status_code)) return -1 except Exception as e: log_handler.WARN("LOGs Send Failed" +str(e)) return -1 #Simulate failure! --->0 Success, -1= failure
def send_storedjason(file): if os.path.isfile(file) and os.path.getsize(file) > 0: try: datahandler.jsonmutex.acquire( ) #this file can also be written from the other thread... we need to lock it with open(file, mode='r+') as f: jtext = f.read() + "]}" f.close() if simplehttpsend( jtext ) == 0: #there is a file...send it and if ok than delete it os.remove(file) log_handler.DBG("JSON file SUCCESSFULLY sent!" + file) return 0 else: log_handler.INFO("JSON_FILE send FAILED!" + file) except: log_handler.WARN("Unable to get MUTEX on file: " + file) return -1 finally: datahandler.jsonmutex.release() else: log_handler.DBG("JSON File was not there...nothing to send!") return -1
def datahandler(datafile, sleeptime, filenoconnection): while utility.CONTINUE_RUNNING: #CONTINUE_RUNNING is set false by SIGINT signal handler try: if os.path.isfile(datafile) and os.path.getsize( datafile ) > 0: #if there is a file and is larger than 0 byte data = '{ "dataarray": [ \n' + datatojson( datafile)[:-1] + "]} \n" if httpsend( data ) != 0: # Try to send the stored data as jason format... if sendhttpdata fails log_handler.WARN( "Unable to send data. Storing it in local file") storejson( data, filenoconnection) # than store jason formated version os.remove( datafile ) # we can delete the file here, since we stored the json formatted version of it log_handler.DBG("Data file rowdata.brn removed") else: log_handler.INFO("Data file NOT found in :" + str(datafile) + ". Trying again in: " + str(sleeptime) + " seconds") except Exception as e: log_handler.ERR( "An exception happed while i was deleting or storing datafile!" + str(e)) time.sleep(sleeptime)
def init_position(): try: myposition=requests.get("http://freegeoip.net/xml/", timeout=10) soup = BeautifulSoup(myposition.text, 'html.parser') global latitude global longitude latitude=soup.latitude.text longitude=soup.longitude.text log_handler.INFO("Position Successfully Initialized..") except: log_handler.WARN("Unable to initialize/find my position") pass
def send_log(): if os.path.isfile(log_handler.LOG_FILE+".1") and os.path.getsize(log_handler.LOG_FILE+".1") > 0: #we only sent the last log this is why .1 try: myloglock.acquire() myje=logtojson(log_handler.LOG_FILE+".1") ret= httplogsent(myje) # if success return 0. otherwise -1 if ret==0: os.remove(log_handler.LOG_FILE+".1") return 0 else: log_handler.WARN("Failed to sent log file") return -1 except Exception as e: log_handler.WARN("Problem while sending log files: "+ str(e)) return -1 finally: myloglock.release() else: log_handler.INFO("No Log to Sent") return -1
def httpsend(JSON): #return -1 #used for TESTING and simulate failure.. TO BE REMOVED try: if JSON == '{ "dataarray": [ \n]} \n': #return 0 so we do not append empty json to the json file return 0 headers = {'Content-Type': 'application/json'} resp = requests.post(POSTURL, headers=headers, data=JSON, auth=HTTPBasicAuth(loginhandler.MY_MAC, loginhandler.MY_TOKEN), verify=loginhandler.certpath, timeout=10) if resp.status_code == 200: log_handler.DBG("Json Successfully sent!") return 0 elif resp.status_code == 400: log_handler.INFO( "The Json was wrongly formatted... probably empty?") return 0 #we return 0 so we do not append a wrong json to the json file elif resp.status_code == 413: log_handler.ERR( "The Json was TOO BIG to be handled by the server and will be lost...reduce the stored file size or reduce the sleeping time for the thread!" ) return 0 #we return 0 so we delete the data... the data will be lost elif resp.status_code == 401: os.remove(loginhandler.TOKEN_FILE) log_handler.ERR( "GOT UNAUTHORIZED BACK... I WILL DELETE THE TOKEN ADN TRY TO REFETCH IT FROM ONLINE" ) loginhandler.init_tokenhandler() return -1 else: log_handler.WARN("JSON failed to send: " + str(resp.status_code)) return -1 except Exception as e: log_handler.WARN("File Send Failed" + str(e)) return -1 #Simulate failure! --->0 Success, -1= failure
def handle_storedjson(file, sleeptime): while utility.CONTINUE_RUNNING: #continue_running is set to false from the sig_int handler.. try: for i in range(datahandler.NumStoredjson, 0, -1): if os.path.isfile( file + '.rot' + str(i)): #lets loop and send all the stored files send_storedjason(file + '.rot' + str(i)) send_storedjason(file) #finally we also send the current file... if log_handler.SERVER_LOG: #we send the logs only if is set the relative variable if storedloghandler.send_log() == 0: log_handler.INFO("Log File Sent and locally removed!") time.sleep(sleeptime) except Exception as e: log_handler.WARN("Problem while sending data or logs:" + str(e)) time.sleep(sleeptime)
def storedata(data, filepath): try: datahandler.rawmutex.acquire() timestamp = int(1000 * time.time()) if (not os.path.isfile(filepath)) or os.path.getsize(filepath) == 0: with open(filepath, mode='w') as f: f.write(str(data + str(struct.pack('=q', timestamp)) + "\n")) f.close() else: with open(filepath, mode='a') as f: f.write(str(data + str(struct.pack('q', timestamp)) + "\n")) #print sys.getsizeof(data+str(struct.pack('q',timestamp))+"\n") f.close() except: log_handler.WARN( "Failed to acquire the MUTEX Lock or to write data to: " + filepath) finally: datahandler.rawmutex.release()
def storejson(data, datafile): doRollOver(data, datafile) try: jsonmutex.acquire() if (not os.path.isfile(datafile)) or os.path.getsize(datafile) == 0: with open(datafile, mode='w') as f: f.write('{ "datacollection": [ \n' + data) f.close() log_handler.INFO( "Successfully stored JSON formatted data in a NEW file called: " + datafile) else: with open(datafile, mode='a') as f: f.write("," + data) f.close() log_handler.INFO( "Successfully stored JSON formatted data in a EXISTING file called: " + datafile) except: log_handler.WARN("Failed to acquire the MUTEX Lock on :" + datafile) finally: jsonmutex.release()