Exemple #1
0
    def test_send(self, SenderMockWebSocket, ReceiverMockWebSocket):
        # Set up some clients
        sender = Client(SenderMockWebSocket)
        receiver = Client(ReceiverMockWebSocket)
        message_handler = MessageHandler()
        message_handler.add_client(sender)
        message_handler.add_client(receiver)
        self.assertTrue(len(message_handler.clients) == 2)

        # It sends a message to receiver
        sample_message = Message(receiver_guid=receiver.address,
                                 type="announce",
                                 data="data")
        asyncio.get_event_loop().run_until_complete(
            message_handler.send(sender, sample_message))
        # It called send
        ReceiverMockWebSocket.send.assert_called()

        # It sent the right message
        sent_message = Message.from_json(
            ReceiverMockWebSocket.send.call_args[0][0])
        sent_data = sent_message.data
        sent_type = sent_message.type
        self.assertEqual(sent_data, sample_message.data)
        self.assertEqual(sent_type, sample_message.type)

        # It set the "sender_guid"
        sent_from = sent_message.sender_guid
        self.assertEqual(sent_from, sender.address)
Exemple #2
0
    def test_broadcast(self, SenderMockWebSocket, Receiver1MockWebSocket, Receiver2MockWebSocket):
        # Set up some clients
        sender = Client(SenderMockWebSocket)
        receiver1 = Client(Receiver1MockWebSocket)
        receiver2 = Client(Receiver2MockWebSocket)
        channel = Channel("test_channel")
        channel.add_client(sender)
        channel.add_client(receiver1)
        channel.add_client(receiver2)
        self.assertTrue(len(channel.clients) == 3)

        # It sends a message to each client
        sample_message = Message(
                type = "announce",
                data= "data"
                )
        asyncio.get_event_loop().run_until_complete(
                channel.broadcast(sender, sample_message)
        )
        # It called send
        Receiver1MockWebSocket.send.assert_called()
        Receiver2MockWebSocket.send.assert_called()

        # It sent the right message
        sent_message = Message.from_json(Receiver1MockWebSocket.send.call_args[0][0])
        sent_data = sent_message.data
        sent_type = sent_message.type
        sent_channel_id = sent_message.channel_id
        self.assertEqual(sent_data, sample_message.data)
        self.assertEqual(sent_type, sample_message.type)
        self.assertEqual(sent_channel_id, channel.id)

        # It set the "sender_guid"
        sent_from = sent_message.sender_guid
        self.assertEqual(sent_from, sender.address)
Exemple #3
0
    def run(self):
        dc = DataCollector(self.config.getint('data', 'number-samples'),
                           self.config.getint('general', 'n-gestures'))
        c = Client()
        c.read_stream(self.config.get('openbci', 'stream-type'),
                      dc.data_reader)
        dp = DataPreprocessor()

        var_frame_size = self.config.getint('ml', 'var-frame-size')

        for i in range(self.config.getint('openbci', 'n-channels')):
            dp.running_var(dc.data_x, i, var_frame_size)

        # for i in range(self.config.getint('openbci','n-channels')):
        # 	dp.running_var(dc.data_x, i, 10)

        for i in range(self.config.getint('openbci', 'n-channels')):
            dp.running_var(dc.data_x, i, 20)

        for i in range(self.config.getint('openbci', 'n-channels')):
            dp.running_var(dc.data_x, i, 30)

        for i in range(self.config.getint('openbci', 'n-channels')):
            dp.running_var(dc.data_x, i, 50)

        for i in range(self.config.getint('openbci', 'n-channels')):
            dp.running_var(dc.data_x, i, 70)

        dc.save_data("{}.bin".format(self.env_path + "training/" +
                                     self.options['--outfile']))
