def main(): parser = argparse.ArgumentParser(description='Tic Tac Toe') subparsers = parser.add_subparsers(title='module', dest='module') subparsers.required = True subparsers.add_parser('server', help='start the server') subparsers.add_parser('client', help='start a client') args = parser.parse_args() if args.module == 'server': server = Server() try: server.start() except BaseException as e: logger.error('Process interrupted, stopping the server', exc_info=e) finally: server.stop() elif args.module == 'client': client = Client() try: client.connect() client.run() except BaseException as e: logger.error('Process interrupted, disconnecting the client', exc_info=e) finally: client.disconnect() else: print('unknown module {}'.format(args.module), file=sys.stderr)
def __init__(self, config): self.__config = config self.__server = Server() conn_info = self.__config.get_conn_info() repeat = self.__config.get_repeat_number() access_manager.init(conn_info, repeat) valid_key_checker.set_max_ttl_key(self.__config.get_ttl_key_hours())
def test_server_will_reject_repeated_socket_registrations(self): """ Server should ignore second and later ConnectRequestMsg sent by a connected socket. """ server = Server() connection_msg_a = e.ConnectRequestMsg('test_name') connection_msg_b = e.ConnectRequestMsg('test_name') uri = 'ws://localhost:8765' server_thread = th.Thread( target=lambda: server.start('localhost', 8765), daemon=True) try: server_thread.start() time.sleep(0.01) # give server thread a little time to start async def test_send_duplicates(): async with websockets.connect(uri) as web_socket: await web_socket.send(e.encode_msg(connection_msg_a)) await web_socket.send(e.encode_msg(connection_msg_b)) asyncio.get_event_loop().run_until_complete(test_send_duplicates()) self.assertEqual(1, len(server.connections)) finally: server.end() server_thread.join()
def main(): server = Server() try: server.start() except (KeyboardInterrupt, IOError): print('[INFO] Exit now...') return
def startProcess(conf, sock): from handler.executor import Executor from handler.handler import Handler from server.server import Server from asyncio import get_event_loop loop = get_event_loop() loop.add_signal_handler(signal.SIGINT, closeHandler, loop) try: info('running source with PID {}'.format(str(getpid()))) handler = Handler(conf.root_dir, Executor(conf.root_dir)) for _ in range(0, int(conf.threads)): server = Server(config=conf, loop=loop, handler=handler, sock=sock) loop.create_task(server.launch_server()) loop.run_forever() except Exception: loop.stop() info('stop loop {}'.format(getpid())) finally: loop.stop() info('stop loop {}'.format(getpid()))
def main(): arguments = docopt(__doc__, version=__version__) level = getattr(logging, arguments.get("--level").upper()) handlers = [StreamHandler()] log = logging.getLogger(__name__) logging.basicConfig( level=level, format="%(asctime)s %(name)-15s: %(levelname)-8s %(message)s", handlers=handlers) port = arguments.get("--port") # Are we a server or a client? if arguments.get("server"): log.debug("We are a server!") log.debug("Running on: {} port: {}".format("localhost", port)) server = Server(port) server.serve() else: server_ip = arguments.get("<server_ip>") log.debug("We are a client!") log.debug("Running on: {} port: {}".format(server_ip, port)) client = Client(server_ip, port) client.run()
def serverStartWithParams(): base_dir = os.path.dirname( os.path.dirname(__file__)) # This is your Project Root config_path = os.path.join( base_dir, 'tests/configs/config_no_vlan') # Join Project Root with config Server.start(config_path=config_path)
class TransmitterLogging(TransmitterBase): def setUp(self): self.server = Server() self.server.setLogTarget("/dev/null") self.server.setLogLevel(0) super(TransmitterLogging, self).setUp() def testLogTarget(self): logTargets = [] for _ in xrange(3): tmpFile = tempfile.mkstemp("fail2ban", "transmitter") logTargets.append(tmpFile[1]) os.close(tmpFile[0]) for logTarget in logTargets: self.setGetTest("logtarget", logTarget) # If path is invalid, do not change logtarget value = "/this/path/should/not/exist" self.setGetTestNOK("logtarget", value) self.transm.proceed(["set", "/dev/null"]) for logTarget in logTargets: os.remove(logTarget) self.setGetTest("logtarget", "STDOUT") self.setGetTest("logtarget", "STDERR") self.setGetTest("logtarget", "SYSLOG") def testLogLevel(self): self.setGetTest("loglevel", "4", 4) self.setGetTest("loglevel", "2", 2) self.setGetTest("loglevel", "-1", -1) self.setGetTest("loglevel", "0", 0) self.setGetTestNOK("loglevel", "Bird")
def make_server(self, name): address = self.settings.setting(name, "address") username = self.settings.setting(name, "username") password = self.settings.setting(name, "password") game_password = self.settings.setting(name, "game_password") url_extras = self.settings.setting(name, "url_extras") refresh_rate = float(self.settings.setting(name, "refresh_rate")) web_interface = WebInterface(address, username, password, name) chat = Chat(web_interface) chat.start() web_admin = WebAdmin(web_interface, chat) database = ServerDatabase(name) game = Game(GameMap(), GAME_TYPE_UNKNOWN) server = Server(web_admin, database, game, name) if game_password: server.game_password = game_password if url_extras: server.url_extras = url_extras tracker = GameTracker(server, refresh_rate) tracker.start() self.stop_list.append(server) self.stop_list.append(chat) self.stop_list.append(tracker) return server
def main(): """ Main function that handles start of server function. :return: int """ sys.path += [SERVER_ROOT] server = Server() server.start('127.0.0.1', 8240)
async def main(): os.chdir(Config.ROOT) sys.path.append(os.path.join(Config.ROOT, 'protocol')) from server.server import Server from game.game import Game game = Game(Config) server = Server(game, Config) await asyncio.gather(game.run(), server.run())
def main(): try: args = parse_arguments() address = (args.host, args.port) sv = Server(address) sv.run() except KeyboardInterrupt: print("\nServer disconnected")
def __init__(self): """ Initialize a client object. Create a logger object used to log data. Create a server object for communication with the server. """ self.logger = self.get_logger() self.server = Server()
def checkListNameIsUsed(self): """Checks if the project has a list with the same name used in the form. """ server = Server() listName = self.name.text() projectId = self.projectDict[self.projectBox.currentText()] id = server.getListIdFromProject(listName, projectId) return id > 0
def loadTableFromServer(self): """Loads the lines that belongs to the table with listId = id and stores the information in the table variable. """ server = Server() self.table = [list(line) for line in server.getLinesFromList(self.id)] for line in self.table: self.lineIds.append(line[0]) line.pop(0)
def __criarServer(self, nJogadores): self.__server = Server(self.__interfaceRede) self.__server.start() self.__server.esperarEntrarJogadores(nJogadores) # iniciar partida self.__server.iniciarJogo() # fim self.__server.desligar()
def createProjectDict(self): """Creates the dictionary projectDict that uses the names of the projects as keys and their ids as values. Only the projects currently available on the database are added to projectDict. """ server = Server() table = server.getTable("projects") self.projectIds = [project[0] for project in table] self.projectNames = [project[1] for project in table] self.projectDict = dict(zip(self.projectNames, self.projectIds))
def save(self): """Sends all changes made in the TagTable to the lines table in the server. """ server = Server() for item in self.changeItems: column = self.colNamesOnSql[item.column()] value = item.text() if item.column() != 4 else int(item.text()) server.updateLine(column, value, item.lineId) self.wasChanged = False
def addTree(self, id: int): """Takes the projectId searchs for the project name on the server, then the function creates a QtWidgets.QTreeWidgetItem using the project name and defines this item as an class top level item. """ server = Server() name = server.getProjectName(id) self.projectIds[name] = id self.trees[name] = QtWidgets.QTreeWidgetItem([name]) self.addTopLevelItem(self.trees[name])
def main(args): server = Server() if args[1] == 'createIndex': server.index_creater() elif args[1] == 'deleteIndex': server.index_remover() elif args[1] == 'insertPdfDir': server.index_pdf_dir(args[2]) elif args[1] == 'deletePdf': server.delete_pdf(args[2])
def attach_plot_components( video: str, server: Server, trace: Optional[str] = None, bandwidth: Optional[int] = None, no_plot: bool = False, ) -> Dict[str, LivePlot]: """ Attach multiple plot components with diverse metrics to react to and plot Monitor-generated metrics. """ segments = get_video_chunks(video) + 1 max_playback = int(1.25 * get_approx_video_length(video)) if no_plot: (server.add_post('/raw_qoe', do_nothing).add_post( '/rebuffer', do_nothing).add_post('/quality', do_nothing).add_post( '/vmaf', do_nothing).add_post('/vmaf_qoe', do_nothing).add_post('/bw', do_nothing)) return {} plots = { 'raw_qoe': LivePlot(figure_name='qoe', y_label='raw qoe', range_size=segments), 'rebuffer': LivePlot(figure_name='rebuffer', y_label='rebuffer', range_size=segments), 'quality': LivePlot(figure_name='quality', y_label='quality', range_size=segments), 'vmaf': LivePlot(figure_name='vmaf', y_label='vmaf', range_size=segments), 'vmaf_qoe': LivePlot(figure_name='vmaf_qoe', y_label='vmaf qoe', range_size=segments), 'bw': BandwidthPlot( figure_name='bw', y_label='bw estimation(mbps)', trace=trace, bandwidth=bandwidth, range_size=max_playback, ), } (server.add_post('/raw_qoe', plots['raw_qoe']).add_post( '/rebuffer', plots['rebuffer']).add_post('/quality', plots['quality']).add_post( '/vmaf', plots['vmaf']).add_post('/vmaf_qoe', plots['vmaf_qoe']).add_post( '/bw', plots['bw'])) return plots
def onItemDoubleClicked(self, it: QtWidgets.QTreeWidgetItem, col: int): """When a item in ProjectTree is double clicked this function is activated. The function checks if QTreeWidgetItem has a parent, if the item has a parent the function will search the list id in the database and it will create a TagTable in the Tab widget. """ if it.parent() is not None: server = Server() listName = it.text(col) projectId = self.projectIds[str(it.parent().text(col))] listId = server.getListIdFromProject(listName, projectId) self.openList(listId)
def run(self): """ run the process as a daemon :return: """ #with daemon.DaemonContext(): # the pid has to be stored inside the daemon because the daemon actually spawns a # different process # The daemon context must have a separate logging.basicConfig and that is crucial. self.prepare_for_daemonizing_process() server_instance = Server() server_instance.start()
def addBranches(self, id: int): """Searches for all the lists in the server that have id as their projectId. Creates a QTreeWidgetItem for each list and add the item into the top level QTreeWidgetItem. """ server = Server() name = server.getProjectName(id) branches = server.getListsNameFromProject(id) self.listsOpen[name] = branches for branch in branches: item = QtWidgets.QTreeWidgetItem(branch) self.trees[name].addChild(item) self.trees[name].setExpanded(True)
def main(): """ Run the server :return: """ server = Server() def signal_handler(signal, frame): server.stop_gui() server.stop() sys.exit(0) signal.signal(signal.SIGINT, signal_handler) server.start()
def test_server_can_register_connection(self): server = Server() connection_msg = e.ConnectRequestMsg('test_name') uri = 'ws://localhost:8765' server_thread = th.Thread( target=lambda: server.start('localhost', 8765), daemon=True) try: server_thread.start() time.sleep(0.01) # give server thread a little time to start send_test_msg(connection_msg, uri) self.assertEqual(1, len(server.connections)) finally: server.end() server_thread.join()
def main(): data_server = Server() start_thread = Thread(target=data_server.start_server) start_thread.start() request_thread = Thread(target=data_server.request_measurements) request_thread.start()
def serve(): print("Starting server at: 8083") s = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) grpc_face.add_FaceGRPCHandlerServicer_to_server(Server(), s) s.add_insecure_port('face:8083') s.start() s.wait_for_termination()
def __init__(self, port, ds_host, ds_port): super(FileService, self).__init__() self.dir = '' self.port = port self.ds_host = ds_host self.ds_port = ds_port self.server = Server(port, self._handler)
def isTreeUpdated(self, projectName: str) -> bool: """If any project is currently open in the ProjectTree, the function checks if those projects are update. If the project is updated the function returns true. """ if len(self.listsOpen) > 0: server = Server() dbList = server.getListsNameFromProject( self.projectIds[projectName]) if len(self.listsOpen[projectName]) != len(dbList): return False else: return sorted(self.listsOpen[projectName]) == sorted(dbList) else: return True
def main(args): http_server = CherrypyHTTPServer({ "server.socket_port": args.port, "server.socket_host": args.host, "server.thread_pool": args.connections, "server.max_request_body_size": 1024 * 1024 * 1024, "log.screen": True, "environment": "test_suite" if args.debug else "production", "tools.sessions.on": True, "tools.sessions.timeout": 24 * 60, # 24 hours "engine.autoreload.on": True, 'tools.proxy.on': True, 'log.access_file': "access.log" }) server = Server() handler = ServerHandler(server) http_server.set_handler(handler) http_server.start()
def run(self): for server_name in config.sections(): # Changing the log level to the level specified in the config file logger.setLevel( logging.getLevelName(config[server_name]["log_level"])) address = config[server_name]["address"] user = config[server_name]["username"] password = config[server_name]["password"] game_password = config[server_name]["game_password"] motd_scoreboard = str_to_bool( config[server_name]["motd_scoreboard"]) scoreboard_type = config[server_name]["scoreboard_type"] level_threshhold = config[server_name]["level_threshhold"] enable_greeter = str_to_bool(config[server_name]["enable_greeter"]) max_players = config[server_name]["max_players"] server = Server(server_name, address, user, password, game_password, max_players, level_threshhold) self.servers.append(server) if motd_scoreboard: motd_updater = MotdUpdater(server, scoreboard_type) motd_updater.start() self.motd_updaters.append(motd_updater) cb = Chatbot(server, greeter_enabled=enable_greeter) server.chat.add_listener(cb) self.chatbots.append(cb) print("Initialisation complete")
def build_server(self, server_address, server_port): """ Initial a new server object """ server = Server(server_address, server_port) p = Process(target=server.start, daemon=True) self.server_process = p
def setUp(self): """Call before every test case.""" self.__server = Server() self.__transm = self.__server._Server__transm self.__server.setLogTarget("/dev/null") self.__server.setLogLevel(0) sock_fd, sock_name = tempfile.mkstemp('fail2ban.sock', 'transmitter') os.close(sock_fd) pidfile_fd, pidfile_name = tempfile.mkstemp( 'fail2ban.pid', 'transmitter') os.close(pidfile_fd) self.__server.start(sock_name, pidfile_name, force=False) self.jailName = "TestJail1" self.__server.addJail(self.jailName, "auto")
#!/usr/bin/env python3 from server.server import Server Server.start()
def setUp(self): self.server = Server() self.server.setLogTarget("/dev/null") self.server.setLogLevel(0) super(TransmitterLogging, self).setUp()
#!/usr/bin/env python3 from server.server import Server if __name__ == "__main__": server = Server(host='', port=8080) server.run()
def serverStartWithParams(): Server.start(debug_mode=True, config_path="../tests/config", vlan_activate=False)
def serverStartWithParams(): base_dir = os.path.dirname(os.path.dirname(__file__)) # This is your Project Root config_path = os.path.join(base_dir, 'tests/configs/config_no_vlan') # Join Project Root with config Server.start(config_path=config_path)
class LockService(Database): """ LockService provdes functionality to lock access to a particular file on the fileservers. Every service that writes to file uses the lockService, every read of a file checks if the particular file is locked. The lockservice provides rudimentary acknowledgments on all operations """ DATABASE = "sql.db" def __init__(self, port): super(LockService, self).__init__() self.port = port self._create_tables() self.server = Server(port, self._handler) def _lock_file(self, file_name): is_locked = self._check_if_locked(file_name) if is_locked: return False insert = self.execute_sql("insert into locks (file_name) values (?)", (file_name,), exclusive=True) if insert: return True else: return False def _unlock_file(self, file_name): delete = self.execute_sql("delete from locks where file_name = ?", (file_name,), exclusive=True) if delete: return True else: return False def _check_if_locked(self, file_name): data = self.fetch_sql("select * from locks where file_name=?", (file_name,)) if file_name in data: return True else: return False def _handler(self, sock): try: data = sock.recv(1024) msg = data.decode('utf-8') msg = msg.replace('$', ' ') if 'KILL_SERVICE' in msg: exit() elif msg[:4] == "LOCK": temp = msg.split() is_locked = self._unlock_file(temp[1]) if is_locked: self.server.respond(sock, response200()) else: self.server.respond(sock, response604()) elif msg[:6] == "UNLOCK": temp = msg.split() is_unlocked = self._unlock_file( temp[1]) if is_unlocked: self.server.respond(sock, response200()) else: self.server.respond(sock, response604()) elif msg[:5] == "CHECK": temp = msg.split() locked = self._check_if_locked(temp[1]) if locked: self.server.respond(sock, responseLocked()) else: self.server.respond(sock, responseUnlocked()) else: self.server.error(sock, 0, responseErrParse()) except sock.error as e: err = e.args[0] self.server.error(sock, 0, e.args[1]) def _create_tables(self): conn = sqlite3.connect(self.DATABASE) cur = conn.cursor() cur.executescript(""" create table if not exists locks( id INTEGER PRIMARY KEY AUTOINCREMENT, file_name TEXT );""")
class StartStop(unittest.TestCase): def setUp(self): """Call before every test case.""" self.__server = Server() self.__server.setLogLevel(0) self.__server.start(False) def tearDown(self): """Call after every test case.""" self.__server.quit() def testStartStopJail(self): name = "TestCase" self.__server.addJail(name) self.__server.startJail(name) time.sleep(1) self.__server.stopJail(name)
def __init__(self, port): super(LockService, self).__init__() self.port = port self._create_tables() self.server = Server(port, self._handler)
class TransmitterLogging(TransmitterBase): def setUp(self): self.server = Server() self.server.setLogTarget("/dev/null") self.server.setLogLevel(0) super(TransmitterLogging, self).setUp() def testLogTarget(self): logTargets = [] for _ in xrange(3): tmpFile = tempfile.mkstemp("fail2ban", "transmitter") logTargets.append(tmpFile[1]) os.close(tmpFile[0]) for logTarget in logTargets: self.setGetTest("logtarget", logTarget) # If path is invalid, do not change logtarget value = "/this/path/should/not/exist" self.setGetTestNOK("logtarget", value) self.transm.proceed(["set", "/dev/null"]) for logTarget in logTargets: os.remove(logTarget) self.setGetTest("logtarget", "STDOUT") self.setGetTest("logtarget", "STDERR") if sys.platform.lower().startswith('linux'): self.setGetTest("logtarget", "SYSLOG") def testLogLevel(self): self.setGetTest("loglevel", "4", 4) self.setGetTest("loglevel", "3", 3) self.setGetTest("loglevel", "2", 2) self.setGetTest("loglevel", "1", 1) self.setGetTest("loglevel", "-1", -1) self.setGetTest("loglevel", "0", 0) self.setGetTestNOK("loglevel", "Bird") def testFlushLogs(self): self.assertEqual(self.transm.proceed(["flushlogs"]), (0, "rolled over")) try: f, fn = tempfile.mkstemp("fail2ban.log") os.close(f) self.server.setLogLevel(2) self.assertEqual(self.transm.proceed(["set", "logtarget", fn]), (0, fn)) logSys.warn("Before file moved") try: f2, fn2 = tempfile.mkstemp("fail2ban.log") os.close(f2) os.rename(fn, fn2) logSys.warn("After file moved") self.assertEqual(self.transm.proceed(["flushlogs"]), (0, "rolled over")) logSys.warn("After flushlogs") # >py2.4: with open(fn2, 'r') as f: f = open(fn2, 'r'); if True: self.assertTrue(f.next().endswith("Before file moved\n")) self.assertTrue(f.next().endswith("After file moved\n")) self.assertRaises(StopIteration, f.next) f.close() # >py2.4: with open(fn, 'r') as f: f = open(fn, 'r'); if True: self.assertTrue(f.next().endswith("After flushlogs\n")) self.assertRaises(StopIteration, f.next) f.close() finally: os.remove(fn2) finally: os.remove(fn) self.assertEqual(self.transm.proceed(["set", "logtarget", "STDERR"]), (0, "STDERR")) self.assertEqual(self.transm.proceed(["flushlogs"]), (0, "flushed"))
def serverStartWithParams(): base_dir = os.path.dirname(os.path.dirname(__file__)) # This is your Project Root config_path = os.path.join(base_dir, "framework_unittests/configs/config_no_vlan") Server.start(config_path=config_path)
def test_get_routers(self): routers = Server.get_routers() assert len(routers) != 0 assert isinstance(routers[0], Router)
"""Script to launch the Python Aboard server.""" import os import sys from command.tree import Tree from server.server import Server from model import Model from system import * from tools.console import Console commands = Tree() commands.add_default_commands() command, args = commands.process(sys.argv[1:]) source_directory = get_source_directory() if command: directory = args.path server = Server(directory, check_dir=command.project_created) server.source_directory = source_directory if command.project_created and not command.children: server.load_configurations() server.prepare() server.load_bundles() command.server = server command.execute(args) if command.project_created: Model.data_connector.repository_manager.save()
def __init__(self, port, replication): super(DirectoryService, self).__init__() self.port = port self.replication = replication self._create_tables() self.server = Server(port, self._handler)
class Transmitter(unittest.TestCase): def setUp(self): """Call before every test case.""" self.__server = Server() self.__transm = self.__server._Server__transm self.__server.setLogTarget("/dev/null") self.__server.setLogLevel(0) sock_fd, sock_name = tempfile.mkstemp('fail2ban.sock', 'transmitter') os.close(sock_fd) pidfile_fd, pidfile_name = tempfile.mkstemp( 'fail2ban.pid', 'transmitter') os.close(pidfile_fd) self.__server.start(sock_name, pidfile_name, force=False) self.jailName = "TestJail1" self.__server.addJail(self.jailName, "auto") def tearDown(self): """Call after every test case.""" self.__server.quit() def setGetTest(self, cmd, inValue, outValue=None, jail=None): setCmd = ["set", cmd, inValue] getCmd = ["get", cmd] if jail is not None: setCmd.insert(1, jail) getCmd.insert(1, jail) if outValue is None: outValue = inValue self.assertEqual(self.__transm.proceed(setCmd), (0, outValue)) self.assertEqual(self.__transm.proceed(getCmd), (0, outValue)) def setGetTestNOK(self, cmd, inValue, jail=None): setCmd = ["set", cmd, inValue] getCmd = ["get", cmd] if jail is not None: setCmd.insert(1, jail) getCmd.insert(1, jail) # Get initial value before trying invalid value initValue = self.__transm.proceed(getCmd)[1] self.assertEqual(self.__transm.proceed(setCmd)[0], 1) # Check after failed set that value is same as previous self.assertEqual(self.__transm.proceed(getCmd), (0, initValue)) def jailAddDelTest(self, cmd, values, jail): cmdAdd = "add" + cmd cmdDel = "del" + cmd self.assertEqual( self.__transm.proceed(["get", jail, cmd]), (0, [])) for n, value in enumerate(values): self.assertEqual( self.__transm.proceed(["set", jail, cmdAdd, value]), (0, values[:n+1])) self.assertEqual( self.__transm.proceed(["get", jail, cmd]), (0, values[:n+1])) for n, value in enumerate(values): self.assertEqual( self.__transm.proceed(["set", jail, cmdDel, value]), (0, values[n+1:])) self.assertEqual( self.__transm.proceed(["get", jail, cmd]), (0, values[n+1:])) def jailAddDelRegexTest(self, cmd, inValues, outValues, jail): cmdAdd = "add" + cmd cmdDel = "del" + cmd if outValues is None: outValues = inValues self.assertEqual( self.__transm.proceed(["get", jail, cmd]), (0, [])) for n, value in enumerate(inValues): self.assertEqual( self.__transm.proceed(["set", jail, cmdAdd, value]), (0, outValues[:n+1])) self.assertEqual( self.__transm.proceed(["get", jail, cmd]), (0, outValues[:n+1])) for n, value in enumerate(inValues): self.assertEqual( self.__transm.proceed(["set", jail, cmdDel, 0]), # First item (0, outValues[n+1:])) self.assertEqual( self.__transm.proceed(["get", jail, cmd]), (0, outValues[n+1:])) def testPing(self): self.assertEqual(self.__transm.proceed(["ping"]), (0, "pong")) def testSleep(self): t0 = time.time() self.assertEqual(self.__transm.proceed(["sleep", "1"]), (0, None)) t1 = time.time() # Approx 1 second delay self.assertAlmostEqual(t1 - t0, 1, places=2) def testLogTarget(self): logTargets = [] for _ in xrange(3): tmpFile = tempfile.mkstemp("fail2ban", "transmitter") logTargets.append(tmpFile[1]) os.close(tmpFile[0]) for logTarget in logTargets: self.setGetTest("logtarget", logTarget) # If path is invalid, do not change logtarget value = "/this/path/should/not/exist" self.assertEqual( self.__transm.proceed(["set", "logtarget", value]), (0, logTarget)) #NOTE: Shouldn't this return 1 self.assertEqual( self.__transm.proceed(["get", "logtarget"]), (0, logTargets[-1])) self.__transm.proceed(["set", "/dev/null"]) for logTarget in logTargets: os.remove(logTarget) def testLogLevel(self): self.setGetTest("loglevel", "4", 4) self.setGetTest("loglevel", "2", 2) self.setGetTest("loglevel", "-1", -1) self.setGetTestNOK("loglevel", "Bird") def testAddJail(self): jail2 = "TestJail2" jail3 = "TestJail3" jail4 = "TestJail4" self.assertEqual( self.__transm.proceed(["add", jail2, "polling"]), (0, jail2)) self.assertEqual(self.__transm.proceed(["add", jail3]), (0, jail3)) self.assertEqual( self.__transm.proceed(["add", jail4, "invalid backend"])[0], 1) self.assertEqual( self.__transm.proceed(["add", jail4, "auto"]), (0, jail4)) # Duplicate Jail self.assertEqual( self.__transm.proceed(["add", self.jailName, "polling"])[0], 1) # All name is reserved self.assertEqual( self.__transm.proceed(["add", "all", "polling"])[0], 1) def testJailIdle(self): self.assertEqual( self.__transm.proceed(["set", self.jailName, "idle", "on"]), (0, True)) self.assertEqual( self.__transm.proceed(["set", self.jailName, "idle", "off"]), (0, False)) self.assertEqual( self.__transm.proceed(["set", self.jailName, "idle", "CAT"])[0], 0) #NOTE: Should this return 1 def testJailFindTime(self): self.setGetTest("findtime", "120", 120, jail=self.jailName) self.setGetTest("findtime", "60", 60, jail=self.jailName) self.setGetTest("findtime", "-60", -60, jail=self.jailName) self.setGetTestNOK("findtime", "Dog", jail=self.jailName) def testJailBanTime(self): self.setGetTest("bantime", "600", 600, jail=self.jailName) self.setGetTest("bantime", "50", 50, jail=self.jailName) self.setGetTest("bantime", "-50", -50, jail=self.jailName) self.setGetTestNOK("bantime", "Cat", jail=self.jailName) def testJailUseDNS(self): self.setGetTest("usedns", "yes", jail=self.jailName) self.setGetTest("usedns", "warn", jail=self.jailName) self.setGetTest("usedns", "no", jail=self.jailName) # Safe default should be "no" value = "Fish" self.assertEqual( self.__transm.proceed(["set", self.jailName, "usedns", value]), (0, "no")) def testJailBanIP(self): self.__server.startJail(self.jailName) # Jail must be started self.assertEqual( self.__transm.proceed(["set", self.jailName, "banip", "127.0.0.1"]), (0, "127.0.0.1")) time.sleep(1) # Give chance to ban self.assertEqual( self.__transm.proceed(["set", self.jailName, "banip", "Badger"]), (0, "Badger")) #NOTE: Is IP address validated? Is DNS Lookup done? time.sleep(1) # Give chance to ban # Unban IP self.assertEqual( self.__transm.proceed( ["set", self.jailName, "unbanip", "127.0.0.1"]), (0, "127.0.0.1")) # Unban IP which isn't banned self.assertEqual( self.__transm.proceed( ["set", self.jailName, "unbanip", "192.168.1.1"]), (0, "None")) #NOTE: Should this return 1? def testJailMaxRetry(self): self.setGetTest("maxretry", "5", 5, jail=self.jailName) self.setGetTest("maxretry", "2", 2, jail=self.jailName) self.setGetTest("maxretry", "-2", -2, jail=self.jailName) self.setGetTestNOK("maxretry", "Duck", jail=self.jailName) def testJailLogPath(self): self.jailAddDelTest( "logpath", [ "testcases/files/testcase01.log", "testcases/files/testcase02.log", "testcases/files/testcase03.log", ], self.jailName ) # Try duplicates value = "testcases/files/testcase04.log" self.assertEqual( self.__transm.proceed(["set", self.jailName, "addlogpath", value]), (0, [value])) # Will silently ignore duplicate self.assertEqual( self.__transm.proceed(["set", self.jailName, "addlogpath", value]), (0, [value])) self.assertEqual( self.__transm.proceed(["get", self.jailName, "logpath"]), (0, [value])) self.assertEqual( self.__transm.proceed(["set", self.jailName, "dellogpath", value]), (0, [])) # Invalid file value = "this_file_shouldn't_exist" result = self.__transm.proceed( ["set", self.jailName, "addlogpath", value]) self.assertTrue(isinstance(result[1], IOError)) def testJailIgnoreIP(self): self.jailAddDelTest( "ignoreip", [ "127.0.0.1", "192.168.1.1", "8.8.8.8", ], self.jailName ) # Try duplicates value = "127.0.0.1" self.assertEqual( self.__transm.proceed(["set", self.jailName, "addignoreip", value]), (0, [value])) # Will allow duplicate #NOTE: Should duplicates be allowed, or silent ignore like logpath? self.assertEqual( self.__transm.proceed(["set", self.jailName, "addignoreip", value]), (0, [value, value])) self.assertEqual( self.__transm.proceed(["get", self.jailName, "ignoreip"]), (0, [value, value])) self.assertEqual( self.__transm.proceed(["set", self.jailName, "delignoreip", value]), (0, [value])) def testJailRegex(self): self.jailAddDelRegexTest("failregex", [ "user john at <HOST>", "Admin user login from <HOST>", "failed attempt from <HOST> again", ], [ "user john at (?:::f{4,6}:)?(?P<host>[\w\-.^_]+)", "Admin user login from (?:::f{4,6}:)?(?P<host>[\w\-.^_]+)", "failed attempt from (?:::f{4,6}:)?(?P<host>[\w\-.^_]+) again", ], self.jailName ) self.assertEqual( self.__transm.proceed( ["set", self.jailName, "addfailregex", "No host regex"]), (0, [])) #NOTE: Shouldn't this return 1? self.assertEqual( self.__transm.proceed( ["set", self.jailName, "addfailregex", 654])[0], 1) def testJailIgnoreRegex(self): self.jailAddDelRegexTest("ignoreregex", [ "user john", "Admin user login from <HOST>", "Dont match me!", ], [ "user john", "Admin user login from (?:::f{4,6}:)?(?P<host>[\w\-.^_]+)", "Dont match me!", ], self.jailName ) self.assertEqual( self.__transm.proceed( ["set", self.jailName, "addignoreregex", 50])[0], 1) def testStatus(self): jails = [self.jailName] self.assertEqual(self.__transm.proceed(["status"]), (0, [('Number of jail', len(jails)), ('Jail list', ", ".join(jails))])) self.__server.addJail("TestJail2", "auto") jails.append("TestJail2") self.assertEqual(self.__transm.proceed(["status"]), (0, [('Number of jail', len(jails)), ('Jail list', ", ".join(jails))])) def testJailStatus(self): self.assertEqual(self.__transm.proceed(["status", self.jailName]), (0, [ ('filter', [ ('Currently failed', 0), ('Total failed', 0), ('File list', [])] ), ('action', [ ('Currently banned', 0), ('Total banned', 0), ('IP list', [])] ) ] ) ) def testAction(self): action = "TestCaseAction" cmdList = [ "actionstart", "actionstop", "actioncheck", "actionban", "actionunban", ] cmdValueList = [ "Action Start", "Action Stop", "Action Check", "Action Ban", "Action Unban", ] self.assertEqual( self.__transm.proceed(["set", self.jailName, "addaction", action]), (0, action)) for cmd, value in zip(cmdList, cmdValueList): self.assertEqual( self.__transm.proceed( ["set", self.jailName, cmd, action, value]), (0, value)) for cmd, value in zip(cmdList, cmdValueList): self.assertEqual( self.__transm.proceed(["get", self.jailName, cmd, action]), (0, value)) self.assertEqual( self.__transm.proceed( ["set", self.jailName, "setcinfo", action, "KEY", "VALUE"]), (0, "VALUE")) self.assertEqual( self.__transm.proceed( ["get", self.jailName, "cinfo", action, "KEY"]), (0, "VALUE")) self.assertEqual( self.__transm.proceed( ["get", self.jailName, "cinfo", action, "InvalidKey"])[0], 1) self.assertEqual( self.__transm.proceed( ["set", self.jailName, "delcinfo", action, "KEY"]), (0, None)) self.assertEqual( self.__transm.proceed(["set", self.jailName, "delaction", action]), (0, None)) self.assertEqual( self.__transm.proceed( ["set", self.jailName, "delaction", "Doesn't exist"]), (0, None)) #NOTE: Should this return 1?
class FileService(object): """docstring for FileService""" def __init__(self, port, ds_host, ds_port): super(FileService, self).__init__() self.dir = '' self.port = port self.ds_host = ds_host self.ds_port = ds_port self.server = Server(port, self._handler) def announce_server(self): sock = self.server.send_as_client(self.ds_host, self.ds_port, "INSERT " + self.server.host + ":" + str(self.port) + " " + "1") data = sock.recv(1024) if data.decode('utf-8') != response200(): print("Error with Directory Server: ", data) sock.shutdown(socket.SHUT_WR) sock.close() #TODO Add to allow exsting files or remove def _open_handler(self, conn, path): filename = self.dir + path if not os.path.isfile(filename): self.server.error(conn, 404, response404()) else: f = open(filename, "rb") pdb.set_trace() self.server.send_file(conn, f) conn.shutdown(socket.SHUT_WR) conn.close() def _create_handler(self, conn, path): file_path = os.path.dirname(path) if not os.path.isdir(file_path): self.server.error(conn, 405, response405()) else: self.server.respond(conn, response200()) exists = os.path.isfile(self.dir + path) f = open(self.dir + path, "wb") conn.recv_file(f) f.close() self.server.respond(conn, response200()) # TODO: send to other replication managers # if not exists: # def _mkdir_handler(self, conn, path): newdir = str(self.dir + path.strip('/')) basedir = os.path.dirname(newdir) if not os.path.isdir(basedir): self.server.error(conn, 404, response404()) else: try: os.makedirs(newdir) self.server.respond(conn, response200()) # self._advertise_buffer.add(path.strip('/'), ObjectBuffer.Type.directory) # TODO: send to replication manager except Exception as e: if e.errno != errno.EEXIST: raise else: conn.send() def _delete_handler(self, conn, path): pdb.set_trace() obj = str(self.dir + path).rstrip('/') if os.path.isdir(obj): shutil.rmtree(obj) self.server.respond(conn, response200()) # self._advertise_buffer.add(path, ObjectBuffer.Type.deleteDirectory) elif os.path.isfile(obj): os.remove(obj) self.server.respond(conn, response200()) # self._advertise_buffer.add(path, ObjectBuffer.Type.deleteFile) else: self.server.error(conn, 404, "not found!") def _handler(self, sock): LF = "\n" try: data = sock.recv(1024) msg = data.decode('utf-8') msg = msg.replace('$', ' ') if 'KILL_SERVICE' in msg: exit() elif msg[:4] == "HELO": response = ["IP:" + self.server.host, "Port:" + str(self.port), "StudentID:" + self.server.studentId] fullResponse = msg + LF.join(response) print(response) sock.send(fullResponse.encode('utf-8')) elif msg[:4] == "OPEN": temp = msg.split() self._open_handler(sock, temp[1]) elif msg[:6] == "CREATE": temp = msg.split() self._create_handler(sock, temp[1]) elif msg[:5] == "MKDIR": temp = msg.split() self._mkdir_handler(sock, temp[1]) elif msg[:6] == "DELETE": temp = msg.split() self._delete_handler(sock, temp[1]) else: self.server.error(sock, 0, responseErrParse()) except sock.error as e: err = e.args[0] self.server.error(sock, 0, e.args[1])
class Transmitter(unittest.TestCase): def setUp(self): """Call before every test case.""" self.__server = Server() self.__server.setLogLevel(0) self.__server.start(False) def tearDown(self): """Call after every test case.""" self.__server.quit() def testSetActionOK(self): name = "TestCase" cmdList = [["add", name], ["set", name, "actionstart", "Action Start"], ["set", name, "actionstop", "Action Stop"], ["set", name, "actioncheck", "Action Check"], ["set", name, "actionban", "Action Ban"], ["set", name, "actionunban", "Action Unban"], ["quit"]] outList = [(0, name), (0, 'Action Start'), (0, 'Action Stop'), (0, 'Action Check'), (0, 'Action Ban'), (0, 'Action Unban'), (0, None)] cnt = 0 for cmd in cmdList: self.assertEqual(self.__server.transm.proceed(cmd), outList[cnt]) cnt += 1 def testSetActionNOK(self): name = "TestCase" cmdList = [["addd", name], ["set", name, "test"], ["prout prout", "Stop"], ["fail2ban", "sucks"], ["set"], ["_/&%", "@*+%&"], [" quit"]] outList = [1, 1, 1, 1, 1, 1, 1] cnt = 0 for cmd in cmdList: msg = self.__server.transm.proceed(cmd) self.assertEqual(msg[0], outList[cnt]) cnt += 1 def testJail(self): name = "TestCase" cmdList = [["add", name], ["set", name, "logpath", "testcases/files/testcase01.log"], ["set", name, "timeregex", "\S{3}\s{1,2}\d{1,2} \d{2}:\d{2}:\d{2}"], ["set", name, "timepattern", "%b %d %H:%M:%S"], ["set", name, "failregex", "Authentication failure"], ["start", name], ["stop", name], ["quit"]] for cmd in cmdList: self.__server.transm.proceed(cmd) if cmd == ["start", name]: time.sleep(2) jail = self.__server.jails[name] self.assertEqual(jail.getFilter().failManager.size(), 0) self.assertEqual(jail.getAction().banManager.size(), 2)
def tearDownClass(cls): Server.stop()
def setUp(self): """Call before every test case.""" self.__server = Server() self.__server.setLogLevel(0) self.__server.start(False)
def serverStartWithParams(): Server.start()
if __name__ == "__main__": # config CFG = ConfigParser.ConfigParser() config.load(CFG) if CFG.has_section('all') and CFG.has_option('all', 'pythonpath'): pypath = CFG.get('all', 'pythonpath').split(':'); sys.path.extend(pypath) # additional modules #from server.singleProcess import singleProcess #from server.requestHandler import requestHandler #from server.watchdog import watchdog from server.server import Server #from server import utils try: s = Server(CFG) s.run() #only the main thread can catch signals. # Ctrl+C input except KeyboardInterrupt: s.log("Server instructed to shutdown by keyboard.") s.terminate() finally: try: s.server.close() except Exception: pass
#!/usr/bin/env python3 from server.server import Server try: Server.start() except KeyboardInterrupt: Server.stop()
class DirectoryService(Database): DATABASE = "sql.db" def __init__(self, port, replication): super(DirectoryService, self).__init__() self.port = port self.replication = replication self._create_tables() self.server = Server(port, self._handler) def _insert_server(self, sock, master, host, port): exists = self._server_exists(host, port) if exists: return True else: self.execute_sql("insert into servers (master, host, port) values (?, ?, ?)", (master, host, port), exclusive=True) return True def _delete_server(self, sock, host, port): self.execute_sql("delete from servers where host=? and port=?", (host, port)) def _list_servers(self, sock): # return (pickle.dumps(self.fetch_sql("select * from servers", ())), "PADDING") pass def _server_exists(self, host, port): return len(self.fetch_sql("select * from servers where host=? and port=?", (host, port))) != 0 def _select_random_server(self): return self.fetch_sql("select * from servers where id >= (abs(random()) % (SELECT max(id) FROM servers));", ()) def _select_server_by_id(self, id): return self.fetch_sql("select * from servers where id=?", (str(id),)) def _select_server_master(self, host, port): return self.fetch_sql("select id from servers where host=? and port=?", (host, port)) def _find_directory_host(self, file_name): return self.fetch_sql("select * from directories where file=?", (file_name,)) def _insert_directory_host(self, directory, file_name, server_id): self.execute_sql("insert into directories (directory, file, server_id) values (?, ?, ?)", (directory, file_name, str(server_id))) def _create_tables(self): conn = sqlite3.connect(self.DATABASE) cur = conn.cursor() cur.executescript(""" create table if not exists servers( id INTEGER PRIMARY KEY ASC, master INTEGER, host TEXT, port TEXT ); create table if not exists directories( id INTEGER PRIMARY KEY ASC, directory TEXT, file TEXT, server_id TEXT );""") def _handler(self, sock): try: data = sock.recv(1024) msg = data.decode('utf-8') msg = msg.replace('$', ' ') if 'KILL_SERVICE' in msg: exit() elif msg[:6] == "INSERT": temp = msg.split() ip_port = temp[1].split(':') master = temp[2] self._insert_server(sock, master, ip_port[0], ip_port[1]) self.server.respond(sock, response200()) elif msg[:4] == "DELETE": temp = msg.split() ip_port = temp[1].split(':') self._delete_server(sock, ip_port[0], ip_port[1]) self.server.respond(sock, response200()) elif msg[:3] == "GET": temp = msg.split() file_name = temp[1] row = self._select_random_server() file_dir = self._find_directory_host(file_name) pdb.set_trace() if file_dir is not None: response = response200() + " " + row[0][2] + ":" + row[0][3] + " " + file_dir[0][1] + " " + str(file_dir[0][0]) self.server.respond(sock, response) else: self.server.respond(sock, response404()) elif msg[:4] == "LIST": # TODO not complete, for replication temp = msg.split() #self._mkdir_handler(sock, temp[1]) elif msg[:3] == "ADD": temp = msg.split() ip_port = temp[1].split(':') server_ids = self._select_server_master(ip_port[0], ip_port[1]) server_id = server_ids[0] self._insert_directory_host(temp[2], temp[3], server_id[0]) else: self.server.error(sock, 0, responseErrParse()) except sock.error as e: err = e.args[0] self.server.error(sock, 0, e.args[1])
def serverStartWithParams(): Server.start(config_path='framework_unittests/configs/config_no_vlan')