def listening(self): sk = socket.socket() # 防止 ctrl+c 后占用端口 sk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sk.bind((ip, port)) sk.listen(1) print toolbox.put_color('Master is online', "green") while 1: try: conn, from_ip = sk.accept() api_key = conn.recv(1024) if api_key == self.api_key: # 通过认证 conn.sendall('Go on') worker = Worker(conn) th = threading.Thread(target=worker.working) th.start() else: msg = u"来自 %s:%s 的非法访问. Silence is gold" % from_ip print toolbox.put_color(msg, "yellow") conn.sendall('Silence is gold...') except KeyboardInterrupt: print toolbox.put_color('Master is offline', "red") break except Exception, e: print toolbox.put_color(u"出现一个隐藏问题\n [-]" + str(e), "red") print "-" * 50 toolbox.log( traceback.format_exc(), level="error", description="Master reported an error", path=".master_log" ) break
def route_account(self): create_success = False if self.method == "POST": # Parse body. body = self.parse_body() username = body.get("username", "") password = body.get("password", "") # Check if this username is used. if not (check_account_exist(username, password)) and (username != ""): add_account(username, password) # Add this user to account_list. log("username and password", username, password) create_success = True header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" body = template("create_success.html") if create_success else template( "account.html") if not create_success: # Decide if user create account or not. if (self.method == "POST"): body = body.replace('{{message}}', "This username cannot be used !") else: body = body.replace('{{message}}', "") reply = header + body return reply.encode(encoding="utf-8")
def run_socket(host, port): # HTTPs # Use below to create cert and key. # openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 # openssl rsa -in key.pem -out key.pem (去除輸入密碼環節) context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) context.load_cert_chain('./cert.pem', './key.pem') s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Reuse port. s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((host, port)) s.listen(5) s = context.wrap_socket(s, server_side=True) # Use double fork to implement multithread. while True: try: (conn, addr) = s.accept() if (os.fork() == 0): if (os.fork() == 0): handle_socket(conn) quit() else: conn.close() os.wait() except KeyboardInterrupt: # Press Ctrl-c exit. quit() except Exception as e: log("error", e)
def api_getImagesList(): """ 所有镜像的列表 返回值示例 dicts = { "code": 1, "msg": "", "result": [ "image_name_1", "image_name_2" ] } """ results = {"code": 0, "msg": "", "result": []} host = '192.168.26.7:5000/' try: results["result"] = [ host + i for i in json.loads( commands.getoutput('curl -s 127.0.0.1:5000/v2/_catalog')) ['repositories'] ] except Exception as e: toolbox.log(traceback.format_exc(), level="error", description="get all images failed", path=".slave_log") results["code"] = 1 results["msg"] = "master(%s) report a error: %s" % ( setting["bridge"]["self_ip"], str(e)) return results
def kill(self): log("process.terminate()") self.process.terminate() sleep(0.5) log("process.kill()") self.process.kill() sleep(0.5)
def write(self, txt): try: print('txt:', txt) self.process.stdin.write(txt.encode() + b'\r\n') self.process.stdin.flush() except Exception as e: log("Error while writting to stdin\n" + str(e)) #self.process.stdin.write(str(self.c)+" "+txt+"\n") self.c += 1
def check_cookie(cookie, pathname="Cookie.npy"): Cookie_list = {} try: Cookie_list = load_dict(pathname) except: pass check = Cookie_list.get(cookie, False) log("check:", check) return check
def parse_body(self): # url ASCII code decode self.body = urllib.parse.unquote(self.body) log("body is ", self.body) args = self.body.split("&") f = {} for arg in args: key, value = arg.split("=") f[key] = value return f
def place_black(self,move): if move == "RESIGN": log("WARNING: trying to play RESIGN as GTP move") self.history.append(["b",move]) return True self.write("play black "+move) answer=self.readline() if answer[0]=="=": self.history.append(["b",move]) return True else:return False
def consume_stderr(self): while 1: try: err_line = self.process.stderr.readline() if err_line: self.stderr_queue.put(err_line) else: log("leaving consume_stderr thread") return except Exception: log("leaving consume_stderr thread due to exception") return
def set_free_handicap(self,positions): stones="" for p in positions: stones+=p+" " log("Setting handicap stones at",stones.strip()) self.write("set_free_handicap "+stones.strip()) answer=self.readline().strip() try: if answer[0]=="=": return True else: return False except Exception, e: raise GtpException("GtpException in set_free_handicap()\nanswer='"+answer+"'\n"+str(e))
def check_account(username, password, pathname="Account_list.npy"): # Read dict from file. Account_list = {} try: Account_list = load_dict(pathname) except: pass user_password = Account_list.get(username, -1) log("user and password:"******"True password:", user_password) if user_password == password: return True else: return False
def test(self, gtp_bot, profil): from gtp_terminal import Terminal if profil == "slow": command = self.current_settings.SlowCommand.get() parameters = self.current_settings.SlowParameters.get().split() if profil == "fast": command = self.current_settings.FastCommand.get() parameters = self.current_settings.FastParameters.get().split() if not command: log("Empty command line!") return popup = Terminal(self.parent, gtp_bot, [command] + parameters) self.parent.add_popup(popup)
def save(self): global lang, translations log("Saving GRP settings") Config = ConfigParser.ConfigParser() Config.read(config_file) for lang2, language in available_translations.iteritems(): if language == self.Language.get(): if lang != lang2: Config.set("General", "Language", lang2) break Config.set("Review", "FuzzyStonePlacement", self.FuzzyStonePlacement.get()) Config.set("Review", "RealGameSequenceDeepness", self.RealGameSequenceDeepness.get()) Config.set("Review", "GobanScreenRatio", self.GobanScreenRatio.get()) Config.set("Analysis", "MaxVariations", self.MaxVariationsToRecord.get()) Config.set("Analysis", "SaveCommandLine", self.SaveCommandLine.get()) Config.set("Analysis", "StopAtFirstResign", self.StopAtFirstResign.get()) Config.set("Review", "MaxVariations", self.MaxVariationsToDisplay.get()) coloring = { _("Winning variations (>50%) only in blue"): "blue_for_winning", _("The best variation in blue"): "blue_for_best", _("Variations better than actual game move in blue"): "blue_for_better" } Config.set("Review", "VariationsColoring", coloring[self.VariationsColoring.get().encode("utf")]) Config.set("Review", "InvertedMouseWheel", self.InvertedMouseWheel.get()) Config.set("Analysis", "NoVariationIfSameMove", self.NoVariationIfSameMove.get()) labeling = {_("Letters"): "letter", _("Rates"): "rate"} Config.set("Review", "VariationsLabel", labeling[self.VariationsLabel.get().encode("utf")]) Config.write(open(config_file, "w")) if self.refresh != None: self.refresh()
def terminate(self): t = 10 while 1: self.quitting_thread.join(0.0) if not self.quitting_thread.is_alive(): log(1, "The bot has quitted properly") break elif t == 0: log(1, "The bot is still running...") log(1, "Forcefully closing it now!") break t -= 1 log(1, "Waiting for the bot to close", t, "s") sleep(1) try: self.process.kill() self.process.stdin.close() except: log(1, "Something went wrong with process.kill") pass
def consume_stderr(self): while 1: try: err_line=self.process.stderr.readline().decode("utf-8") if err_line: log("#",err_line.strip()) self.stderr_queue.put(err_line) else: log("leaving consume_stderr thread") return except Exception,e: log("leaving consume_stderr thread due to exception") log(e) return
def handle_socket(conn): request = Request() receive_message = conn.recv(4096) # Transform receive_message from bytes to str. receive_message = receive_message.decode('utf-8') log("receive:", receive_message) # HTTP message processing. try: # Get path and query. path = receive_message.split()[1] request.parse_path(path) # Get cookie content. request.get_cookie(receive_message) # Get method and body. request.method = receive_message.split()[0] request.body = receive_message.split('\r\n\r\n')[1] # Check if body is empty. If body is empty, treats it as a GET package. request.method = "POST" if request.body != "" else "GET" # Respond corresponding to receive route. response_message = request.response() conn.sendall(response_message) except Exception as e: log("error", e) # Close connection. log("conn end", request.method) conn.close()
def main(): options, args = params() dir = options.sgf_directory file = options.sgf_file start_move = options.start_move force = options.force append = options.append profiles = options.profiles output = options.output toolbox.verbose = options.verbose if dir is not None: if not os.path.exists(dir): toolbox.log(1, "Directory with sgf doesn't exist. Quit now") sys.exit() else: leela = toolbox.MasterAnalyze(dir, start_move, profiles, output, force, append) leela.start() elif file is not None: if not os.path.exists(file): toolbox.log(1, "Sgf-file doesn't exist. Quit now") sys.exit() else: leela = toolbox.MasterAnalyze(file, start_move, profiles, output, force, append) leela.start() else: toolbox.log( 1, "Sgf-file or directory with sgf-files wasn't specified. Quit now") sys.exit()
def save(self): global lang, translations log("Saving GRP settings") Config = ConfigParser.ConfigParser() Config.read(config_file) for lang2, language in available_translations.iteritems(): if language==self.Language.get(): if lang!=lang2: Config.set("General","Language",lang2) break Config.set("Review","FuzzyStonePlacement",self.FuzzyStonePlacement.get()) Config.set("Review","RealGameSequenceDeepness",self.RealGameSequenceDeepness.get()) Config.set("Review","GobanScreenRatio",self.GobanScreenRatio.get()) Config.set("Analysis","MaxVariations",self.MaxVariationsToRecord.get()) Config.set("Analysis","SaveCommandLine",self.SaveCommandLine.get()) Config.set("Analysis","StopAtFirstResign",self.StopAtFirstResign.get()) Config.set("Review","MaxVariations",self.MaxVariationsToDisplay.get()) Config.write(open(config_file,"w")) if self.parent!=None: self.parent.refresh()
def working(self): ''' 多线程处理 api 调用 具体的事情留给 _api() 处理,这里只处理异常 ''' try: self._api() except Exception, e: print toolbox.put_color(u"调用 _api() 时出现问题\n [-]" + str(e), "red") print "-" * 50 toolbox.log( traceback.format_exc(), level="error", description="Something went wrong in _api()", path=".master_log" ) self.conn.sendall(json.dumps([{ "code": 1, "msg": 'Something went wrong', "result": "", }]))
def route_message(self): msgs = "" msg = Message() if self.method == "POST": # Parse body. body = self.parse_body() msg.author = body.get("author", "") msg.message = body.get("message", "") msg.save_txt() # Load previous message from file. try: msgs = msg.load_txt() except: pass header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" body = template("message.html") log("msgs ", msgs) body = body.replace('{{message}}', msgs) reply = header + body return reply.encode(encoding="utf-8")
def terminate(self): t=10 while 1: self.quitting_thread.join(0.0) if not self.quitting_thread.is_alive(): log("The bot has quitted properly") break elif t==0: log("The bot is still running...") log("Forcefully closing it now!") break t-=1 log("Waiting for the bot to close",t,"s") sleep(1) try: self.process.kill() except: pass try: self.process.stdin.close() except: pass
def consume_stdout(self): while 1: try: line=self.process.stdout.readline().decode("utf-8") if line: self.stdout_queue.put(line) else: log("leaving consume_stdout thread") return except Exception, e: log("leaving consume_stdout thread due to exception") log(e) return
def consume_stderr(self): while 1: try: err_line=self.process.stderr.readline() if err_line: self.stderr_queue.put(err_line) else: log("leaving consume_stderr thread") return except Exception, e: import sys, os exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] log(exc_type, fname, exc_tb.tb_lineno) log("leaving consume_stderr thread due to exception") return
def display_GRP_settings(self,top_setting_frame): log("Initializing GRP setting interface") Config = ConfigParser.ConfigParser() Config.read(config_file) setting_frame=Frame(top_setting_frame) row=0 Label(setting_frame,text=_("%s settings")%"Go Review Partner", font="-weight bold").grid(row=row,column=1,sticky=W) row+=1 Label(setting_frame,text="").grid(row=row,column=1) row+=1 Label(setting_frame,text=_("General parameters")).grid(row=row,column=1,sticky=W) row+=1 Label(setting_frame,text=_("Language")).grid(row=row,column=1,sticky=W) Language = StringVar() Language.set(available_translations[lang]) OptionMenu(setting_frame,Language,*tuple(available_translations.values())).grid(row=row,column=2,sticky=W) row+=1 Label(setting_frame,text="").grid(row=row,column=1) row+=1 Label(setting_frame,text=_("Parameters for the analysis")).grid(row=row,column=1,sticky=W) row+=1 Label(setting_frame,text=_("Maximum number of variations to record during analysis")).grid(row=row,column=1,sticky=W) MaxVariationsToRecord = StringVar() MaxVariationsToRecord.set(Config.get("Analysis","MaxVariations")) Entry(setting_frame, textvariable=MaxVariationsToRecord, width=30).grid(row=row,column=2) row+=1 Label(setting_frame,text=_("Save bot command line into RSGF file")).grid(row=row,column=1,sticky=W) SaveCommandLine = BooleanVar(value=Config.getboolean('Analysis', 'SaveCommandLine')) SaveCommandLineCheckbutton=Checkbutton(setting_frame, text="", variable=SaveCommandLine,onvalue=True,offvalue=False) SaveCommandLineCheckbutton.grid(row=row,column=2,sticky=W) SaveCommandLineCheckbutton.var=SaveCommandLine row+=1 Label(setting_frame,text=_("Stop the analysis if the bot resigns")).grid(row=row,column=1,sticky=W) StopAtFirstResign = BooleanVar(value=Config.getboolean('Analysis', 'StopAtFirstResign')) StopAtFirstResignCheckbutton=Checkbutton(setting_frame, text="", variable=StopAtFirstResign,onvalue=True,offvalue=False) StopAtFirstResignCheckbutton.grid(row=row,column=2,sticky=W) StopAtFirstResignCheckbutton.var=StopAtFirstResign row+=1 Label(setting_frame,text="").grid(row=row,column=1) row+=1 Label(setting_frame,text=_("Parameters for the review")).grid(row=row,column=1,sticky=W) row+=1 Label(setting_frame,text=_("Fuzzy Stone")).grid(row=row,column=1,sticky=W) FuzzyStonePlacement = StringVar() FuzzyStonePlacement.set(Config.get("Review","FuzzyStonePlacement")) Entry(setting_frame, textvariable=FuzzyStonePlacement, width=30).grid(row=row,column=2) row+=1 Label(setting_frame,text=_("Real game sequence deepness")).grid(row=row,column=1,sticky=W) RealGameSequenceDeepness = StringVar() RealGameSequenceDeepness.set(Config.get("Review","RealGameSequenceDeepness")) Entry(setting_frame, textvariable=RealGameSequenceDeepness, width=30).grid(row=row,column=2) row+=1 Label(setting_frame,text=_("Goban/screen ratio")).grid(row=row,column=1,sticky=W) GobanScreenRatio = StringVar() GobanScreenRatio.set(Config.get("Review","GobanScreenRatio")) Entry(setting_frame, textvariable=GobanScreenRatio, width=30).grid(row=row,column=2) row+=1 Label(setting_frame,text=_("Maximum number of variations to display during review")).grid(row=row,column=1,sticky=W) MaxVariationsToDisplay = StringVar() MaxVariationsToDisplay.set(Config.get("Review","MaxVariations")) Entry(setting_frame, textvariable=MaxVariationsToDisplay, width=30).grid(row=row,column=2) self.Language=Language self.FuzzyStonePlacement=FuzzyStonePlacement self.RealGameSequenceDeepness=RealGameSequenceDeepness self.GobanScreenRatio=GobanScreenRatio self.MaxVariationsToRecord=MaxVariationsToRecord self.SaveCommandLine=SaveCommandLine self.StopAtFirstResign=StopAtFirstResign self.MaxVariationsToDisplay=MaxVariationsToDisplay setting_frame.save=self.save return setting_frame
def write(self,txt): try: self.process.stdin.write(txt+"\n") except Exception, e: log("Error while writting to stdin\n"+unicode(e))
def close(self): log("Now closing") self.quitting_thread=threading.Thread(target=self.quit) self.quitting_thread.start() threading.Thread(target=self.terminate).start()
def get_cookie(self, receive_message): cookie_str = receive_message.split("\r\n")[-3] self.cookie = cookie_str.split("=")[1] log("Cookie:", self.cookie)
'code': 0, 'msg': '', 'result': [] } for i in setting["slave_ip"]} } for ip in setting["slave_ip"]: result = json.loads(toolbox.send_mission(ip, mission)) if result["code"]: results['result'][ip]['code'] = 1 results['result'][ip]['msg'] = result['msg'] else: results['result'][ip]['result'] = result['result'] return results # ------------- 载入配置 ------------- try: with open(".setting", "r") as fp: setting = json.load(fp) except Exception as e: print toolbox.put_color(u"载入配置出错", "red") print str(e) toolbox.log(traceback.format_exc(), level="error", description="load setting failed!", path=".slave_log") raise
def close(self): log("closing popup") self.destroy() self.parent.remove_popup(self) log("done")
def display_GRP_settings(self, top_setting_frame): log("Initializing GRP setting interface") Config = ConfigParser.ConfigParser() Config.read(config_file) setting_frame = Frame(top_setting_frame) row = 0 Label(setting_frame, text=_("%s settings") % "Go Review Partner", font="-weight bold").grid(row=row, column=1, sticky=W) row += 1 Label(setting_frame, text="").grid(row=row, column=1) row += 1 Label(setting_frame, text=_("General parameters")).grid(row=row, column=1, sticky=W) row += 1 Label(setting_frame, text=_("Language")).grid(row=row, column=1, sticky=W) Language = StringVar() Language.set(available_translations[lang]) OptionMenu(setting_frame, Language, *tuple(available_translations.values())).grid(row=row, column=2, sticky=W) row += 1 Label(setting_frame, text="").grid(row=row, column=1) row += 1 Label(setting_frame, text=_("Parameters for the analysis")).grid(row=row, column=1, sticky=W) row += 1 Label(setting_frame, text=_("Maximum number of variations to record during analysis") ).grid(row=row, column=1, sticky=W) MaxVariationsToRecord = StringVar() MaxVariationsToRecord.set(Config.get("Analysis", "MaxVariations")) Entry(setting_frame, textvariable=MaxVariationsToRecord, width=30).grid(row=row, column=2) row += 1 Label(setting_frame, text=_("Only keep variations when game move and bot move differ") ).grid(row=row, column=1, sticky=W) NoVariationIfSameMove = BooleanVar() NoVariationIfSameMove.set( Config.getboolean("Analysis", "NoVariationIfSameMove")) NoVariationIfSameMoveCheckbutton = Checkbutton( setting_frame, text="", variable=NoVariationIfSameMove, onvalue=True, offvalue=False) NoVariationIfSameMoveCheckbutton.grid(row=row, column=2, sticky=W) NoVariationIfSameMoveCheckbutton.var = NoVariationIfSameMove row += 1 Label(setting_frame, text=_("Save bot command line into RSGF file")).grid(row=row, column=1, sticky=W) SaveCommandLine = BooleanVar( value=Config.getboolean('Analysis', 'SaveCommandLine')) SaveCommandLineCheckbutton = Checkbutton(setting_frame, text="", variable=SaveCommandLine, onvalue=True, offvalue=False) SaveCommandLineCheckbutton.grid(row=row, column=2, sticky=W) SaveCommandLineCheckbutton.var = SaveCommandLine row += 1 Label(setting_frame, text=_("Stop the analysis if the bot resigns")).grid(row=row, column=1, sticky=W) StopAtFirstResign = BooleanVar( value=Config.getboolean('Analysis', 'StopAtFirstResign')) StopAtFirstResignCheckbutton = Checkbutton(setting_frame, text="", variable=StopAtFirstResign, onvalue=True, offvalue=False) StopAtFirstResignCheckbutton.grid(row=row, column=2, sticky=W) StopAtFirstResignCheckbutton.var = StopAtFirstResign row += 1 Label(setting_frame, text="").grid(row=row, column=1) row += 1 Label(setting_frame, text=_("Parameters for the review")).grid(row=row, column=1, sticky=W) row += 1 Label(setting_frame, text=_("Fuzzy Stone")).grid(row=row, column=1, sticky=W) FuzzyStonePlacement = StringVar() FuzzyStonePlacement.set(Config.get("Review", "FuzzyStonePlacement")) Entry(setting_frame, textvariable=FuzzyStonePlacement, width=30).grid(row=row, column=2) row += 1 Label(setting_frame, text=_("Real game sequence deepness")).grid(row=row, column=1, sticky=W) RealGameSequenceDeepness = StringVar() RealGameSequenceDeepness.set( Config.get("Review", "RealGameSequenceDeepness")) Entry(setting_frame, textvariable=RealGameSequenceDeepness, width=30).grid(row=row, column=2) row += 1 Label(setting_frame, text=_("Goban/screen ratio")).grid(row=row, column=1, sticky=W) GobanScreenRatio = StringVar() GobanScreenRatio.set(Config.get("Review", "GobanScreenRatio")) Entry(setting_frame, textvariable=GobanScreenRatio, width=30).grid(row=row, column=2) row += 1 Label(setting_frame, text=_("Maximum number of variations to display during review") ).grid(row=row, column=1, sticky=W) MaxVariationsToDisplay = StringVar() MaxVariationsToDisplay.set(Config.get("Review", "MaxVariations")) Entry(setting_frame, textvariable=MaxVariationsToDisplay, width=30).grid(row=row, column=2) row += 1 Label(setting_frame, text=_("Blue/red coloring of the variations")).grid(row=row, column=1, sticky=W) VariationsColoring = StringVar() coloring = (_("Winning variations (>50%) only in blue"), _("The best variation in blue"), _("Variations better than actual game move in blue")) VariationsColoring.set(coloring[0]) OptionMenu(setting_frame, VariationsColoring, *coloring).grid(row=row, column=2, sticky=W) row += 1 Label(setting_frame, text=_("Labels for the variations")).grid(row=row, column=1, sticky=W) value = {"letter": _("Letters"), "rate": _("Rates")} VariationsLabel = StringVar() VariationsLabel.set(value[Config.get("Review", "VariationsLabel")]) OptionMenu(setting_frame, VariationsLabel, _("Letters"), _("Rates")).grid(row=row, column=2, sticky=W) row += 1 Label(setting_frame, text=_("Inverted mouse wheel")).grid(row=row, column=1, sticky=W) InvertedMouseWheel = BooleanVar( value=Config.getboolean('Review', 'InvertedMouseWheel')) InvertedMouseWheelCheckbutton = Checkbutton( setting_frame, text="", variable=InvertedMouseWheel, onvalue=True, offvalue=False) InvertedMouseWheelCheckbutton.grid(row=row, column=2, sticky=W) InvertedMouseWheelCheckbutton.var = InvertedMouseWheel self.Language = Language self.FuzzyStonePlacement = FuzzyStonePlacement self.RealGameSequenceDeepness = RealGameSequenceDeepness self.GobanScreenRatio = GobanScreenRatio self.MaxVariationsToRecord = MaxVariationsToRecord self.SaveCommandLine = SaveCommandLine self.StopAtFirstResign = StopAtFirstResign self.MaxVariationsToDisplay = MaxVariationsToDisplay self.VariationsColoring = VariationsColoring self.InvertedMouseWheel = InvertedMouseWheel self.NoVariationIfSameMove = NoVariationIfSameMove self.VariationsColoring = VariationsColoring self.VariationsLabel = VariationsLabel setting_frame.save = self.save return setting_frame