Exemple #4
0
 def __init__(self, userName, startDir, autohostFactory, password, map_file,
              mod_file, engineName, engineVersion, roomName, gameName,
              battlePort, autohostCTLClient):
     threading.Thread.__init__(self)
     self.autohost = autohostFactory
     self.username = autohostFactory.new_autohost()
     self.hostedby = userName
     print(colored('[INFO]', 'green'),
           colored(self.username + ': Autohost account received!', 'white'))
     self.password = password
     self.map_file = map_file
     self.mod_file = mod_file
     self.engineName = engineName
     self.engineVersion = engineVersion
     self.roomName = roomName
     self.gameName = gameName
     self.engineToken = ''.join(
         random.choices(string.ascii_uppercase + string.digits, k=6))
     self.battlePort = battlePort
     self.startDir = startDir
     self.autohostCTL = autohostCTLClient
     self.client = Client(self.battlePort, self.startDir)
     self.unitSync = UnitSync(self.startDir,
                              self.startDir + '/engine/libunitsync.so',
                              self.username)
     self.isLaunched = False
     self.server = ServerLauncher()
     self.hosterMem = {}
     self.deliver = queue.Queue()
Exemple #5
0
    def test_remove_client(self):
        # It removes the client
        remaining_client = Client(websockets.protocol.WebSocketCommonProtocol(host="1"))
        removed_client = Client(websockets.protocol.WebSocketCommonProtocol(host="2"))
        channel = Channel("test-channel")
        channel.add_client(remaining_client)
        channel.add_client(removed_client)
        self.assertTrue(len(channel.clients) == 2)
        
        # It removes a client
        channel.remove_client(removed_client)
        self.assertTrue(len(channel.clients) == 1)

        # It removes the right client
        actual_remaining_client = channel.clients.pop()
        self.assertEqual(remaining_client, actual_remaining_client)
Exemple #6
0
    def __init__(path, rhost, rport, bid):
        self.path = path

        self.client = Client(rhost, rport, bytes(bid, 'utf8'))
        self.client.SetLinkDropHandler(lambda: SimpleBackup.LinkDrop(self))
        self.cs = ChunkPushPullSystem(client, load=False)
        self.ValidCheck()
 def test_init(self):
     # It sets the websocket and address
     sample_websocket = websockets.protocol.WebSocketCommonProtocol(
         host="1")
     client = Client(sample_websocket)
     self.assertEqual(client.websocket, sample_websocket)
     self.assertTrue(uuid.UUID(client.address) != "")
Exemple #8
0
    def get_client(args, config):
        """Return instantiated client.

        Config will provide global overrides.
        """
        client = Client()
        date = datetime.strftime(datetime.today(), "%Y_%m_%dT%H_%M_%S_%f")

        client.ip = args.remote
        client.jobname = "{0}-{1}-worker".format(client.ip, date)

        if args.user is not None:
            client.user = args.user
            client.is_sudoer = True

        if args.delay_pickup:
            client.delay_pickup = True

        if not config['DEFAULT']['output']:
            if args.output is not None:
                client.output = args.output

        if not config['DEFAULT']['compress']:
            if args.dont_compress:
                client.compress = not client.compress

        cprint(
            "> Establishing secure connection {0}@{1}".format(
                client.user, client.ip), 'blue')
        client.pass_ = getpass.getpass()

        return client
 def setUp(self):
     self.target_channel = Channel("target_channel")
     self.target_channel.broadcast = unittest.mock.AsyncMock()
     self.target_channel.send = unittest.mock.AsyncMock()
     self.another_channel = Channel("another_channel")
     self.channels = {
         self.target_channel.id: self.target_channel,
         self.another_channel.id: self.another_channel
     }
     self.sender_mock_socket = MockAsyncIterator(iter([]))
     self.sender = Client(self.sender_mock_socket)
     self.receiver_mock_socket = MockAsyncIterator(iter([]))
     self.receiver = Client(self.receiver_mock_socket)
     self.sample_message = Message(self.sender.address,
                                   self.receiver.address,
                                   self.target_channel.id, "")
Exemple #10
0
    def api(self) -> Client:
        if self._api is None:
            self._api = Client(
                urljoin(request.url_root,
                        self.cfg.get("api", "Address", fallback="/")))

        return self._api
Exemple #11
0
    def test_add_client(self):
        # It adds the client and assigns a uuid address
        test_client = Client(websockets.protocol.WebSocketCommonProtocol())
        message_handler = MessageHandler()
        message_handler.add_client(test_client)
        self.assertTrue(len(message_handler.clients) == 1)

        client = message_handler.clients.pop()
        self.assertEqual(client, test_client)
