def test_slice_line(self): print "::test_slice_line starts ..." sv = Server() sv.stored_previous_partial = "cuu" sv.prepared_line = "p B" sv.slice_line() self.assertEquals("cuu", sv.sm.slice_result[0])
class Master(): def __init__(self): self.client = None self.server = None def call(self, host, port): if self.client==None: self.client = Client(self) self.client.call(host, port) def callOn(self, addr): if self.client == None: self.client = Client(self) thread.start_new_thread( self.client.call, addr) def serve(self): if self.server==None: self.server = Server(self) self.server.listen() def serveOn(self, socket): if self.server==None: self.server = Server(self) thread.start_new_thread( self.server.listenTo, (socket,))
def main(): # Build the library subprocess.call(['npm', 'run', 'build']) # Copy the library to the http server path subprocess.call([ 'cp', join(BASEDIR, '..', 'dist/ether.global.js'), join(BASEDIR, 'public') ]) # Build the sample user app under test # Building the app only works if dir is set properly os.chdir(BASEDIR) subprocess.call(['node', join(BASEDIR, 'rollup.js')]) # Start the http server httpd = Server() httpd.start() # Run CasperJS tests subprocess.call([ join(BASEDIR, '..', 'node_modules/casperjs/bin/casperjs'), 'test', '--log-level=debug', '--verbose', join(BASEDIR, 'test') ]) # Stop the http server httpd.stop()
class DemoMicroBridge(ServerListener): def __init__(self): self.droid = android.Android() self.droid.webViewShow('file:///sdcard/sl4a/scripts/microbridge/demo.html') # Start server self.server = Server() self.server.add_listener(self) self.server.start() def on_server_started(self, server): print "Server Started!" def on_client_connect(self, server, client): print "Client Connected!" def on_client_disconnect(self, server, client): print "Client Disconnected!" def on_receive(self, client, string): # print "Received " + string # Received data from ADC # Arduino uses little-endian format data = 0 for d in string[::-1]: data = data << 8 | ord(d) self.droid.eventPost("ADC", str(data))
def serve_forever(self, poll_interval=0.5): # make sure every process knows the https parent pid secure_pid = os.getpid() plain_pid = os.fork() if self.plain_mode else None # when using simultaneous plain http, fork and run that part on the child. # os.fork() warns about ssl and multiprocessing. I think we're okay tho, # since only the parent does any ssl and its right on the socket??? # plain (child) part: if plain_pid == 0: # TODO: catch signals from / poll for pid of parent? try: self.plain_server.serve_forever(poll_interval) finally: # could probably investigate the nature of our pid more closely, # however pidfiles are too tryhard atm if secure_pid == os.getppid(): os.kill(secure_pid, signal.SIGTERM) # secure (parent) part: else: # might get away w/ lazy style? if more sophisticated management is desired # can override SocketServer.py copypastastyle try: Server.serve_forever(self, poll_interval) finally: # again, might want to think at least two seconds about our cleanup... # None == no fork even happened. if plain_pid is not None: os.kill(plain_pid, signal.SIGTERM)
def __init__(self, core="tarantool"): Server.__init__(self, core) self.default_bin_name = "tarantool_box" self.default_config_name = "tarantool.cfg" self.default_init_lua_name = "init.lua" # append additional cleanup patterns self.re_vardir_cleanup += ['*.snap', '*.xlog', '*.inprogress', '*.cfg', '*.sup', '*.lua'] self.process = None self.config = None self.vardir = None self.valgrind_log = "valgrind.log" self.valgrind_sup = os.path.join("share/", "%s.sup" % ('tarantool')) self.init_lua = None self.default_suppression_name = "valgrind.sup" self.pidfile = None self.port = None self.binary = None self.is_started = False self.mem = False self.start_and_exit = False self.gdb = False self.valgrind = False
def main(): import argparse parser = argparse.ArgumentParser() parser.add_argument('-w', '--waitress', action='store_true') args = parser.parse_args() config_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "pywps.cfg") processes = [ FeatureCount(), SayHello(), Centroids(), UltimateQuestion(), Sleep(), Buffer(), Area(), Viewshed() ] s = Server(processes=processes, config_file=config_file) # TODO: need to spawn a different process for different server if args.waitress: import waitress from pywps import configuration configuration.load_configuration(config_file) host = configuration.get_config_value('wps', 'serveraddress').split('://')[1] port = int(configuration.get_config_value('wps', 'serverport')) waitress.serve(s.app, host=host, port=port) else: s.run()
def run(self): """ Start HTTP daemon """ self.log("Starting new API instance on %d" % self.port) http_handler = Handler SocketServer.TCPServer.allow_reuse_address = True try: http_service = ApiServer((self.host, self.port), http_handler, manager=self.manager) except socket.error as e: self.log("Failed to bind to port. Got: %s" % str(e)) return False if self.ssl: self.manager.get_key({'hostname': self.ssl, 'algo': 'RSA', 'bits': 2048}) key_path = self.manager.get_key_path(self.ssl) while True: res = self.manager.cert({'hostname': self.ssl, 'ip': '127.0.0.1'}) if res['status'] != 'available': time.sleep(10) continue self.log("Certificate successfully received for %s" % self.ssl) break cert_path = self.manager.get_fullchain_path(self.ssl) self.log("Key: %s, Cert: %s" % (key_path, cert_path)) http_service.socket = ssl.wrap_socket(http_service.socket, keyfile=key_path, certfile=cert_path, server_side=True) self.log("HTTP API server started") while self.is_running(): http_service.handle_request()
def main(): #use argparse to get role, ip, port and user name parser = argparse.ArgumentParser( description="""PySyncIt""", formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument( '-ip', help='Specify the ip address of this machine', required=True) parser.add_argument( '-port', help='Specify the port of this machine to run rpc server', required=True) parser.add_argument( '-uname', help='Specify the user name of this machine', required=True) parser.add_argument( '-role', help='Specify the role of this machine - client or server', required=True) args = parser.parse_args() #start logging setup_logging("syncit.log.%s-%s" % (args.ip, args.port)); logger = logging.getLogger('syncIt') #Read config file config = ConfigParser.ConfigParser() logger.info("Using config file: syncit.cfg") config.read('syncit.cfg') if (args.role == 'server'): node = Server(args.role, args.ip, int(args.port), args.uname, get_watch_dirs(config, args.uname), get_clients(config)) else: node = Client(args.role, args.ip, int(args.port), args.uname, get_watch_dirs(config, args.uname), get_server_tuple(config)) node.activate()
def start(args, kill = None): config_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "pywps.cfg") processes = [ FeatureCount(), SayHello(), Centroids(), UltimateQuestion(), Sleep(), Buffer(), Area(), Box(), Warp() ] s = Server(processes=processes, config_file=config_file) # TODO: need to spawn a different process for different server if args.waitress: import waitress from pywps import configuration configuration.load_configuration(config_file) host = configuration.get_config_value('wps', 'serveraddress').split('://')[1] port = int(configuration.get_config_value('wps', 'serverport')) waitress.serve(s.app, host=host, port=port) else: s.run()
def crawl_listing(): from server import Server ss = Server() # pool = Pool(50) it = 0 num_cats = Category.objects().count() for c in Category.objects(is_leaf=True): it += 1 print c.catname, it, 'of', num_cats if c.spout_time and c.spout_time > datetime.utcnow()-timedelta(hours=8): print '...skipped' continue if c.num: num_page = (c.num - 1) // ITEM_PER_PAGE + 1 for page in xrange(1, num_page+1): page_item_NO = (page-1)*ITEM_PER_PAGE url = c.url( page_item_NO ) if page == num_page: # pool.spawn(ss.crawl_listing, url, c.catstr, page, c.num % ITEM_PER_PAGE) ss.crawl_listing( url, c.catstr(), page, c.num % ITEM_PER_PAGE) else: # pool.spawn(ss.crawl_listing, url, c.catstr, page) ss.crawl_listing( url, c.catstr(), page) progress(str(page)+' ') print c.spout_time = datetime.utcnow() c.save()
def test_assertPathOk(self) : s = Server("badpath") try : s._assertPathOk() self.fail("Exception expected") except BadServerPath, e: self.assertEqual(e.message, "badpath")
def setUp(self): temp = tempfile.NamedTemporaryFile() filename_temp = temp.name temp.close() Server.SOCKET_ATTR = (socket.AF_UNIX, socket.SOCK_STREAM) Server.SOCKET_HOST = filename_temp server = Server() worker = Worker(target=server.initSocket) worker.start() self.server = server self.worker = worker self.filename_temp = filename_temp # wait socket up while not hasattr(server, '_server_socket'): pass self.clients = [] self._new_client() self._new_client() gid = server.create_game(self.clients[0].uid, 'JungleRumble') server.join_game(self.clients[1].uid, gid) result = self.clients[0].read() # new_opponent
def main(): main_server_instance = Server(HOST, user=USER, passwd=PASSWD, port=PORT, use_ssl=USE_SSL) manager = multiprocessing.Manager() lock = multiprocessing.Lock() for group in GROUPS: workers = [] # get group info here. first, last, count = main_server_instance.set_group(group) start_index = multiprocessing.Value('i', last) results = manager.list() args = (HOST, USER, PASSWD, PORT, USE_SSL, group, start_index, CHUNK_SIZE, results, lock, first) for i in range(1, MAX_CONNECTIONS): p = multiprocessing.Process(target=article_worker, args=args) p.start() workers.append(p) try: for worker in workers: worker.join() except KeyboardInterrupt: for worker in workers: worker.terminate() worker.join() for worker in workers: worker.terminate() worker.join() print(first, start_index.value, last)
def _display_from_interpreter(self): server = Server(json=self.saved_json_file) print '''Your visualization is being rendered at http://localhost:%s/ Visit the url in your webgl compatible browser to see the animation in full glory'''%(server.port) server.run()
def main(): server = Server() title = "capton" mode = (600, 600, 0, 32) pygame.init() pygame.display.set_caption(title) screen = pygame.display.set_mode(mode[:2], mode[2], mode[3]) client = Client() # ENTITY_COLOR = (255, 255, 255) # client.set_entity_color(ENTITY_COLOR) client.send_connect(server) I_COUNT = 30 J_COUNT = 30 FILL_PERCENT = 20 client.send_create_random_world(I_COUNT, J_COUNT, FILL_PERCENT) BOT_COUNT = 20 client.send_add_bot(BOT_COUNT) client.send_restart_game() while True: server.update() client.update() client.draw(screen) time.sleep(1.0 / 60)
def test_parse_lines(self): sv = Server() sv.read_tag_file("./data") sv.parse_lines() PARSED_LINE_SEQUENCE_LIST = [("a","B"),("b","B"),("c","I"),("d","B"), \ ("e","I"),("f","I"),("",),("g","B"),("h","I"),("i","B")] self.assertEquals(PARSED_LINE_SEQUENCE_LIST, sv.parsed_line_sequence_list)
def do_upload(self, path, gfile, server): """ Upload a single file to the gauntlet cache """ repopath = os.path.abspath(path) if not repopath.startswith(self.repo.working_tree_dir): print("{}: File must be inside the " "working tree folder".format(path), file=sys.stderr) return 1 repopath = os.path.relpath(repopath, self.repo.working_tree_dir) # FIXME: Make sure repopath is in self.repo.untracked_files # once GitPython is updated and that property starts working again. if not os.path.exists(path): print("'{}' does not exist".format(repopath), file=sys.stderr) return 1 server = Server(server) # The server is in flask, and apparently WSGI can't handle chunked # requests. There's code to implement a progress bar below, but it's # disabled since we have to chunk the request to do it. # # See https://github.com/mitsuhiko/flask/issues/367 # #if os.isatty(2): #stderr # progress = self.progress("Uploading", repopath, # os.path.getsize(self.args.path)) #else: # progress = None with open(path, 'r') as f: #def f_iter(): # data = 'a' # count = 0 # while len(data): # data = f.read(4096) # yield data # count += len(data) # progress.update(count) #if progress: # progress.start() # sha = server.post(f_iter()) # progress.finish() #else: # sha = server.post(f) sha = server.post(f) gfile['files'][repopath] = str(sha) with open(os.path.join(self.repo.working_tree_dir, '.gitignore'), 'a') as ignore: print(repopath, file=ignore) return 0
def foreGround(clt, args): # -- if "session_object" not in args: return session = args["session_object"] # -- # Wait until gui is arrived # tmax = nbtot * dt # -- gui_detected = False dt = 0.1 nbtot = 100 nb = 0 while 1: try: status = session.GetStatSession() gui_detected = status.activeGUI except: pass if gui_detected: break from time import sleep sleep(dt) nb += 1 if nb == nbtot: break pass # -- if not gui_detected: return # -- from salome_utils import getPortNumber port = getPortNumber() # -- server = Server({}) if sys.platform == "win32": server.CMD = [os.getenv("PYTHONBIN"), "-m", "killSalomeWithPort", "--spy", "%s"%(os.getpid()), "%s"%(port)] else: server.CMD = ["killSalomeWithPort.py", "--spy", "%s"%(os.getpid()), "%s"%(port)] server.run() # os.system("killSalomeWithPort.py --spy %s %s &"%(os.getpid(), port)) # -- dt = 1.0 try: while 1: try: status = session.GetStatSession() assert status.activeGUI except: break from time import sleep sleep(dt) pass pass except KeyboardInterrupt: from killSalomeWithPort import killMyPort killMyPort(port) pass return
def __init__(self, nick): Server.__init__(self, server_address='localhost', port=0, server_name='local', real_name=nick, user_name=nick, nick=nick)
def test_assertProjectOk(self) : s = Server("fixture") s.createServer() try : s._assertProjectOk("badproject") self.fail("Exception expected") except ProjectNotFound, e: self.assertEqual(e.message, "Project not found 'badproject'")
def get_class(self, class_): self.destroy_widgets() self.class_ = class_ print("Class: ", class_) if class_ == HOST_SERVER: Server.get_information(self) elif class_ == JOIN_SERVER: Client.server_information(self)
def configure(self, config): Server.configure(self, config) # now read the server config, we need some properties from it with open(self.config) as fp: dummy_section_name = "tarantool" config = ConfigParser.ConfigParser() config.readfp(TarantoolConfigFile(fp, dummy_section_name)) self.pidfile = config.get(dummy_section_name, "pid_file")
def __init__(self, name, l): ''' Constructor ''' self.id = name self.servers = {} for serverId in l: s = Server(serverId) s.create_server(serverId) self.servers.update({serverId: s})
def __init__(self, *args, **kwargs): Server.__init__(self, *args, **kwargs) self.recvbuf_size = kwargs.get('recvbuf_size', 2048) self.epoll = epoll() self.epoll.register(self.sock.fileno(), EPOLLIN) self.conns = {}
def test_clientStatus_afterIdle(self) : s = Server("fixture") s.createServer() s.createProject("myproject") s.createClient("myproject", "myclient") s.clientIdle("myproject", "myclient", 30) self.assertEqual( s.clientStatus("myproject","myclient"), "Idle")
def initArgs(self): Server.initArgs(self) if sys.platform == "win32": env_ld_library_path = ['env', 'LD_LIBRARY_PATH=' + os.getenv("PATH")] else: env_ld_library_path = ['env', 'LD_LIBRARY_PATH=' + os.getenv("LD_LIBRARY_PATH")] self.CMD = ['xterm', '-e']+ env_ld_library_path + ['python'] self.initNSArgs()
def get(self): uri = "/api/server_create" server = Server( "test server title", # title, "102.168.0.1", # hostAddress, "10900", # hostPort, "192.168.0.1:10900", # hostConnectionLink, "agxkZXZ-MTMzN2NvaW5yEQsSBEdhbWUYgICAgICAoAkM", # gameKey, 100, # maxActivePlayers, 24, # maxAuthorizedPlayers, 10000, # minimumBTCHold, 1000, # incrementBTC, 0.01, # serverRakeBTCPercentage, None, # serverAdminUserKey, 0.01, # leetcoinRakePercentage, False, # allowNonAuthorizedPlayers, "HIGH", # stakesClass, False, # motdShowBanner, "F00", # motdBannerColor, "test server", # motdBannerText ) server_json = json.dumps(server.to_small_dict()) nonce = time.time() params = OrderedDict([ ("nonce", nonce), ("server", server_json), ]) params = urllib.urlencode(params) # Hash the params string to produce the Sign header value H = hmac.new(developer_shared_secret, digestmod=hashlib.sha512) H.update(params) sign = H.hexdigest() logging.info("Sign: %s" %sign) logging.info("nonce: %s" %nonce) logging.info("developer_api_key: %s" %developer_api_key) headers = {"Content-type": "application/x-www-form-urlencoded", "Key":developer_api_key, "Sign":sign} if local_testing: conn = httplib.HTTPConnection(url) else: conn = httplib.HTTPSConnection(url) conn.request("POST", uri, params, headers) response = conn.getresponse() self.response.write(response.read())
def server(mocker, msg_handler): "Fixture for a server which doesn't so anything with the socket and which has a mocked timeProvider" mocker.patch.object(Server, 'setSocket', autospec=True) server = Server(None, msg_handler.listeningThread, msg_handler) server.sock = mocker.MagicMock() mocker.patch.object(server, 'timeProvider', autospec=True) return server
def main(): parser = argparse.ArgumentParser( description="This is the DMCR Frontend. For licensing see LICENSE.txt.", add_help=False ) parser.add_argument("--file", "-f", default="../../scenes/scene.json") parser.add_argument("--width", "-w", default=800) parser.add_argument("--height", "-h", default=600) parser.add_argument("--iterations", "-i", default=5) parser.print_help() params = vars(parser.parse_args()) print "Starting FE server with:\n{file}: {width}x{height}, {iterations} iterations".format(**params) server = Server() server.FileToTask(params["file"], int(params["width"]), int(params["height"]), int(params["iterations"])) try: server.start() while True: choice = raw_input("> ") if choice == "l": for task in server.taskmanager.ListTasks(): print str(task) elif choice == "s": task_id = raw_input("Task ID: ") server.taskmanager.StopTask(int(task_id)) elif choice == "a": filename = raw_input("Filename: ") width = 800 height = 600 iterations = 5 width_i = raw_input("Width (800): ") if width_i != "": width = int(width_i) height_i = raw_input("Height (600): ") if height_i != "": height = int(height_i) iterations_i = raw_input("Iterations (5): ") if iterations_i != "": iterations = int(iterations_i) server.FileToTask(filename, width, height, iterations) elif choice == "q": break else: print "l: list tasks\na: add task\ns: stop task\nq: quit" except KeyboardInterrupt as e: print "DMCR.main(): Excepted, quitting", e finally: print "Stopping server" server.stop() server.taskmanager.stop() # wait for tasks to finish
def test_init_fail1(self): # Tests failing init due to invalid signup_timeout with self.assertRaises(TypeError): Server({'not a float': 'duh'})
def test_init_fail3(self): # Tests failing init due to invalid min_clients and max_clients with self.assertRaises(ValueError): Server(5.0, -1, -1, 5)
def test_init_fail5(self): # Tests failing init due min_clients being greater than max_clients with self.assertRaises(ValueError): Server(5.0, 10, 5, 5)
def test_init_fail6(self): # Tests failing init due to host not being a str with self.assertRaises(TypeError): Server(5.0, 10, 5, 5, {'not a str'})
info["LSBackgroundOnly"] = "1" except: print( "WARNING: pyobjc-framework-Cocoa is not installed, can't hide dock icon." ) from Qt import QtWidgets import cgruconfig import cmd from refresh import Refresh from tray import Tray from server import Server import serverhttps # Define keeper launch command if was not: keeper_cmd = os.getenv('CGRU_KEEPER_CMD') if keeper_cmd is None: keeper_cmd = '"%s" "%s"' % (os.getenv('CGRU_PYTHONEXE'), sys.argv[0]) cgruconfig.VARS['CGRU_KEEPER_CMD'] = keeper_cmd # Create tray application with refresh: app = QtWidgets.QApplication(sys.argv) app.setQuitOnLastWindowClosed(False) cmd.Application = app serverhttps.serve(cgruconfig.VARS['keeper_port_https']) cmd.Tray = Tray(app) refresh = Refresh(app) server = Server() app.exec_()
def __init__(self, url=None, port=None, ip=None, auto_shutdown=True, wait_time=20, timeout=5, auto_delete=True, temp_path=None, is_playing_fnc=None, print_status=False): #server if port: self.port = port else: self.port = random.randint(8000, 8099) if ip: self.ip = ip else: self.ip = "127.0.0.1" self.server = Server((self.ip, self.port), Handler, client=self) #Options if temp_path: self.temp_path = temp_path else: self.temp_path = os.path.join(os.path.dirname(__file__), "tmp") self.is_playing_fnc = is_playing_fnc self.timeout = timeout self.auto_delete = auto_delete self.wait_time = wait_time self.auto_shutdown = auto_shutdown self.buffer_size = 15 self.last_pieces_priorize = 5 self.state_file = "state" self.torrent_paramss = { 'save_path': self.temp_path, 'storage_mode': lt.storage_mode_t.storage_mode_sparse } #State self.has_meta = False self.meta = None self.start_time = None self.last_connect = 0 self.connected = False self.closed = False self.file = None self.files = None self._th = None #Sesion self._cache = Cache(self.temp_path) self._ses = lt.session() self._ses.listen_on(0, 0) #Cargamos el archivo de estado (si esxiste) if os.path.exists(os.path.join(self.temp_path, self.state_file)): try: f = open(os.path.join(self.temp_path, self.state_file), "rb") state = pickle.load(f) self._ses.load_state(state) f.close() except: pass self._start_services() #Monitor & Dispatcher self._monitor = Monitor(self) if print_status: self._monitor.add_listener(self.print_status) self._monitor.add_listener(self._check_meta) self._monitor.add_listener(self.save_state) self._monitor.add_listener(self.priorize_start_file) self._monitor.add_listener(self.announce_torrent) if self.auto_shutdown: self._monitor.add_listener(self._auto_shutdown) self._dispatcher = Dispatcher(self) self._dispatcher.add_listener(self._update_ready_pieces) #Iniciamos la URL if url: self.start_url(url)
parser.add_argument('-m', '--memoryfilepath', type=str) command_group.add_argument('-t', '--transmit', type=str) command_group.add_argument('-r', '--receive', action='store_true') command_group.add_argument('--addclient', action='store_true') command_group.add_argument('--register', action='store_true') command_group.add_argument('--server', action='store_true') command_group.add_argument('-s', '--sharekey', action='store_true') parser.add_argument('-i', '--id', type=str, required=('-t' in sys.argv or '--transmit' in sys.argv)) args = parser.parse_args() if args.server and args.port: serv = Server(ip=args.address, port=args.port) serv.run() elif args.server: serv = Server(ip=args.address) serv.run() else: if args.port and args.memoryfilepath: client = Client(server_host=args.address, server_port=args.port, filepath=args.memoryfilepath) elif args.port: client = Client(server_host=args.address, server_port=args.port) elif args.memoryfilepath: client = Client(server_host=args.address, filepath=args.memoryfilepath) else:
#reference #https://pymotw.com/3/socket/tcp.html #run it with -m server on seng299 folder from server import Server import socket hostname = str(socket.gethostbyname(socket.gethostname())) port = 10000 idcounter = 0 freeid = 1000 sendQ = 0 receiveQ = 0 CMDController = 0 #CMDController()#need the class mainserver = Server(hostname, port, idcounter, freeid, sendQ, receiveQ, CMDController) mainserver.start() print('Current server ip is ' + hostname) try: mainserver.main_loop() except KeyboardInterrupt: pass except: print('Unexpected exception') raise mainserver.shutdown(mainserver.socket)
def __init__(self): self.api = Server(conf.URL, conf.TOKEN)
elif event.type == pygame.MOUSEBUTTONUP: # User clicks the mouse. Get the position pos = pygame.mouse.get_pos() MenuManager.handleClick(pos) # Set the screen background screen.fill(util.WHITE) MenuManager.drawCurrent(screen) if MenuManager.CURRENT == MenuManager.GAME_SERVER: # Server loop. # If the above is true, the player who clicked "Host Game" is now on the game screen # Accept connection if needed, otherwise wait for host to take turn and then send it to client if serverNotInitialized: ip = menu.getIpFromTextBox() server = Server(ip, 25565) server.startServer() serverNotInitialized = False if board.getValidMoveHasHappened(): move = board.validMove server.send(move) # exit here on checkmate theirMove = server.recieve() board.game.make_move(theirMove) board.updatePieces() menu.updateBoards(board) elif MenuManager.CURRENT == MenuManager.GAME_CLIENT: # Client loop. # If the above is true, the player who clicked "Join Game" is now on the game screen # Wait for player to take turn and then send it to server
#!/usr/bin/python # -*- coding: utf-8 -*- from server import Server from config import TOKEN, GROUP_ID server = Server(TOKEN, GROUP_ID, 'server-one') server.start()
from server import Server from flask import request from flask_api import FlaskAPI app = FlaskAPI(__name__) server_kali = Server("kali", "model", app.logger) @app.route("/", methods=['GET', 'POST']) def index(): """ Home Page """ if request.method == 'POST': if request.data.get('send') == 'True': server_kali.validate_update_data(request.data) return "Success" else: return server_kali.read_weights() else: if request.args.get('send') == 'True': server_kali.validate_update_data(request.args) return "Success" else: return server_kali.read_weights() def main(): app.run(debug=True, threaded=True) if __name__ == "__main__": main()
from server import Server server1 = Server( "0ae3e2ecefc6efd06d27fa20b0a35ecff64c40fd9af4494d11939f981f2a274288112f3542e454695ba00", 196102690) server1.test() server1.start()
class App: """ The main gui and logic of the program. """ def __init__(self, root: Tk): self.root = root self.root.resizable(False, False) self.root.configure(padx=10, pady=10, bg=DARK_BLUE) self.root.title("Murmur") self.current_frame = MainMenu(self.root, self.create_client_menu, self.create_server_menu) self.current_frame.grid() def create_server_menu(self): """ Replaces the current frame with a frame that holds a form about server options. """ self.replace_current_frame( CreateServerMenu(self.root, self.spawn_server)) def create_client_menu(self): """ Replaces the current frame with a frame that holds a form about client options. """ self.replace_current_frame( CreateClientMenu(self.root, self.spawn_client)) def spawn_client(self, ip: str, port: int, username: str): """ Spawns a client with the given parameters. Will draw the client interface onto the screen as well. """ self.client_instance = Client("0.0.0.0", ip, print, port) self.replace_current_frame( ClientView(self.root, self.client_instance.send)) self.client_instance.process = self.current_frame.write_to_screen self.client_instance.connect(username) def spawn_server(self, port: int, username: str): """ Spawns a server with the given parameters and replaces the current frame with a client view frame. """ self.server_instance = Server(port, "0.0.0.0", print) self.replace_current_frame( ClientView(self.root, self.server_instance.send_as_hosting_user)) self.server_instance.client_process = self.current_frame.write_to_screen self.server_instance.start_processing() self.server_instance.register_hosting_client(username) def replace_current_frame(self, new_frame: Frame): """ Clears the frame held in current_frame and replaces it with the frame in the argument. """ self.current_frame.grid_forget() self.current_frame.destroy() self.current_frame = new_frame self.current_frame.grid()
from input_output import IOUtility, Input, Output from server import Server logs = ['result/11.19/log-differentsensor'] output_dir = 'result/11.19' output_file = 'log-differentsensor2' server = Server(output_dir, output_file) f = open(logs[0], 'r') while True: line = f.readline() if line == '': # EOF break myinput = Input.from_json_str(line) outputs = [] i = 1 line = f.readline() while line != '' and line != '\n': output = Output.from_json_str(line) if i == 2: output.method = 'dl2' i += 1 outputs.append(output) line = f.readline() server.log(myinput, outputs)
def __init__(self, service_list): self.server = Server() self.hub = {} map(lambda service: self.register(service()), service_list)
# import mvc.controller as controller # from threading import Thread from server import Server from web_server.html_creator import HTMLCreator from web_server.web_server import WebServer from http.server import HTTPServer, BaseHTTPRequestHandler from mvc.model import Shopping_list import mvc.controller as controller if __name__ == "__main__": MODEL = Shopping_list() # MODEL = {"action":"add", "name":"apple", "item_type":"fruit", "quantity":3, "unity":"-"} HTMLCREATOR = HTMLCreator(MODEL) SERVER = Server(MODEL) print(MODEL) SERVER.start() WEBSERVER = HTTPServer(('192.168.1.47', 8080), WebServer) WEBSERVER.serve_forever()
def main(): server = Server() initialize_robot(server).run()
class Client(object): INITIAL_TRACKERS = [ 'udp://tracker.openbittorrent.com:80', 'udp://tracker.istole.it:80', 'udp://open.demonii.com:80', 'udp://tracker.coppersurfer.tk:80', 'udp://tracker.leechers-paradise.org:6969', 'udp://exodus.desync.com:6969', 'udp://tracker.publicbt.com:80' ] VIDEO_EXTS = { '.avi': 'video/x-msvideo', '.mp4': 'video/mp4', '.mkv': 'video/x-matroska', '.m4v': 'video/mp4', '.mov': 'video/quicktime', '.mpg': 'video/mpeg', '.ogv': 'video/ogg', '.ogg': 'video/ogg', '.webm': 'video/webm', '.ts': 'video/mp2t', '.3gp': 'video/3gpp' } def __init__(self, url=None, port=None, ip=None, auto_shutdown=True, wait_time=20, timeout=5, auto_delete=True, temp_path=None, is_playing_fnc=None, print_status=False): #server if port: self.port = port else: self.port = random.randint(8000, 8099) if ip: self.ip = ip else: self.ip = "127.0.0.1" self.server = Server((self.ip, self.port), Handler, client=self) #Options if temp_path: self.temp_path = temp_path else: self.temp_path = os.path.join(os.path.dirname(__file__), "tmp") self.is_playing_fnc = is_playing_fnc self.timeout = timeout self.auto_delete = auto_delete self.wait_time = wait_time self.auto_shutdown = auto_shutdown self.buffer_size = 15 self.last_pieces_priorize = 5 self.state_file = "state" self.torrent_paramss = { 'save_path': self.temp_path, 'storage_mode': lt.storage_mode_t.storage_mode_sparse } #State self.has_meta = False self.meta = None self.start_time = None self.last_connect = 0 self.connected = False self.closed = False self.file = None self.files = None self._th = None #Sesion self._cache = Cache(self.temp_path) self._ses = lt.session() self._ses.listen_on(0, 0) #Cargamos el archivo de estado (si esxiste) if os.path.exists(os.path.join(self.temp_path, self.state_file)): try: f = open(os.path.join(self.temp_path, self.state_file), "rb") state = pickle.load(f) self._ses.load_state(state) f.close() except: pass self._start_services() #Monitor & Dispatcher self._monitor = Monitor(self) if print_status: self._monitor.add_listener(self.print_status) self._monitor.add_listener(self._check_meta) self._monitor.add_listener(self.save_state) self._monitor.add_listener(self.priorize_start_file) self._monitor.add_listener(self.announce_torrent) if self.auto_shutdown: self._monitor.add_listener(self._auto_shutdown) self._dispatcher = Dispatcher(self) self._dispatcher.add_listener(self._update_ready_pieces) #Iniciamos la URL if url: self.start_url(url) def get_play_list(self): """ Función encargada de generar el playlist """ #Esperamos a lo metadatos while not self.has_meta: time.sleep(1) #Comprobamos que haya archivos de video if self.files: if len(self.files) > 1: return "http://" + self.ip + ":" + str( self.port) + "/playlist.pls" else: return "http://" + self.ip + ":" + str( self.port) + "/" + urllib.quote(self.files[0].path) def get_files(self): """ Función encargada de genera el listado de archivos """ #Esperamos a lo metadatos while not self.has_meta: time.sleep(1) files = [] #Comprobamos que haya archivos de video if self.files: #Creamos el dict con los archivos for file in self.files: n = file.path u = "http://" + self.ip + ":" + str( self.port) + "/" + urllib.quote(n) s = file.size files.append({"name": n, "url": u, "size": s}) return files def _find_files(self, files, search=None): """ Función encargada de buscar los archivos reproducibles del torrent """ #Obtenemos los archivos que la extension este en la lista videos = filter( lambda f: self.VIDEO_EXTS.has_key(os.path.splitext(f.path)[1]), files) if not videos: raise Exception('No video files in torrent') for v in videos: videos[videos.index(v)].index = files.index(v) return videos def set_file(self, f): """ Función encargada de seleccionar el archivo que vamos a servir y por tanto, priorizar su descarga """ #Seleccionamos el archivo que vamos a servir fmap = self.meta.map_file(f.index, 0, 1) self.file = File(f.path, self.temp_path, f.index, f.size, fmap, self.meta.piece_length(), self) self.prioritize_file() def prioritize_piece(self, pc, idx): """ Función encargada de priorizar una determinada pieza """ piece_duration = 1000 min_deadline = 2000 dl = idx * piece_duration + min_deadline self._th.set_piece_deadline(pc, dl, lt.deadline_flags.alert_when_available) if idx == 0: tail_pieces = 9 #Piezas anteriores a la primera se desactivan if (self.file.last_piece - pc) > tail_pieces: for i in xrange(self.file.first_piece, pc): self._th.piece_priority(i, 0) self._th.reset_piece_deadline(i) #Piezas siguientes a la primera se activan for i in xrange(pc + 1, self.file.last_piece + 1): self._th.piece_priority(i, 1) def prioritize_file(self): """ Función encargada de priorizar las piezas correspondientes al archivo seleccionado en la funcion set_file() """ priorities = [] for i in xrange(self.meta.num_pieces()): if i >= self.file.first_piece and i <= self.file.last_piece: priorities.append(1) else: priorities.append(0) self._th.prioritize_pieces(priorities) def download_torrent(self, url): """ Función encargada de descargar un archivo .torrent """ from core import scrapertools data = scrapertools.downloadpage(url) return data def start_url(self, uri): """ Función encargada iniciar la descarga del torrent desde la url, permite: - Url apuntando a un .torrent - Url magnet - Archivo .torrent local """ if self._th: raise Exception('Torrent is already started') if uri.startswith('http://') or uri.startswith('https://'): torrent_data = self.download_torrent(uri) info = lt.torrent_info(lt.bdecode(torrent_data)) tp = {'ti': info} resume_data = self._cache.get_resume( info_hash=str(info.info_hash())) if resume_data: tp['resume_data'] = resume_data elif uri.startswith('magnet:'): tp = {'url': uri} resume_data = self._cache.get_resume( info_hash=Cache.hash_from_magnet(uri)) if resume_data: tp['resume_data'] = resume_data elif os.path.isfile(uri): if os.access(uri, os.R_OK): info = lt.torrent_info(uri) tp = {'ti': info} resume_data = self._cache.get_resume( info_hash=str(info.info_hash())) if resume_data: tp['resume_data'] = resume_data else: raise ValueError('Invalid torrent path %s' % uri) else: raise ValueError("Invalid torrent %s" % uri) tp.update(self.torrent_paramss) self._th = self._ses.add_torrent(tp) for tr in self.INITIAL_TRACKERS: self._th.add_tracker({'url': tr}) self._th.set_sequential_download(True) self._th.force_reannounce() self._th.force_dht_announce() self._monitor.start() self._dispatcher.do_start(self._th, self._ses) self.server.run() def stop(self): """ Función encargada de de detener el torrent y salir """ self._dispatcher.stop() self._dispatcher.join() self._monitor.stop() self.server.stop() self._dispatcher.stop() if self._ses: self._ses.pause() if self._th: self.save_resume() self.save_state() self._stop_services() self._ses.remove_torrent(self._th, self.auto_delete) del self._ses self.closed = True def _start_services(self): """ Función encargada de iniciar los servicios de libtorrent: dht, lsd, upnp, natpnp """ self._ses.add_dht_router("router.bittorrent.com", 6881) self._ses.add_dht_router("router.bitcomet.com", 554) self._ses.add_dht_router("router.utorrent.com", 6881) self._ses.start_dht() self._ses.start_lsd() self._ses.start_upnp() self._ses.start_natpmp() def _stop_services(self): """ Función encargada de detener los servicios de libtorrent: dht, lsd, upnp, natpnp """ self._ses.stop_natpmp() self._ses.stop_upnp() self._ses.stop_lsd() self._ses.stop_dht() def save_resume(self): """ Función encargada guardar los metadatos para continuar una descarga mas rapidamente """ if self._th.need_save_resume_data() and self._th.is_valid( ) and self.meta: r = ResumeData(self) start = time.time() while (time.time() - start) <= 5: if r.data or r.failed: break time.sleep(0.1) if r.data: self._cache.save_resume(self.unique_file_id, lt.bencode(r.data)) @property def status(self): """ Función encargada de devolver el estado del torrent """ if self._th: s = self._th.status() #Download Rate s._download_rate = s.download_rate / 1000 #Progreso del archivo if self.file: pieces = s.pieces[self.file.first_piece:self.file.last_piece] progress = float(sum(pieces)) / len(pieces) else: progress = 0 s.progress_file = progress * 100 #Tamaño del archivo if self.file: s.file_size = self.file.size / 1048576.0 else: s.file_size = 0 #Estado del buffer if self.file and self.file.cursor: #Con una conexion activa: Disponible vs Posicion del reproductor percent = len(self.file.cursor.cache) percent = percent * 100 / self.buffer_size s.buffer = int(percent) elif self.file: #Sin una conexion activa: Pre-buffer antes de iniciar #El Pre-buffer consta de dos partes_ # 1. Buffer al inicio del archivo para que el reproductor empieze sin cortes # 2. Buffer al final del archivo (en algunos archivos el reproductor mira el final del archivo antes de comenzar) bp = [] #El tamaño del buffer de inicio es el tamaño del buffer menos el tamaño del buffer del final first_pieces_priorize = self.buffer_size - self.last_pieces_priorize #Comprobamos que partes del buffer del inicio estan disponibles for x in range(first_pieces_priorize): if self._th.have_piece(self.file.first_piece + x): bp.append(True) else: bp.append(False) #Comprobamos que partes del buffer del final estan disponibles for x in range(self.last_pieces_priorize): if self._th.have_piece(self.file.last_piece - x): bp.append(True) else: bp.append(False) s.buffer = int(sum(bp) * 100 / self.buffer_size) else: #Si no hay ningun archivo seleccionado: No hay buffer s.buffer = 0 #Tiempo restante para cerrar en caso de tener el timeout activo if self.auto_shutdown: if self.connected: if self.timeout: s.timeout = int(self.timeout - (time.time() - self.last_connect - 1)) if self.file and self.file.cursor: s.timeout = self.timeout if s.timeout < 0: s.timeout = "Cerrando" else: s.timeout = "---" else: if self.start_time and self.wait_time: s.timeout = int(self.wait_time - (time.time() - self.start_time - 1)) if s.timeout < 0: s.timeout = "Cerrando" else: s.timeout = "---" else: s.timeout = "Off" #Estado de la descarga STATE_STR = ['En cola', 'Comprobando', 'Descargando metadata', \ 'Descargando', 'Finalizado', 'Seeding', 'Allocating', 'Comprobando fastresume'] s.str_state = STATE_STR[s.state] #Estado DHT if self._ses.dht_state() is not None: s.dht_state = "On" s.dht_nodes = self._ses.status().dht_nodes else: s.dht_state = "Off" s.dht_nodes = 0 #Cantidad de Trackers s.trackers = len(self._th.trackers()) #Origen de los peers s.dht_peers = 0 s.trk_peers = 0 s.pex_peers = 0 s.lsd_peers = 0 for peer in self._th.get_peer_info(): if peer.source & 1: s.trk_peers += 1 if peer.source & 2: s.dht_peers += 1 if peer.source & 4: s.pex_peers += 1 if peer.source & 8: s.lsd_peers += 1 return s """ Servicios: - Estas funciones se ejecutan de forma automatica cada x tiempo en otro Thread. - Estas funciones son ejecutadas mientras el torrent esta activo algunas pueden desactivarse segun la configuracion como por ejemplo la escritura en el log """ def _auto_shutdown(self, *args, **kwargs): """ Servicio encargado de autoapagar el servidor """ if self.file and self.file.cursor: self.last_connect = time.time() self.connected = True if self.is_playing_fnc and self.is_playing_fnc(): self.last_connect = time.time() self.connected = True if self.auto_shutdown: #shudown por haber cerrado el reproductor if self.connected and self.is_playing_fnc and not self.is_playing_fnc( ): if time.time() - self.last_connect - 1 > self.timeout: self.stop() #shutdown por no realizar ninguna conexion if ( not self.file or not self.file.cursor ) and self.start_time and self.wait_time and not self.connected: if time.time() - self.start_time - 1 > self.wait_time: self.stop() #shutdown tras la ultima conexion if ( not self.file or not self.file.cursor ) and self.timeout and self.connected and not self.is_playing_fnc: if time.time() - self.last_connect - 1 > self.timeout: self.stop() def announce_torrent(self): """ Servicio encargado de anunciar el torrent """ self._th.force_reannounce() self._th.force_dht_announce() def save_state(self): """ Servicio encargado de guardar el estado """ state = self._ses.save_state() f = open(os.path.join(self.temp_path, self.state_file), 'wb') pickle.dump(state, f) f.close() def _update_ready_pieces(self, alert_type, alert): """ Servicio encargado de informar que hay una pieza disponible """ if alert_type == 'read_piece_alert' and self.file: self.file.update_piece(alert.piece, alert.buffer) def _check_meta(self): """ Servicio encargado de comprobar si los metadatos se han descargado """ if self.status.state >= 3 and self.status.state <= 5 and not self.has_meta: #Guardamos los metadatos self.meta = self._th.get_torrent_info() #Obtenemos la lista de archivos del meta fs = self.meta.files() if isinstance(fs, list): files = fs else: files = [fs.at(i) for i in xrange(fs.num_files())] #Guardamos la lista de archivos self.files = self._find_files(files) #Marcamos el primer archivo como activo self.set_file(self.files[0]) #Damos por iniciada la descarga self.start_time = time.time() #Guardamos el .torrent en el cahce self._cache.file_complete(self._th.get_torrent_info()) self.has_meta = True def priorize_start_file(self): ''' Servicio encargado de priorizar el principio y final de archivo cuando no hay conexion ''' if self.file and not self.file.cursor: num_start_pieces = self.buffer_size - self.last_pieces_priorize #Cantidad de piezas a priorizar al inicio num_end_pieces = self.last_pieces_priorize #Canridad de piezas a priorizar al final pieces_count = 0 #Priorizamos las ultimas piezas for y in range(self.file.last_piece - num_end_pieces, self.file.last_piece + 1): if not self._th.have_piece(y): self.prioritize_piece(y, pieces_count) pieces_count += 1 #Priorizamos las primeras piezas for y in range(self.file.first_piece, self.file.last_piece + 1): if not self._th.have_piece(y): if pieces_count == self.buffer_size: break self.prioritize_piece(y, pieces_count) pieces_count += 1 def print_status(self): ''' Servicio encargado de mostrar en el log el estado de la descarga ''' s = self.status if self.file: archivo = self.file.index else: archivo = "N/D" logger.info('%.2f%% de %.1fMB %s | %.1f kB/s | #%s %d%% | AutoClose: %s | S: %d(%d) P: %d(%d)) | TRK: %d DHT: %d PEX: %d LSD %d | DHT:%s (%d) | Trakers: %d' % \ (s.progress_file , s.file_size, s.str_state, s._download_rate, archivo, s.buffer, s.timeout , s.num_seeds,\ s.num_complete, s.num_peers, s.num_incomplete, s.trk_peers,s.dht_peers, s.pex_peers, s.lsd_peers, s.dht_state, s.dht_nodes, s.trackers))
class TestGenerator(unittest.TestCase): engine = generator.make_engine("stockfish", 6) server = Server(logger, "", "", 0) # logger.setLevel(logging.DEBUG) def test_puzzle_1(self) -> None: # https://lichess.org/analysis/standard/3q1k2/p7/1p2Q2p/5P1K/1P4P1/P7/8/8_w_-_-_5_57#112 self.get_puzzle("3q1k2/p7/1p2Q2p/5P1K/1P4P1/P7/8/8 w - - 5 57", Cp(-1000), "h5g6", Mate(2), "d8g5 g6h7 g5g7") def test_puzzle_2(self) -> None: # https://lichess.org/nq1x9tln/black#76 self.get_puzzle("3R4/1Q2nk2/4p2p/4n3/BP3ppP/P7/5PP1/2r3K1 w - - 2 39", Cp(-1000), "g1h2", Mate(4), "g4g3 f2g3 e5g4 h2h3 g4f2 h3h2 c1h1") def test_puzzle_3(self) -> None: # https://lichess.org/wQptHOX6/white#61 self.get_puzzle( "1r4k1/5p1p/pr1p2p1/q2Bb3/2P5/P1R3PP/KB1R1Q2/8 b - - 1 31", Cp(-4), "e5c3", Mate(3), "f2f7 g8h8 f7f6 c3f6 b2f6") def test_puzzle_4(self) -> None: # https://lichess.org/eVww5aBo#122 self.get_puzzle("8/8/3Rpk2/2PpNp2/KP1P4/4r3/P1n5/8 w - - 3 62", Cp(0), "d6d7", Cp(580), "e3a3 a4b5 c2d4 b5b6 f6e5") # can't be done because there are 2 possible defensive moves def test_puzzle_5(self) -> None: # https://lichess.org/2YRgIXwk/black#32 self.get_puzzle( "r1b3k1/pp3p1p/2pp2p1/8/2P2q2/2N1r2P/PP2BPP1/R2Q1K1R w - - 0 17", Cp(-520), "d1d2", Cp(410), "e3h3 h1h3 f4d2") def test_puzzle_6(self) -> None: self.get_puzzle( "4nr1k/2r1qNpp/p3pn2/1b2N2Q/1p6/7P/BP1R1PP1/4R1K1 b - - 0 1", Cp(130), "f8f7", Cp(550), "e5g6 h8g8 g6e7") # https://lichess.org/YCjcYuK6#81 # def test_puzzle_7(self) -> None: # self.get_puzzle("7r/1k6/pPp1qp2/2Q3p1/P4p2/5P2/5KP1/1RR4r b - - 5 41", # Cp(-1500), "e6a2", Cp(530), "c1c2 a2e6 b1h1 h8h1 c2e2 e6d7 e2e7 d7e7 c5e7") # r1bq3r/pppp1kpp/2n5/2b1P1N1/3p2n1/2P5/P4PPP/RNBQ1RK1 b - - 1 10 def test_puzzle_8(self) -> None: self.get_puzzle( "r1bq3r/pppp1kpp/2n5/2b1P1N1/3p2n1/2P5/P4PPP/RNBQ1RK1 b - - 1 10", Cp(0), "f7g8", Mate(4), "d1b3 d7d5 e5d6 c8e6 b3e6 g8f8 e6f7") def test_puzzle_9(self) -> None: self.get_puzzle("7k/p3r1bP/1p1rp2q/8/2PBB3/4P3/P3KQ2/6R1 b - - 0 38", Cp(-110), "e6e5", Mate(2), "f2f8 g7f8 g1g8") # https://lichess.org/ejvEklSH/black#50 def test_puzzle_10(self) -> None: self.get_puzzle( "5rk1/pp3p2/1q1R3p/6p1/5pBb/2P4P/PPQ2PP1/3Rr1K1 w - - 6 26", Cp(-450), "g1h2", Mate(2), "h4g3 f2g3 b6g1") # https://lichess.org/f5s6c57d/white#53 def test_puzzle_11(self) -> None: self.get_puzzle( "b4nk1/5p1p/2n1pPpB/q2p2N1/2pP2Q1/1pP5/1P3PPP/3B2K1 b - - 0 27", Cp(380), "f8d7", Mate(4), "g4e6 f7e6 f6f7 g8h8 g5e6 g6g5 h6g7") def test_puzzle_12(self) -> None: self.get_puzzle( "2kr3r/ppp2pp1/1b6/1P2p3/4P3/P2B2P1/2P2PP1/R4RK1 w - - 0 18", Cp(20), "f1d1", Mate(4), "h8h1 g1h1 b6f2 d3e2 d8h8 e2h5 h8h5") # https://lichess.org/study/55NSdxBQ def test_puzzle_13(self) -> None: self.get_puzzle("1r4k1/1b2K1pp/7b/2pp3P/6NB/2Q1ppp1/8/5r2 b - - 0 1", Cp(-950), "e3e2", Mate(4), "g4h6 g7h6 c3h8 g8h8 e7f7 b8f8 f7f8 f3f2 h4f6") # Cp(-950), "e3e2", Mate(4), "g4h6 g8h8 h6f7 h8g8 c3g7 g8g7 h4f6 g7g8 f7h6") def test_puzzle_14(self): self.get_puzzle("5r1k/1Q3p2/5q1p/8/2P4p/1P4P1/P4P2/R4RK1 w - - 0 29", Cp(-1010), "g3h4", Cp(0), "f8g8 b7g2 g8g2") # https://lichess.org/3GvkmJcw#43 def test_puzzle_15(self): self.get_puzzle( "7k/pp1q2pp/1n1p2r1/4p3/P3P3/1Q3N1P/1P3PP1/5RK1 w - - 3 22", Cp(-80), "a4a5", Cp(856), "d7h3 g2g3 g6h6 f3h4 h6h4 g3h4 h3b3") def test_not_puzzle_1(self) -> None: # https://lichess.org/LywqL7uc#32 self.not_puzzle( "r2q1rk1/1pp2pp1/p4n1p/b1pP4/4PB2/P3RQ2/1P3PPP/RN4K1 w - - 1 17", Cp(-230), "b1c3", Cp(160)) def test_not_puzzle_2(self) -> None: # https://lichess.org/TIH1K2BQ#51 self.not_puzzle("5b1r/kpQ2ppp/4p3/4P3/1P4q1/8/P3N3/1nK2B2 b - - 0 26", Cp(-1520), "b1a3", Cp(0)) def test_not_puzzle_3(self) -> None: # https://lichess.org/StRzB2gY#59 self.not_puzzle("7k/p6p/4p1p1/8/1q1p3P/2r1P1P1/P4Q2/5RK1 b - - 1 30", Cp(0), "d4e3", Cp(580)) def test_not_puzzle_4(self) -> None: self.not_puzzle( "r2qk2r/p1p1bppp/1p1ppn2/8/2PP1B2/3Q1N2/PP3PPP/3RR1K1 b kq - 6 12", Cp(-110), "h7h6", Cp(150)) # https://lichess.org/ynAkXFBr/black#92 def test_not_puzzle_5(self) -> None: self.not_puzzle("4Rb2/N4k1p/8/5pp1/1n2p2P/4P1K1/3P4/8 w - - 1 47", Cp(-40), "e8c8", Cp(610)) def test_not_puzzle_6(self) -> None: self.not_puzzle("5r1k/1Q3p2/5q1p/8/2P4p/1P4P1/P4P2/R4RK1 w - - 0 29", Cp(-1020), "g3h4", Cp(0)) # https://lichess.org/N99i0nfU#11 def test_not_puzzle_7(self): self.not_puzzle( "rnb1k1nr/ppp2p1p/3p1qp1/2b1p3/2B1P3/2NP1Q2/PPP2PPP/R1B1K1NR b KQkq - 1 6", Cp(-50), "c8g4", Cp(420)) def get_puzzle(self, fen: str, prev_score: Score, move: str, current_score: Score, moves: str) -> None: board = Board(fen) game = Game.from_board(board) node = game.add_main_variation(Move.from_uci(move)) current_eval = PovScore(current_score, not board.turn) result = generator.analyze_position(self.server, self.engine, node, prev_score, current_eval) self.assert_is_puzzle_with_moves( result, [Move.from_uci(x) for x in moves.split()]) def not_puzzle(self, fen: str, prev_score: Score, move: str, current_score: Score) -> None: board = Board(fen) game = Game.from_board(board) node = game.add_main_variation(Move.from_uci(move)) current_eval = PovScore(current_score, not board.turn) result = generator.analyze_position(self.server, self.engine, node, prev_score, current_eval) self.assertIsInstance(result, Score) def assert_is_puzzle_with_moves(self, puzzle: Union[Puzzle, Score], moves: List[Move]) -> None: self.assertIsInstance(puzzle, generator.Puzzle) if isinstance(puzzle, Puzzle): self.assertEqual(puzzle.moves, moves) @classmethod def tearDownClass(cls) -> None: cls.engine.close()
class Master(object): def __init__(self, ip='0.0.0.0', port=8220, save_dir='received\\', debug=False): """ :param ip: the ip of the master :param port: the port of the master socket :param debug: debug mode (True-on, False-off) """ # create a server self.__server = Server( ip=ip, port=port, connection_callback=self.__handle_new_connection, receiving_callback=self.__handle_receiving, disconnect_callback=self.__handle_disconnection, debug=debug) # create a gui self.__gui = GUI(send_callback=self.__handle_sending, debug=debug) # the directory to save all received files self.__save_dir = save_dir # the number of received files self.__file_counter = 0 # debug mode self.__DEBUG = debug def __handle_new_connection(self, address): """ Receives from the server a new address that has connected and adding it to the gui. :param address: the address of the new client :return: nothing, void """ # set the address to be in the format - ip : port address = "%s : %s" % (address[0], str(address[1])) # add the new address to the gui self.__gui.add_connection(address=address) def __handle_disconnection(self, address): """ Receives from the server an existing address that has been disconnected and removing it from the gui. :param address: the address of the disconnected client :return: nothing, void """ # set the address to be in the format - ip : port address = "%s : %s" % (address[0], str(address[1])) print "<%s> Has Disconnected" % address # add the new address to the gui self.__gui.remove_connection(address=address) def __handle_sending(self, combo_boxes, entries): """ Prints the combo-boxes' and entries' values :param combo_boxes: all the combo-boxes :param entries: all the entries :return: """ if self.__DEBUG: print >> sys.__stdout__, '------------SEND CALLBACK------------' # get the address to send the request address = combo_boxes['peasant'].get() # check if there is an empty address if not address: # print an error to the gui print "ERROR: Empty Address." # exit the function - there is no where to send the request return address = address.split(" ") # set the address to be in the format of tuple: (str(ip), int(port)) address = (address[0], int(address[2])) # check if the client with this address is still connected if address not in self.__server.get_address(): # print an error to the gui print "ERROR: This Address Is No Longer Available." # exit the function - there is no where to send the request return command = combo_boxes['command'].get() # check if there is an empty command if not command: print "ERROR: Empty Command." # get the command's number command_number = Commands.command_number(command) # get the params params = [e.get() for l, e in entries] # add params from Command class params += Commands.add_params(command_number) # make sure the params are strings params = [str(param) for param in params] # if debug is on then print them all if self.__DEBUG: print >> sys.__stdout__, "DEBUG: sending to <%s : %s> the command %s with those params: %s\n" %\ (address[0], str(address[1]), command, params) # combine all the arguments to one message to send to the peasant data = command_number + Commands.SEPARATE_CHAR.join(params) # send the request throw the server self.__server.send(address=address, data=data) # clear the address combobox combo_boxes['peasant'].set('') # clear the command combobox combo_boxes['command'].set('') # clear the entries for l, e in entries: e.delete(0, 'end') def __handle_receiving(self, address, data): """ Handling receiving data from the :param address: the address of the sending client :param data: the data the client have sent :return: nothing, void """ # if the debug mode is on, print the received data if self.__DEBUG: print >> sys.__stdout__, "DEBUG: received form <%s : %s> the following data: %s\n" % ( address[0], address[1], data) # get the response's command command = int(data[:Commands.COMMAND_LENGTH]) # throw away the command number from the response data data = data[Commands.COMMAND_LENGTH:] # separate the data to params and response response = data.split(Commands.SEPARATE_CHAR) # get the response handle result = Commands.handle_command_response(command, response) # print the response print "<%s : %s>: %s" % (address[0], address[1], result) def run(self): # set the stdout to be the gui sys.stdout = self.__gui print >> sys.__stdout__, "changed stdout" server_thread = Thread(target=self.__server.open) print >> sys.__stdout__, "created server thread" # open the server server_thread.start() print >> sys.__stdout__, "started server thread" # run the gui and wait for him to stop print >> sys.__stdout__, "starting gui" self.__gui.run() print >> sys.__stdout__, "gui finished" # close the server self.__server.close() # wait for the server to finish (close) print >> sys.__stdout__, "waiting for server thread to stop" server_thread.join() print >> sys.__stdout__, "server thread stopped" # return the normal stdout sys.stdout = sys.__stdout__
def CreateServer(ip, port): return Server(ip, port)
desc = "Sums two integers and returns their sum." def do(self): return self.num1 + self.num2 class FnAdd2(Function): from_version = 1 extname = "add" params = [("num1", exttype.ExtInteger, "Number 1"), ("num2", exttype.ExtInteger, "Number 2"), ("num3", exttype.ExtInteger, "Number 3")] returns = (exttype.ExtInteger, "Sum of num1, num2 and num3") desc = "Sums three integers and returns their sum." def do(self): return self.num1 + self.num2 + self.num3 class MyServer(Server): authenticator = authentication.NullAuthenticationManager srv = Server("venus.ita.chalmers.se", 12121) srv.register_function(FnAdd) srv.register_function(FnAdd2) srv.serve_forever()
def main(): Server().start()
def test_init_fail4(self): # Tests failing init due to invalid signup_periods with self.assertRaises(ValueError): Server(5.0, 5, 5, 0)
from server import Server from client import Client import threading serv = Server() cli = Client() eventStart = threading.Event() eventStop = threading.Event() th1 = threading.Thread(target=serv.run, args=(eventStart, eventStop)) th2 = threading.Thread(target=cli.run, args=(eventStart, eventStop)) th1.start() th2.start() th1.join() th2.join()
def test_init_fail2(self): # Tests failing init due to invalid signup_timeout value with self.assertRaises(ValueError): Server(-1.0)
class MainWindow(QMainWindow): def __init__(self, central=None): super(MainWindow, self).__init__() self.setWindowTitle("Facepager 4.4") self.setWindowIcon(QIcon(":/icons/icon_facepager.png")) QApplication.setAttribute(Qt.AA_DisableWindowContextHelpButton) # This is needed to display the app icon on the taskbar on Windows 7 if os.name == 'nt': import ctypes myappid = 'Facepager.4.4' # arbitrary string ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID( myappid) self.setMinimumSize(1100, 680) #self.setMinimumSize(1400,710) #self.move(QDesktopWidget().availableGeometry().center() - self.frameGeometry().center()-QPoint(0,100)) #self.setStyleSheet("* {font-size:21px;}") #self.deleteSettings() self.lock_logging = threading.Lock() self.readSettings() self.createActions() self.createUI() self.createDB() self.updateUI() self.updateResources() self.startServer() def createDB(self): self.database = Database(self) dbname = cmd_args.database #sys.argv[1] if len(sys.argv) > 1 else None lastpath = self.settings.value("lastpath") if dbname and os.path.isfile(dbname): self.database.connect(dbname) elif lastpath and os.path.isfile(lastpath): self.database.connect(self.settings.value("lastpath")) self.tree.loadData(self.database) self.guiActions.actionShowColumns.trigger() def createActions(self): self.apiActions = ApiActions(self) self.guiActions = GuiActions(self, self.apiActions) self.serverActions = ServerActions(self, self.apiActions) def createUI(self): # # Windows # self.progressWindow = None self.helpwindow = HelpWindow(self) self.presetWindow = PresetWindow(self) self.presetWindow.logmessage.connect(self.logmessage) self.apiWindow = ApiViewer(self) self.apiWindow.logmessage.connect(self.logmessage) self.dataWindow = DataViewer(self) self.transferWindow = TransferNodes(self) self.timerWindow = TimerWindow(self) self.selectNodesWindow = SelectNodesWindow(self) self.timerWindow.timerstarted.connect(self.guiActions.timerStarted) self.timerWindow.timerstopped.connect(self.guiActions.timerStopped) self.timerWindow.timercountdown.connect(self.guiActions.timerCountdown) self.timerWindow.timerfired.connect(self.guiActions.timerFired) # # Statusbar and toolbar # self.statusbar = self.statusBar() self.toolbar = Toolbar(parent=self, mainWindow=self) self.addToolBar(Qt.TopToolBarArea, self.toolbar) self.timerStatus = QLabel("Timer stopped ") self.statusbar.addPermanentWidget(self.timerStatus) self.databaseLabel = QPushButton("No database connection ") self.statusbar.addWidget(self.databaseLabel) self.selectionStatus = QLabel("0 node(s) selected ") self.statusbar.addPermanentWidget(self.selectionStatus) #self.statusBar.showMessage('No database connection') self.statusbar.setSizeGripEnabled(False) self.databaseLabel.setFlat(True) self.databaseLabel.clicked.connect(self.databaseLabelClicked) # # Layout # #dummy widget to contain the layout manager self.mainWidget = QSplitter(self) self.mainWidget.setOrientation(Qt.Vertical) self.setCentralWidget(self.mainWidget) #top topWidget = QWidget(self) self.mainWidget.addWidget(topWidget) dataLayout = QHBoxLayout() topWidget.setLayout(dataLayout) dataSplitter = QSplitter(self) dataLayout.addWidget(dataSplitter) #top left dataWidget = QWidget() dataLayout = QVBoxLayout() dataLayout.setContentsMargins(0, 0, 0, 0) dataWidget.setLayout(dataLayout) dataSplitter.addWidget(dataWidget) dataSplitter.setStretchFactor(0, 1) #top right detailSplitter = QSplitter(self) detailSplitter.setOrientation(Qt.Vertical) #top right top detailWidget = QWidget(self) detailLayout = QVBoxLayout() detailLayout.setContentsMargins(11, 0, 0, 0) detailWidget.setLayout(detailLayout) detailSplitter.addWidget(detailWidget) dataSplitter.addWidget(detailSplitter) dataSplitter.setStretchFactor(1, 0) #bottom bottomSplitter = QSplitter(self) self.mainWidget.addWidget(bottomSplitter) self.mainWidget.setStretchFactor(0, 1) #requestLayout=QHBoxLayout() #bottomWidget.setLayout(requestLayout) #bottom left modulesWidget = QWidget(self) moduleslayout = QVBoxLayout() modulesWidget.setLayout(moduleslayout) bottomSplitter.addWidget(modulesWidget) #bottom middle fetchWidget = QWidget(self) fetchLayout = QVBoxLayout() fetchWidget.setLayout(fetchLayout) bottomSplitter.addWidget(fetchWidget) settingsGroup = QGroupBox("Settings") fetchLayout.addWidget(settingsGroup) fetchsettings = QFormLayout() fetchsettings.setRowWrapPolicy(QFormLayout.DontWrapRows) fetchsettings.setFieldGrowthPolicy(QFormLayout.AllNonFixedFieldsGrow) fetchsettings.setFormAlignment(Qt.AlignLeft | Qt.AlignTop) fetchsettings.setLabelAlignment(Qt.AlignLeft) settingsGroup.setLayout(fetchsettings) #fetchLayout.addLayout(fetchsettings) fetchdata = QHBoxLayout() fetchdata.setContentsMargins(10, 0, 10, 0) fetchLayout.addLayout(fetchdata) #bottom right statusWidget = QWidget(self) statusLayout = QVBoxLayout() statusWidget.setLayout(statusLayout) bottomSplitter.addWidget(statusWidget) # # Settings widget #s self.settingsWidget = QWidget() self.settingsLayout = QFormLayout() self.settingsLayout.setContentsMargins(0, 0, 0, 0) self.settingsWidget.setLayout(self.settingsLayout) # Add headers self.headersCheckbox = QCheckBox("Create header nodes", self) self.headersCheckbox.setChecked( str(self.settings.value('saveheaders', 'false')) == 'true') self.headersCheckbox.setToolTip( wraptip( "Check if you want to create nodes containing headers of the response." )) self.settingsLayout.addRow(self.headersCheckbox) # Full offcut self.offcutCheckbox = QCheckBox("Keep all data in offcut", self) self.offcutCheckbox.setChecked( str(self.settings.value('fulloffcut', 'false')) == 'true') self.offcutCheckbox.setToolTip( wraptip( "Check if you don't want to filter out data from the offcut node. Useful for webscraping, keeps the full HTML content" )) self.settingsLayout.addRow(self.offcutCheckbox) # Timeout self.timeoutEdit = QSpinBox(self) self.timeoutEdit.setMinimum(1) self.timeoutEdit.setMaximum(300) self.timeoutEdit.setToolTip( wraptip("How many seconds will you wait for a response?")) self.timeoutEdit.setValue(self.settings.value('timeout', 15)) self.settingsLayout.addRow('Request timeout', self.timeoutEdit) # Max data size self.maxsizeEdit = QSpinBox(self) self.maxsizeEdit.setMinimum(1) self.maxsizeEdit.setMaximum(300000) self.maxsizeEdit.setToolTip( wraptip("How many megabytes will you download at maximum?")) self.maxsizeEdit.setValue(self.settings.value('maxsize', 5)) self.settingsLayout.addRow('Maximum size', self.maxsizeEdit) # Expand Box self.autoexpandCheckbox = QCheckBox("Expand new nodes", self) self.autoexpandCheckbox.setToolTip( wraptip( "Check to automatically expand new nodes when fetching data. Disable for big queries to speed up the process." )) self.settingsLayout.addRow(self.autoexpandCheckbox) self.autoexpandCheckbox.setChecked( str(self.settings.value('expand', 'true')) == 'true') # Log Settings self.logCheckbox = QCheckBox("Log all requests", self) self.logCheckbox.setToolTip( wraptip( "Check to see every request in the status log; uncheck to hide request messages." )) self.settingsLayout.addRow(self.logCheckbox) self.logCheckbox.setChecked( str(self.settings.value('logrequests', 'true')) == 'true') # Clear setttings self.clearCheckbox = QCheckBox("Clear settings when closing.", self) self.settings.beginGroup("GlobalSettings") self.clearCheckbox.setChecked( str(self.settings.value('clearsettings', 'false')) == 'true') self.settings.endGroup() self.clearCheckbox.setToolTip( wraptip( "Check to clear all settings and access tokens when closing Facepager. You should check this on public machines to clear credentials." )) self.settingsLayout.addRow(self.clearCheckbox) # Style self.styleEdit = QComboBox(self) self.styleEdit.setToolTip(wraptip("Choose the styling of Facepager.")) styles = [x for x in QStyleFactory.keys() if x != "Windows"] styles = ['<default>'] + styles self.styleEdit.insertItems(0, styles) self.styleEdit.setCurrentText(self.settings.value( 'style', '<default>')) self.styleEdit.currentIndexChanged.connect(self.setStyle) self.settingsLayout.addRow('Style', self.styleEdit) # # Components # #main tree treetoolbar = QToolBar(self) treetoolbar.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) treetoolbar.setIconSize(QSize(16, 16)) treetoolbar.addActions(self.guiActions.treeActions.actions()) dataLayout.addWidget(treetoolbar) self.tree = DataTree(self.mainWidget) self.tree.nodeSelected.connect(self.guiActions.treeNodeSelected) self.tree.logmessage.connect(self.logmessage) self.tree.showprogress.connect(self.showprogress) self.tree.hideprogress.connect(self.hideprogress) self.tree.stepprogress.connect(self.stepprogress) dataLayout.addWidget(self.tree) #right sidebar - json toolbar jsontoolbar = QToolBar(self) jsontoolbar.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) jsontoolbar.setIconSize(QSize(16, 16)) jsontoolbar.addActions(self.guiActions.detailActions.actions()) detailLayout.addWidget(jsontoolbar) #right sidebar - json viewer self.detailTree = DictionaryTree(self.mainWidget, self.apiWindow) detailLayout.addWidget(self.detailTree) # right sidebar - column setup detailGroup = QWidget() detailSplitter.addWidget(detailGroup) groupLayout = QVBoxLayout() detailGroup.setLayout(groupLayout) #column setup toolbar columntoolbar = QToolBar(self) columntoolbar.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) columntoolbar.setIconSize(QSize(16, 16)) columntoolbar.addActions(self.guiActions.columnActions.actions()) groupLayout.addWidget(columntoolbar) self.fieldList = QTextEdit() self.fieldList.setLineWrapMode(QTextEdit.NoWrap) self.fieldList.setWordWrapMode(QTextOption.NoWrap) self.fieldList.acceptRichText = False self.fieldList.clear() self.fieldList.append('name') self.fieldList.append('message') self.fieldList.setPlainText( self.settings.value('columns', self.fieldList.toPlainText())) groupLayout.addWidget(self.fieldList) #columnhelp = QLabel("Custom table columns: one key per line") #groupLayout.addWidget(columnhelp) #Requests/Apimodules self.RequestTabs = QTabWidget() moduleslayout.addWidget(self.RequestTabs) self.RequestTabs.addTab(YoutubeTab(self), "YouTube") self.RequestTabs.addTab(TwitterTab(self), "Twitter") self.RequestTabs.addTab(TwitterStreamingTab(self), "Twitter Streaming") self.RequestTabs.addTab(FacebookTab(self), "Facebook") self.RequestTabs.addTab(AmazonTab(self), "Amazon") self.RequestTabs.addTab(GenericTab(self), "Generic") module = self.settings.value('module', False) tab = self.getModule(module) if tab is not None: self.RequestTabs.setCurrentWidget(tab) #Fetch settings #-Level self.levelEdit = QSpinBox(self.mainWidget) self.levelEdit.setMinimum(1) self.levelEdit.setToolTip( wraptip( "Based on the selected nodes, only fetch data for nodes and subnodes of the specified level (base level is 1)" )) fetchsettings.addRow("Node level", self.levelEdit) #-Selected nodes self.allnodesCheckbox = QCheckBox(self) self.allnodesCheckbox.setCheckState(Qt.Unchecked) self.allnodesCheckbox.setToolTip( wraptip( "Check if you want to fetch data for all nodes. This helps with large datasets because manually selecting all nodes slows down Facepager." )) fetchsettings.addRow("Select all nodes", self.allnodesCheckbox) #-Empty nodes self.emptyCheckbox = QCheckBox(self) self.emptyCheckbox.setCheckState(Qt.Unchecked) self.emptyCheckbox.setToolTip( wraptip("Check if you want process only empty nodes.")) fetchsettings.addRow("Only empty nodes", self.emptyCheckbox) #Object types self.typesEdit = QLineEdit('offcut') self.typesEdit.setToolTip( wraptip( "Skip nodes with these object types, comma separated list. Normally this should not be changed." )) fetchsettings.addRow("Exclude object types", self.typesEdit) #-Continue pagination self.resumeCheckbox = QCheckBox(self) self.resumeCheckbox.setCheckState(Qt.Unchecked) self.resumeCheckbox.setToolTip( wraptip( "Check if you want to continue collection after fetching was cancelled or nodes were skipped. A selected node (you can use the option to select all nodes) will be added to the queue if: a) It is empty. b) It has no child node with query status fetched (200), object type data, offcut, or empty, and a corresponding query type. c) Such child nodes are used to determine the pagination value. Nodes for which pagination values can be determined are added to the queue." )) fetchsettings.addRow("Resume collection", self.resumeCheckbox) # Thread Box self.threadsEdit = QSpinBox(self) self.threadsEdit.setMinimum(1) self.threadsEdit.setMaximum(40) self.threadsEdit.setToolTip( wraptip( "The number of concurrent threads performing the requests. Higher values increase the speed, but may result in API-Errors/blocks" )) fetchsettings.addRow("Parallel Threads", self.threadsEdit) # Speed Box self.speedEdit = QSpinBox(self) self.speedEdit.setMinimum(1) self.speedEdit.setMaximum(60000) self.speedEdit.setValue(200) self.speedEdit.setToolTip( wraptip( "Limit the total amount of requests per minute (calm down to avoid API blocking)" )) fetchsettings.addRow("Requests per minute", self.speedEdit) #Error Box self.errorEdit = QSpinBox(self) self.errorEdit.setMinimum(1) self.errorEdit.setMaximum(100) self.errorEdit.setValue(10) self.errorEdit.setToolTip( wraptip( "Set the number of consecutive errors after which fetching will be cancelled. Please handle with care! Continuing with erroneous requests places stress on the servers." )) fetchsettings.addRow("Maximum errors", self.errorEdit) #More button = QPushButton(QIcon(":/icons/more.png"), "", self.mainWidget) button.setToolTip( wraptip( "Can't get enough? Here you will find even more settings!")) # button.setMinimumSize(QSize(120,40)) # button.setIconSize(QSize(32,32)) button.clicked.connect(self.guiActions.actionSettings.trigger) fetchsettings.addRow("More settings", button) #Fetch data #-button f = QFont() f.setPointSize(11) button = QPushButton(QIcon(":/icons/fetch.png"), "Fetch Data", self.mainWidget) button.setToolTip( wraptip( "Fetch data from the API with the current settings. If you click the button with the control key pressed, a browser window is opened instead." )) button.setMinimumSize(QSize(120, 40)) button.setIconSize(QSize(32, 32)) button.clicked.connect(self.guiActions.actionQuery.trigger) button.setFont(f) fetchdata.addWidget(button, 1) #-timer button button = QToolButton(self.mainWidget) button.setIcon(QIcon(":/icons/timer.png")) button.setMinimumSize(QSize(40, 40)) button.setIconSize(QSize(25, 25)) button.clicked.connect(self.guiActions.actionTimer.trigger) fetchdata.addWidget(button, 1) #Status detailGroup = QGroupBox("Status Log") groupLayout = QVBoxLayout() detailGroup.setLayout(groupLayout) statusLayout.addWidget(detailGroup, 1) self.loglist = QTextEdit() self.loglist.setLineWrapMode(QTextEdit.NoWrap) self.loglist.setWordWrapMode(QTextOption.NoWrap) self.loglist.acceptRichText = False self.loglist.clear() groupLayout.addWidget(self.loglist) def setStyle(self): style = self.styleEdit.currentText() try: if style == '<default>': style = self.styleEdit.itemText(1) QApplication.setStyle(style) except Exception as e: self.logmessage(e) def databaseLabelClicked(self): if self.database.connected: if platform.system() == "Windows": webbrowser.open(os.path.dirname(self.database.filename)) elif platform.system() == "Darwin": webbrowser.open('file:///' + os.path.dirname(self.database.filename)) else: webbrowser.open('file:///' + os.path.dirname(self.database.filename)) def getModule(self, module): for i in range(0, self.RequestTabs.count()): if self.RequestTabs.widget(i).name == module: return self.RequestTabs.widget(i) return None def updateUI(self): #disable buttons that do not work without an opened database self.guiActions.databaseActions.setEnabled(self.database.connected) self.guiActions.actionQuery.setEnabled(self.tree.selectedCount() > 0) if self.database.connected: #self.statusBar().showMessage(self.database.filename) self.databaseLabel.setText(self.database.filename) else: #self.statusBar().showMessage('No database connection') self.databaseLabel.setText('No database connection') # Downloads default presets and api definitions in the background def updateResources(self): self.apiWindow.checkDefaultFiles() self.presetWindow.checkDefaultFiles() def getter(): self.apiWindow.downloadDefaultFiles(True) self.presetWindow.downloadDefaultFiles(True) t = threading.Thread(target=getter) t.start() def startServer(self): port = cmd_args.port if port is None: self.serverInstance = None self.serverThread = None return False self.serverInstance = Server(port, self.serverActions) self.serverThread = threading.Thread( target=self.serverInstance.serve_forever) self.serverThread.start() self.logmessage('Server started on http://localhost:%d.' % port) def stopServer(self): if self.serverInstance is not None: self.serverInstance.shutdown() self.logmessage("Server stopped") def cleanupModules(self): for i in range(self.RequestTabs.count()): self.RequestTabs.widget(i).cleanup() def writeSettings(self): QCoreApplication.setOrganizationName("Strohne") QCoreApplication.setApplicationName("Facepager") self.settings = QSettings() self.settings.beginGroup("MainWindow") self.settings.setValue("size", self.size()) self.settings.setValue("pos", self.pos()) self.settings.setValue("version", "4.4") self.settings.endGroup() self.settings.setValue('columns', self.fieldList.toPlainText()) self.settings.setValue('module', self.RequestTabs.currentWidget().name) self.settings.setValue("lastpath", self.database.filename) self.settings.setValue('saveheaders', self.headersCheckbox.isChecked()) self.settings.setValue('expand', self.autoexpandCheckbox.isChecked()) self.settings.setValue('logrequests', self.logCheckbox.isChecked()) self.settings.setValue('style', self.styleEdit.currentText()) self.settings.beginGroup("GlobalSettings") self.settings.setValue("clearsettings", self.clearCheckbox.isChecked()) self.settings.endGroup() for i in range(self.RequestTabs.count()): self.RequestTabs.widget(i).saveSettings() def readSettings(self): QSettings.setDefaultFormat(QSettings.IniFormat) QCoreApplication.setOrganizationName("Strohne") QCoreApplication.setApplicationName("Facepager") self.settings = QSettings() def deleteSettings(self): QSettings.setDefaultFormat(QSettings.IniFormat) QCoreApplication.setOrganizationName("Strohne") QCoreApplication.setApplicationName("Facepager") self.settings = QSettings() self.settings.clear() self.settings.sync() self.settings.beginGroup("GlobalSettings") self.settings.setValue("clearsettings", self.clearCheckbox.isChecked()) self.settings.endGroup() def closeEvent(self, event=QCloseEvent()): if self.close(): if self.clearCheckbox.isChecked(): self.deleteSettings() else: self.writeSettings() self.stopServer() self.cleanupModules() event.accept() else: event.ignore() @Slot(str) def logmessage(self, message): with self.lock_logging: if isinstance(message, Exception): self.loglist.append( str(datetime.now()) + " Exception: " + str(message)) logging.exception(message) else: self.loglist.append(str(datetime.now()) + " " + message) time.sleep(0) def getlog(self): with self.lock_logging: return self.loglist.toPlainText().splitlines() @Slot(str) def showprogress(self, maximum=None): pass # if self.progressWindow is None: # self.progressWindow = ProgressBar("Loading nodes...",self) # # self.progressWindow.setMaximum(maximum) @Slot(str) def stepprogress(self): pass # if (self.progressWindow is not None): # self.progressWindow.step() @Slot(str) def hideprogress(self): pass
def main(): args = parse_args() # Set the random seed if provided (affects client sampling, and batching) random.seed(1 + args.seed) np.random.seed(12 + args.seed) tf.set_random_seed(123 + args.seed) model_path = '%s/%s.py' % (args.dataset, args.model) if not os.path.exists(model_path): print('Please specify a valid dataset and a valid model.') model_path = '%s.%s' % (args.dataset, args.model) print('############################## %s ##############################' % model_path) mod = importlib.import_module(model_path) ClientModel = getattr(mod, 'ClientModel') tup = MAIN_PARAMS[args.dataset][args.t] num_rounds = args.num_rounds if args.num_rounds != -1 else tup[0] eval_every = args.eval_every if args.eval_every != -1 else tup[1] clients_per_round = args.clients_per_round if args.clients_per_round != -1 else tup[ 2] num_client_servers = args.num_client_servers # Suppress tf warnings tf.logging.set_verbosity(tf.logging.WARN) # Create 2 models model_params = MODEL_PARAMS[model_path] if args.lr != -1: model_params_list = list(model_params) model_params_list[0] = args.lr model_params = tuple(model_params_list) # Create client model, and share params with server model tf.reset_default_graph() server_model = ClientModel(args.seed, *model_params) if args.multi_node: ray.init(address='auto', redis_password='******') else: ray.init(local_mode=args.no_parallel) # Create clients client_servers = setup_client_servers(args.dataset, args.seed, model_params, ClientModel, args.use_val_set, num_client_servers) # Create server server = Server(server_model, client_servers) # clients = setup_clients(args.dataset, client_model, args.use_val_set) client_ids, client_groups, client_num_samples = server.get_clients_info( all_clients=True) print('Clients in Total: %d' % len(client_ids)) # Initial status print('--- Random Initialization ---') stat_writer_fn = get_stat_writer_function(client_ids, client_groups, client_num_samples, args) sys_writer_fn = get_sys_writer_function(args) print_stats(0, server, client_num_samples, args, stat_writer_fn, args.use_val_set) # Simulate training for i in range(num_rounds): print('--- Round %d of %d: Training %d Clients ---' % (i + 1, num_rounds, clients_per_round)) # Select clients to train this round server.select_clients(i, num_clients=clients_per_round) c_ids, c_groups, c_num_samples = server.get_clients_info() # Simulate server model training on selected clients' data sys_metrics = server.train_model(num_epochs=args.num_epochs, batch_size=args.batch_size, minibatch=args.minibatch) sys_writer_fn(i + 1, c_ids, sys_metrics, c_groups, c_num_samples) # Update server model server.update_model() # Test model if (i + 1) % eval_every == 0 or (i + 1) == num_rounds: print_stats(i + 1, server, c_num_samples, args, stat_writer_fn, args.use_val_set) # Save server model ckpt_path = os.path.join('checkpoints', args.dataset) if not os.path.exists(ckpt_path): os.makedirs(ckpt_path) save_path = server.save_model( os.path.join(ckpt_path, '{}.ckpt'.format(args.model))) print('Model saved in path: %s' % save_path) # Close models server.close_model() # stop Ray driver after job finish ray.shutdown()
## ## File: server.py ## Date: 8 march 2020 ## Author: Romain Goasdoué ## from typing import overload from mail import Mail from server import Server import asyncio import sys if __name__ == "__main__": print("==================================") print("||\tDead Man's Switch\t||") print("==================================") server = Server() server.run()