Exemple #12
0
    def test_remove_client(self):
        # It removes the client
        remaining_client = Client(
            websockets.protocol.WebSocketCommonProtocol(host="1"))
        removed_client = Client(
            websockets.protocol.WebSocketCommonProtocol(host="2"))
        message_handler = MessageHandler()
        message_handler.add_client(remaining_client)
        message_handler.add_client(removed_client)
        self.assertTrue(len(message_handler.clients) == 2)

        # It removes a client
        message_handler.remove_client(removed_client)
        self.assertTrue(len(message_handler.clients) == 1)

        # It removes the right client
        actual_remaining_client = message_handler.clients.pop()
        self.assertEqual(remaining_client, actual_remaining_client)
Exemple #13
0
    def test_add_client(self):
        # It adds the client and assigns a uuid address
        test_client = Client(websockets.protocol.WebSocketCommonProtocol())
        channel = Channel("test-channel")
        channel.add_client(test_client)
        self.assertTrue(len(channel.clients) == 1)

        client = channel.clients.pop()
        self.assertEqual(client, test_client)
Exemple #14
0
 def setup(self):
     self.logger = self._sensor_service.get_logger(__name__)
     self.api_url = self._config[
         'api_url'] + '/events?queue=state_change&types=StateChange'
     self.api_user = self._config['api_state_change_user']
     self.api_password = self._config['api_state_change_password']
     self.trigger_name = 'event.state_change'
     self.trigger_pack = 'icinga2'
     self.trigger_ref = '.'.join([self.trigger_pack, self.trigger_name])
     self.client = Client(self, self.api_url, self.api_user,
                          self.api_password)
     self.logger.info(
         'Icinga2StateChangeSensor initialized with URL: %s User: %s Password: *****',
         self.api_url, self.api_user)
Exemple #15
0
def tweet_list(args):
    if path.isfile(args.tweet_list):
        with open(args.tweet_list, 'r') as f:
            tweets = f.readlines()
        twit = Client(args.config_path, log=False)
        for tweet in tweets:
            stripped = tweet.strip()
            print("[*] Tweeting: %s" % stripped)
            twit.tweet(stripped)
            print("[*] Sleeping for %s seconds" % str(args.int_sleep))
            sleep(args.int_sleep)

    else:
        exit("No such file: %s" % args.tweet_list)
Exemple #16
0
def main():
    with open("./users.json", mode="r") as f:
        data = f.read()

    client = Client(data)

    user = client.get_user(FilterMethods.USERNAME, 'arthurdw')

    if not user:
        print("Couldn't find a user with the username 'arthurdw'")
        return

    print(f"Arthurdw github: {user.github}")

    print('C# programmers: ' +
          ', '.join(user.name for user in client.get_users_by_language('c#')))
Exemple #17
0
    def run(self):
        if self.options['run-tests']:
            data = self.load("{}.bin".format(self.env_path + "training/" +
                                             self.options['--infile']))
            data = self.discard_transition_samples(data[0], data[1])
            data = list(zip(data[0], data[1]))
            shuffle(data)

            data_x = [r[0][:2] + r[0][-2:] for r in data]
            data_y = [r[1] for r in data]

            for key, c in self.classifiers.items():
                print '============'
                print 'Class.: {}'.format(key)
                c.fit(data_x[:-500], data_y[:-500])
                print 'Score: ' + str(c.score(data_x[-500:], data_y[-500:]))
                print '============\n'

        elif self.options['train']:
            data = self.load("{}.bin".format(self.env_path + "training/" +
                                             self.options['--infile']))
            data = self.discard_transition_samples(data[0], data[1])
            data = list(zip(data[0], data[1]))
            shuffle(data)

            data_x = [r[0][:2] + r[0][-2:] for r in data]
            data_y = [r[1] for r in data]

            clf = self.classifiers[self.options['--clf']]
            clf.fit(data_x[:-500], data_y[:-500])
            joblib.dump(
                clf, "{}_{}.pkl".format(
                    self.env_path + "models/" + self.options['--infile'],
                    self.options['--clf']))
            print 'Saved clf with score: ' + str(
                clf.score(data_x[-500:], data_y[-500:]))

        elif self.options['run']:
            scikit_c = joblib.load("{}.pkl".format(self.env_path + "models/" +
                                                   self.options['--infile']))
            self.clf = SeqClassifier(scikit_c)
            self.window = SimpleWindow(self.q)
            c = Client()
            c.read_stream(self.config.get('openbci', 'stream-type'),
                          self.data_reader)
Exemple #18
0
 def __listen_for_connections(self):
     self.__socket.listen(self.__MAX_HOSTS)
     print('Server initialized')
     while not self.__check_stop():
         try:
             self.__socket.settimeout(1)
             connection, address = self.__socket.accept()
             client = Client(connection, address,
                             self.__requests_dictionary, self.__MAX_PACKAGE,
                             self)
             client.start()
             self.__connected_clients.append(client)
         except socket.timeout:
             pass
         except Exception as e:
             print("unexpected error server will be stopped" + str(e))
             self.__stopped = True
     self.__close_server()
Exemple #19
0
async def dispatch(websocket, path):
    client = Client(websocket)
    print("New client: " + client.address, flush=True)
    try:
        async for json_message in websocket:
            message = Message.from_json(json_message)
            if (message.type == "announce"):
                message_handler.add_client(client)
                await message_handler.broadcast(client, message)
            elif (message.type in ["offer","answer","ice"]):
                await message_handler.send(client, message)
    finally:
        message_handler.remove_client(client)

        # broadcast the departure
        message = Message(type = "hangup")
        await message_handler.broadcast(client, message)
        print("Client " + client.address + " left", flush=True)
Exemple #20
0
def main():
    print('--- Data Miner by Trivernis ---')
    print('(Please respect the api providers)')
    print('-------------------------------')
    args = parse_arguments()
    interval = parse_duration(args.interval)
    if interval.total_seconds() == 0:
        print(
            "[-] The interval must be greater than one second (this is not a dos tool)."
        )
        exit(1)
    if args.tor:
        client = TorClient(password=args.tor_password)
    else:
        client = Client()
    if not os.path.exists(args.output_dir):
        os.mkdir(args.output_dir)
    mapping = {}
    mapping_file = '%s/mapping.json' % args.output_dir
    if os.path.exists(mapping_file):
        with open(mapping_file, 'r') as mf:
            try:
                mapping = json.load(mf)
            except Exception as e:
                print(e)
    dirs = []
    for url in args.url:
        folder_name = get_folder_name(url)
        mapping[url] = folder_name
        dirs.append(folder_name)
    with open(mapping_file, 'w') as mf:
        json.dump(mapping, mf, indent='  ')
    body = None
    if args.body:
        body = open(args.body, 'rb')
    fm = FileManager(args.output_dir, dirs, compress=args.compress)
    print('[ ] Starting request loop...')
    request_loop(client,
                 args.url,
                 fm,
                 args.method,
                 not args.no_verify,
                 int(interval.total_seconds()),
                 body=body)
Exemple #21
0
    def run(self):

        print(colored('[INFO]', 'green'),
              colored(self.username + ': Loading unitsync.', 'white'))
        unitSync = UnitSync(self.startDir + '/engine/libunitsync.so')
        unitSync.startHeshThread(self.map_file, self.mod_file)
        unit_sync = unitSync.getResult()

        client = Client(self.battlePort, self.startDir)

        client.login(self.username, self.password)

        print(colored('[INFO]', 'green'),
              colored(self.username + ': Logging in', 'white'))
        client.clearBuffer(self.username)

        client.openBattle(0, 0, '*', self.battlePort, 5, unit_sync['modHesh'],
                          1, unit_sync['mapHesh'], self.engineName,
                          self.engineVersion, self.mapName, self.roomName,
                          self.gameName)
        print(colored('[INFO]', 'green'),
              colored(self.username + ': Opening Battle.', 'white'))
        client.clearBuffer(self.username)

        _thread.start_new_thread(client.keepalive, (self.username, ))
        client.clearBuffer(self.username)

        client.joinChat('bus')
        print(colored('[INFO]', 'green'),
              colored(self.username + ': Joining Battle Chat.', 'white'))
        client.clearBuffer(self.username)

        #try:
        #_thread.start_new_thread( client.observeChat,(self.username,))
        print(
            colored('[INFO]', 'green'),
            colored(self.username + ': Monitoring Battle Chat.(idling)',
                    'white'))
        #except:
        #	print ("Error: unable to observe chat")

        return
Exemple #22
0
 def __init__(self, config_path, log=True):
     self.config_path = config_path
     self.client = Client(config_path, log=log)
     self.reload_config()
     self.tweets = self.load_tweets()
     self.running = True
     self.start_time = datetime.now()
     self.total_retweets = 0
     self.total_tweets = 0
     self.total_follows = 0
     self.total_favourites = 0
     self.uptime = ''
     self.last_tweet_user = ''
     self.last_tweet_text = ''
     self.log_handler = self.LogHandler()
     signal(SIGUSR1, self.catch_signal)
     if log:
         logging.basicConfig(level=self.client.log_level,
             format='[%(levelname)s] (%(threadName)-10s) %(message)s'
         )
Exemple #23
0
    async def handle_connection(self, websocket: iter, path: str):
        client = Client(websocket)
        print("New client: " + client.address, flush=True)
        channel = None
        try:
            async for json_message in websocket:
                message = Message.from_json(json_message)
                if message.type == "create_channel":
                    new_channel = Channel(message.data["name"])
                    self.channels[new_channel.id] = new_channel
                    ack = Message(type="ack",
                                  data={"channel_id": new_channel.id})
                    await websocket.send(ack.to_json())
                    continue

                channel_id = message.channel_id
                if channel_id in self.channels.keys():
                    channel = self.channels[channel_id]
                else:
                    message = Message(type="error")
                    await websocket.send(message.to_json())
                    await websocket.close()
                    break

                if (message.type in ["announce"]):
                    channel.add_client(client)
                    await channel.broadcast(client, message)
                elif (message.type in ["offer", "answer", "ice"]):
                    await channel.send(client, message)
                elif (message.type in ["name"]):
                    await channel.broadcast(client, message)

        finally:
            if channel:
                channel.remove_client(client)

                # broadcast the departure
                message = Message(type="hangup")
                await channel.broadcast(client, message)
            print("Client " + client.address + " left", flush=True)
Exemple #24
0
import time
import urllib2
from lib.client import Client, get_api_path

client = Client(access_key='', secret_key='')

#demo of GET APIs

#get member info
print client.get(get_api_path('members'))

#get markets
markets =  client.get(get_api_path('markets'))
print "markets:", markets

#get tickers of each market
#market should be specified in url
print 
print "tickers in markets"
for market in markets:
    print client.get(get_api_path('tickers') % market['id'])

#get orders of each market
#market should be specified in params
print 
print "orders in markets"
for market in markets:
    print client.get(get_api_path('orders'), {'market': market['id']})

#get order book
print client.get(get_api_path('order_book'), params={'market': 'btccny'})
Exemple #25
0
def user_lookup(args):
    twit = Client(args.config_path, log=True)
    twit.dump_stats(user=args.user_lookup)
    twit.extended_stats(user=args.user_lookup)
Exemple #26
0
def get_my_stats(args):
    twit = Client(args.config_path, log=True)
    twit.dump_stats()
Exemple #27
0
def retweet(args):
    twit = Client(args.config_path, log=True)
    twit.retweet_loop(args.retweet_search)
Exemple #28
0
def tweet(args):
    twit = Client(args.config_path, log=True)
    stripped = args.tweet.strip()
    print("[*] Tweeting: %s" % stripped)
    twit.tweet(stripped)
Exemple #29
0
 def __init__(self, config_path, seed):
     self.seed = seed
     self.client = Client(config_path, log=True)
     self.client.dump_stats(user=self.seed)
     self.client.extended_stats(user=self.seed)
     self.pp = pprint.PrettyPrinter(depth=6)
import time
from lib.client import Client, get_api_path

client = Client(access_key='w1xbUls4U3siMljMx6RLjQTvFNTJ4C3BY0Hw8f5D',
                secret_key='aDJy2NWOIyj5UiDlxXxXpcmxlW9xXQnGzT9IKfhH')

#demo of GET APIs

#get member info
print(client.get(get_api_path('members')))

#get markets
markets = client.get(get_api_path('markets'))
print("markets:", markets)

#get tickers of each market
#market should be specified in url

print("tickers in markets")
for market in markets:
    print(client.get(get_api_path('tickers') % market['id']))

#get orders of each market
#market should be specified in params

print("orders in markets")
for market in markets:
    print(client.get(get_api_path('orders'), {'market': market['id']}))

#get order book
print(client.get(get_api_path('order_book'), params={'market': 'btccny'}))