コード例 #1
0
def _configure_server(restarting=False):
    global websocket_plugin

    # Configure server error log
    cherrypy.config.update({'log.error_file': 'cherrypy.error.log'})

    # Configure server url
    cherrypy.config.update({'server.socket_host': utils.s2n(autosubliminal.WEBSERVERIP),
                            'server.socket_port': int(autosubliminal.WEBSERVERPORT)
                            })

    # Disable engine plugins (no need for autoreload plugin)
    cherrypy.config.update({'engine.autoreload.on': False})

    # Configure authentication in if a username and password is set by the user
    if autosubliminal.USERNAME and autosubliminal.PASSWORD:
        users = {utils.s2n(autosubliminal.USERNAME): utils.s2n(autosubliminal.PASSWORD)}
        cherrypy.config.update({'tools.auth_digest.on': True,
                                'tools.auth_digest.realm': 'Auto-Subliminal website',
                                'tools.auth_digest.get_ha1': auth_digest.get_ha1_dict_plain(users),
                                'tools.auth_digest.key': 'yek.tsegid_htua.lanimilbuS-otuA'  # Can be any random string
                                })

    if not restarting:
        # Enable websocket plugin
        websocket_plugin = WebSocketPlugin(cherrypy.engine)
        websocket_plugin.subscribe()
        cherrypy.tools.websocket = WebSocketTool()
    else:
        # When restarting we need to create a new websocket manager thread (you cannot start the same thread twice!)
        websocket_plugin.manager = WebSocketManager()
        # When restarting we need to clear the httpserver to force the creation of a new one (needed for ip/port change)
        cherrypy.server.httpserver = None
コード例 #2
0
    def test_websocket_close_all(self, MockSelectPoller):
        m = WebSocketManager(poller=MockSelectPoller())

        ws = MagicMock()
        m.add(ws)
        m.close_all()
        ws.terminate.assert_call_once_with(1001, 'Server is shutting down')
コード例 #3
0
    def init(self, interface):
        """ Initializes the PB WS Client"""

        self.logger = logging.getLogger('PAI').getChild(__name__)
        self.pb = Pushbullet(cfg.PUSHBULLET_KEY, cfg.PUSHBULLET_SECRET)
        self.manager = WebSocketManager()
        self.alarm = None
        self.interface = interface
コード例 #4
0
    def test_websocket_close_all(self, MockSelectPoller):
        m = WebSocketManager(poller=MockSelectPoller())

        ws = MagicMock()
        m.add(ws)
        m.close_all()
        ws.close.assert_called_once_with(code=1001,
                                         reason='Server is shutting down')
コード例 #5
0
 def initialize_websockets_manager(self):
     """
     Call thos to start the underlying websockets
     manager. Make sure to call it once your server
     is created.
     """
     self.manager = WebSocketManager()
     self.manager.start()
コード例 #6
0
    def test_broadcast(self, MockSelectPoller):
        m = WebSocketManager(poller=MockSelectPoller())

        ws = MagicMock()
        ws.terminated = False
        m.add(ws)

        m.broadcast(b'hello there')
        ws.send.assert_called_once_with(b'hello there', False)
コード例 #7
0
    def test_mainloop_can_be_stopped_when_no_websocket_were_registered(
            self, MockSelectPoller):
        m = WebSocketManager(poller=MockSelectPoller())
        self.assertFalse(m.running)

        m.start()
        self.assertTrue(m.running)

        m.stop()
        self.assertFalse(m.running)
コード例 #8
0
    def test_cannot_add_websocket_more_than_once(self, MockSelectPoller):
        m = WebSocketManager(poller=MockSelectPoller())

        ws = MagicMock()
        ws.sock.fileno.return_value = 1

        m.add(ws)
        self.assertEqual(len(m), 1)

        m.add(ws)
        self.assertEqual(len(m), 1)
コード例 #9
0
    def test_add_and_remove_websocket(self, MockSelectPoller):
        m = WebSocketManager(poller=MockSelectPoller())

        ws = MagicMock()
        ws.sock.fileno.return_value = 1

        m.add(ws)
        m.poller.register.assert_called_once_with(1)

        m.remove(ws)
        m.poller.unregister.assert_called_once_with(1)
コード例 #10
0
    def test_broadcast_failure_must_not_break_caller(self, MockSelectPoller):
        m = WebSocketManager(poller=MockSelectPoller())

        ws = MagicMock()
        ws.terminated = False
        ws.send.side_effect = RuntimeError
        m.add(ws)

        try:
            m.broadcast(b'hello there')
        except:
            self.fail("Broadcasting shouldn't have failed")
コード例 #11
0
    def test_mainloop_can_be_stopped(self, MockSelectPoller):
        m = WebSocketManager(poller=MockSelectPoller())

        def poll():
            yield 1
            m.stop()
            yield 2

        m.poller.poll.return_value = poll()
        self.assertFalse(m.running)

        m.start()
        # just make sure it had the time to finish
        time.sleep(0.1)
        self.assertFalse(m.running)
コード例 #12
0
def _configure_server(restarting=False):
    global websocket_plugin

    # Configure server error log
    cherrypy.config.update({'log.error_file': 'cherrypy.error.log'})

    # Configure server url
    cherrypy.config.update({
        'server.socket_host':
        s2n(autosubliminal.WEBSERVERIP),
        'server.socket_port':
        int(autosubliminal.WEBSERVERPORT)
    })

    # Disable engine plugins (no need for autoreload plugin)
    cherrypy.config.update({'engine.autoreload.on': False})

    # Read and store cherrypy server version (if not set, it returns CherryPy/Unknown because it's not installed)
    server_header = 'CherryPy/%s' % get_library_version('cherrypy')
    cherrypy.config.update({'response.headers.server': server_header})

    # Configure authentication in if a username and password is set by the user
    if autosubliminal.USERNAME and autosubliminal.PASSWORD:
        users = {s2n(autosubliminal.USERNAME): s2n(autosubliminal.PASSWORD)}
        cherrypy.config.update({
            'tools.auth_digest.on':
            True,
            'tools.auth_digest.realm':
            'Auto-Subliminal website',
            'tools.auth_digest.get_ha1':
            auth_digest.get_ha1_dict_plain(users),
            'tools.auth_digest.key':
            'yek.tsegid_htua.lanimilbuS-otuA'  # Can be any random string
        })

    # Configure our custom json_out_handler (Uncomment if it should be used for any @cherrypy.tools.json_out())
    # cherrypy.config.update({'tools.json_out.handler': json_out_handler})

    if not restarting:
        # Enable websocket plugin
        websocket_plugin = WebSocketPlugin(cherrypy.engine)
        websocket_plugin.subscribe()
        cherrypy.tools.websocket = WebSocketTool()
    else:
        # When restarting we need to create a new websocket manager thread (you cannot start the same thread twice!)
        websocket_plugin.manager = WebSocketManager()
        # When restarting we need to clear the httpserver to force the creation of a new one (needed for ip/port change)
        cherrypy.server.httpserver = None
コード例 #13
0
    def test_websocket_terminated_from_mainloop(self, MockSelectPoller):
        m = WebSocketManager(poller=MockSelectPoller())
        m.poller.poll.return_value = [1]

        ws = MagicMock()

        ws.terminated = False
        ws.sock.fileno.return_value = 1
        ws.once.return_value = False

        m.add(ws)
        m.start()

        ws.terminate.assert_call_once_with()

        m.stop()
コード例 #14
0
ファイル: container.py プロジェクト: overquota/pylxd
    def execute(self, commands, environment={}):
        """Execute a command on the container.

        In pylxd 2.2, this method will be renamed `execute` and the existing
        `execute` method removed.
        """
        if not _ws4py_installed:
            raise ValueError(
                'This feature requires the optional ws4py library.')
        if isinstance(commands, six.string_types):
            raise TypeError("First argument must be a list.")
        response = self.api['exec'].post(
            json={
                'command': commands,
                'environment': environment,
                'wait-for-websocket': True,
                'interactive': False,
            })

        fds = response.json()['metadata']['metadata']['fds']
        operation_id = response.json()['operation'].split('/')[-1]
        parsed = parse.urlparse(
            self.client.api.operations[operation_id].websocket._api_endpoint)

        with managers.web_socket_manager(WebSocketManager()) as manager:
            stdin = _StdinWebsocket(self.client.websocket_url)
            stdin.resource = '{}?secret={}'.format(parsed.path, fds['0'])
            stdin.connect()
            stdout = _CommandWebsocketClient(manager,
                                             self.client.websocket_url)
            stdout.resource = '{}?secret={}'.format(parsed.path, fds['1'])
            stdout.connect()
            stderr = _CommandWebsocketClient(manager,
                                             self.client.websocket_url)
            stderr.resource = '{}?secret={}'.format(parsed.path, fds['2'])
            stderr.connect()

            manager.start()

            while len(manager.websockets.values()) > 0:
                time.sleep(.1)

            operation = self.client.operations.get(operation_id)

            return _ContainerExecuteResult(operation.metadata['return'],
                                           stdout.data, stderr.data)
コード例 #15
0
ファイル: pushbullet.py プロジェクト: lsta/pai
    def __init__(self, interface, url):
        """ Initializes the PB WS Client"""
        super().__init__(url)

        self.pb = Pushbullet(cfg.PUSHBULLET_KEY)
        self.manager = WebSocketManager()
        self.interface = interface

        self.device = None
        for i, device in enumerate(self.pb.devices):
            if device.nickname == 'pai':
                logger.debug("Device found")
                self.device = device
                break
        else:
            logger.exception("Device not found. Creating 'pai' device")
            self.device = self.pb.new_device(nickname='pai', icon='system')
コード例 #16
0
def run(script_options):
    global logger
    level  = logging.DEBUG if script_options.verbose else logging.INFO
    logger = configure_logger(level = level)

    mgr = WebSocketManager()

    try:
        mgr.start()
        clients = []

        # Connect
        for connection_idx in range(script_options.concurrency):
            client = EchoClient(script_options.url, mgr,
                                script_options.ca, script_options.key, script_options.cert)
            client.connect()
            clients.append(client)

        logger.info("%d clients are connected" % (connection_idx + 1))

        # Send
        msg = getMessage(script_options)
        if msg:
            msg = json.write(msg)
            logger.info("Sending messages (num=%d):\n%s", script_options.num, msg)
            for client in clients:
                for _ in range(script_options.num):
                    client.send(msg)
                    time.sleep(SEND_INTERVAL)
            logger.info("Done sending")

        # Sleep before disconnecting
        logger.info("Sleeping for %d s before disconnecting",
                    script_options.interval)
        time.sleep(script_options.interval)

    except KeyboardInterrupt:
        logger.info("Interrupted by user")
    finally:
        logger.info("Disconnecting!")
        mgr.close_all(code    = 1000,
                      message = "Client is closing the connection")
        mgr.stop()
        mgr.join()
コード例 #17
0
ファイル: container.py プロジェクト: pombredanne/pylxd
    def execute(self, commands, environment={}):
        """Execute a command on the container."""
        if not _ws4py_installed:
            raise ValueError(
                'This feature requires the optional ws4py library.')
        if isinstance(commands, six.string_types):
            raise TypeError("First argument must be a list.")
        response = self.api['exec'].post(
            json={
                'command': commands,
                'environment': environment,
                'wait-for-websocket': True,
                'interactive': False,
            })

        fds = response.json()['metadata']['metadata']['fds']
        operation_id = response.json()['operation'].split('/')[-1]
        parsed = parse.urlparse(
            self.client.api.operations[operation_id].websocket._api_endpoint)

        manager = WebSocketManager()

        stdin = _StdinWebsocket(manager, self.client.websocket_url)
        stdin.resource = '{}?secret={}'.format(parsed.path, fds['0'])
        stdin.connect()
        stdout = _CommandWebsocketClient(manager, self.client.websocket_url)
        stdout.resource = '{}?secret={}'.format(parsed.path, fds['1'])
        stdout.connect()
        stderr = _CommandWebsocketClient(manager, self.client.websocket_url)
        stderr.resource = '{}?secret={}'.format(parsed.path, fds['2'])
        stderr.connect()

        manager.start()

        while True:  # pragma: no cover
            for websocket in manager.websockets.values():
                if not websocket.terminated:
                    break
            else:
                break
            time.sleep(1)

        return stdout.data, stderr.data
コード例 #18
0
    def test_cannot_remove_unregistered_websocket(self, MockSelectPoller):
        m = WebSocketManager(poller=MockSelectPoller())

        ws = MagicMock()
        ws.sock.fileno.return_value = 1

        m.remove(ws)
        self.assertEqual(len(m), 0)
        self.assertFalse(m.poller.unregister.called)

        m.add(ws)
        self.assertEqual(len(m), 1)
        m.remove(ws)
        self.assertEqual(len(m), 0)
        m.poller.unregister.assert_called_once_with(1)
        m.poller.reset_mock()

        m.remove(ws)
        self.assertEqual(len(m), 0)
        self.assertFalse(m.poller.unregister.called)
コード例 #19
0
ファイル: mfccuck.py プロジェクト: SpicyA/MBOT
from core.database import Database
from core.timezone import *
from core.cmn  import *
import time
import traceback
import datetime
import threading
import json
import sys
import os
import random
import re
import urllib
import math

main_session = WebSocketManager()
online_camgirls={}

def main_keep_alive(web_sock):
        print "Starting Keep alive for MFCCUCKmgr\n"
	while web_sock.is_running == 1:
		#s_time=(math.floor(10 * random.random()) + 10)
		time.sleep(5);
		try:
       			for ws in web_sock.websockets.itervalues():
				if not ws.terminated:
					ws.send("0 0 0 0 0 -\n\0")
		except:
			pass
def start_cuckmgr():
    main_session.start()
コード例 #20
0
import os
import sys
import time
import thread
import logging
import json
import boto3
import botocore
import pymongo
from sys import stdout
from pymongo import MongoClient
from sqs_client import SQSClient
from ws4py.client.threadedclient import WebSocketClient
from ws4py.manager import WebSocketManager

manager = WebSocketManager()
timeout = 60
logging.basicConfig(filename='queue.log', filemode='w', level=logging.INFO)

############################### Get the service resources
sqs = boto3.resource('sqs')
s3 = boto3.resource('s3')

bucketName = 'spreza-audio'
queueIn = 'transcribe'
queueOut = 'complete'
#URI = '54.205.82.96'
URI = '54.221.51.233'
ASR_URI = 'ws://' + URI + '/client/ws/speech'
STATUS_URI = 'ws://' + URI + '/client/ws/status'
baseS3 = 'https://' + bucketName + '.s3.amazonaws.com/'
コード例 #21
0
 def __init__(self, bus):
     plugins.SimplePlugin.__init__(self, bus)
     self.manager = WebSocketManager()
コード例 #22
0
ファイル: container.py プロジェクト: sahid/pylxd
    def execute(self,
                commands,
                environment={},
                encoding=None,
                decode=True,
                stdin_payload=None,
                stdin_encoding="utf-8",
                stdout_handler=None,
                stderr_handler=None):
        """Execute a command on the container.

        In pylxd 2.2, this method will be renamed `execute` and the existing
        `execute` method removed.

        :param commands: The command and arguments as a list of strings
        :type commands: [str]
        :param environment: The environment variables to pass with the command
        :type environment: {str: str}
        :param encoding: The encoding to use for stdout/stderr if the param
            decode is True.  If encoding is None, then no override is
            performed and whatever the existing encoding from LXD is used.
        :type encoding: str
        :param decode: Whether to decode the stdout/stderr or just return the
            raw buffers.
        :type decode: bool
        :param stdin_payload: Payload to pass via stdin
        :type stdin_payload: Can be a file, string, bytearray, generator or
            ws4py Message object
        :param stdin_encoding: Encoding to pass text to stdin (default utf-8)
        :param stdout_handler: Callable than receive as first parameter each
            message recived via stdout
        :type stdout_handler: Callable[[str], None]
        :param stderr_handler: Callable than receive as first parameter each
            message recived via stderr
        :type stderr_handler: Callable[[str], None]
        :raises ValueError: if the ws4py library is not installed.
        :returns: The return value, stdout and stdin
        :rtype: _ContainerExecuteResult() namedtuple
        """
        if not _ws4py_installed:
            raise ValueError(
                'This feature requires the optional ws4py library.')
        if isinstance(commands, six.string_types):
            raise TypeError("First argument must be a list.")
        response = self.api['exec'].post(
            json={
                'command': commands,
                'environment': environment,
                'wait-for-websocket': True,
                'interactive': False,
            })

        fds = response.json()['metadata']['metadata']['fds']
        operation_id = response.json()['operation'].split('/')[-1]
        parsed = parse.urlparse(
            self.client.api.operations[operation_id].websocket._api_endpoint)

        with managers.web_socket_manager(WebSocketManager()) as manager:
            stdin = _StdinWebsocket(self.client.websocket_url,
                                    payload=stdin_payload,
                                    encoding=stdin_encoding)
            stdin.resource = '{}?secret={}'.format(parsed.path, fds['0'])
            stdin.connect()
            stdout = _CommandWebsocketClient(manager,
                                             self.client.websocket_url,
                                             encoding=encoding,
                                             decode=decode,
                                             handler=stdout_handler)
            stdout.resource = '{}?secret={}'.format(parsed.path, fds['1'])
            stdout.connect()
            stderr = _CommandWebsocketClient(manager,
                                             self.client.websocket_url,
                                             encoding=encoding,
                                             decode=decode,
                                             handler=stderr_handler)
            stderr.resource = '{}?secret={}'.format(parsed.path, fds['2'])
            stderr.connect()

            manager.start()

            # watch for the end of the command:
            while True:
                operation = self.client.operations.get(operation_id)
                if 'return' in operation.metadata:
                    break
                time.sleep(.5)  # pragma: no cover

            while len(manager.websockets.values()) > 0:
                time.sleep(.1)  # pragma: no cover

            manager.stop()
            manager.join()

            return _ContainerExecuteResult(operation.metadata['return'],
                                           stdout.data, stderr.data)
コード例 #23
0
    def execute(
        self,
        commands,
        environment=None,
        encoding=None,
        decode=True,
        stdin_payload=None,
        stdin_encoding="utf-8",
        stdout_handler=None,
        stderr_handler=None,
    ):
        """Execute a command on the instance. stdout and stderr are buffered if
        no handler is given.

        :param commands: The command and arguments as a list of strings
        :type commands: [str]
        :param environment: The environment variables to pass with the command
        :type environment: {str: str}
        :param encoding: The encoding to use for stdout/stderr if the param
            decode is True.  If encoding is None, then no override is
            performed and whatever the existing encoding from LXD is used.
        :type encoding: str
        :param decode: Whether to decode the stdout/stderr or just return the
            raw buffers.
        :type decode: bool
        :param stdin_payload: Payload to pass via stdin
        :type stdin_payload: Can be a file, string, bytearray, generator or
            ws4py Message object
        :param stdin_encoding: Encoding to pass text to stdin (default utf-8)
        :param stdout_handler: Callable than receive as first parameter each
            message received via stdout
        :type stdout_handler: Callable[[str], None]
        :param stderr_handler: Callable than receive as first parameter each
            message received via stderr
        :type stderr_handler: Callable[[str], None]
        :raises ValueError: if the ws4py library is not installed.
        :returns: A tuple of `(exit_code, stdout, stderr)`
        :rtype: _InstanceExecuteResult() namedtuple
        """
        if not _ws4py_installed:
            raise ValueError(
                "This feature requires the optional ws4py library.")
        if isinstance(commands, str):
            raise TypeError("First argument must be a list.")
        if environment is None:
            environment = {}

        response = self.api["exec"].post(
            json={
                "command": commands,
                "environment": environment,
                "wait-for-websocket": True,
                "interactive": False,
            })

        fds = response.json()["metadata"]["metadata"]["fds"]
        operation_id = Operation.extract_operation_id(
            response.json()["operation"])
        parsed = parse.urlparse(
            self.client.api.operations[operation_id].websocket._api_endpoint)

        with managers.web_socket_manager(WebSocketManager()) as manager:
            stdin = _StdinWebsocket(
                self.client.websocket_url,
                payload=stdin_payload,
                encoding=stdin_encoding,
            )
            stdin.resource = "{}?secret={}".format(parsed.path, fds["0"])
            stdin.connect()
            stdout = _CommandWebsocketClient(
                manager,
                self.client.websocket_url,
                encoding=encoding,
                decode=decode,
                handler=stdout_handler,
            )
            stdout.resource = "{}?secret={}".format(parsed.path, fds["1"])
            stdout.connect()
            stderr = _CommandWebsocketClient(
                manager,
                self.client.websocket_url,
                encoding=encoding,
                decode=decode,
                handler=stderr_handler,
            )
            stderr.resource = "{}?secret={}".format(parsed.path, fds["2"])
            stderr.connect()

            manager.start()

            # watch for the end of the command:
            while True:
                operation = self.client.operations.get(operation_id)
                if "return" in operation.metadata:
                    break
                time.sleep(0.5)  # pragma: no cover

            try:
                stdin.close()
            except BrokenPipeError:
                pass

            stdout.finish_soon()
            stderr.finish_soon()
            try:
                manager.close_all()
            except BrokenPipeError:
                pass

            while not stdout.finished or not stderr.finished:
                time.sleep(0.1)  # progma: no cover

            manager.stop()
            manager.join()

            return _InstanceExecuteResult(operation.metadata["return"],
                                          stdout.data, stderr.data)
コード例 #24
0
""" Sets up websockets for Dashboard """
# pylint: disable-msg=C0103

import json
import os
from ws4py.client import WebSocketBaseClient
from ws4py.manager import WebSocketManager
from ws4py import format_addresses, configure_logger
import queue
import requests

LOGGER = configure_logger()

MANAGER = WebSocketManager()

coin_queue = queue.Queue()

#FIXME: Change these global to local variable
BTC_USD = 0.0
XRB_BTC = 0.0
STORM_USD = 0.0


class SetQueue():
    """
    Custom Queue that uses a set instead of a regular queue. This keeps a set of coin names as well as a dict of it's coin:value
        :param queue.Queue: 
    """
    def __init__(self):
        self.queue = set()
        self.values = dict({})
コード例 #25
0
ファイル: script.py プロジェクト: francma/piper-ci-lxd-runner
    def __enter__(self):
        self._container = None
        self._container_name = 'piper' + uuid.uuid4().hex
        container_config = {
            'name': self._container_name,
            'profiles': self._lxd_profiles,
            'source': self._job.lxd_source,
            'devices': {
                'piper_repository': {
                    'type': 'disk',
                    'path': '/piper',
                    'source': str(self._repository_path),
                }
            }
        }

        try:
            self._container = self._lxd_client.containers.create(
                container_config, wait=True)
        except LXDAPIException as e:
            raise PScriptException('Failed to create LXD container. Raw: ' +
                                   str(e))

        try:
            self._container.start(wait=True)
        except LXDAPIException as e:
            raise PScriptException('Failed to start LXD container. Raw: ' +
                                   str(e))
        env = {k: str(v) for k, v in self._job.env.items()}
        config = {
            'command': ['/bin/sh', '-c', self._job.script],
            'wait-for-websocket': True,
            'interactive': False,
            'environment': env,
        }

        try:
            response = self._lxd_client.api.containers[
                self._container_name].exec.post(json=config)
        except LXDAPIException as e:
            raise PScriptException('Failed to execute command. Raw: ' + str(e))

        fds = response.json()['metadata']['metadata']['fds']
        self.operation_id = response.json()['operation'].split('/')[-1]

        websocket_url = '/1.0/operations/{}/websocket'.format(
            self.operation_id)
        stdin_url = '{}?secret={}'.format(websocket_url, fds['0'])
        stdout_url = '{}?secret={}'.format(websocket_url, fds['1'])
        stderr_url = '{}?secret={}'.format(websocket_url, fds['2'])

        stdin = self.NullWebSocket(self._lxd_client.websocket_url)
        stdin.resource = stdin_url
        stdin.connect()

        self.manager = WebSocketManager()
        self.manager.start()

        self._handler = BufferHandler()
        stdout = self.WebSocket(self.manager, self._handler,
                                self._lxd_client.websocket_url)
        stdout.resource = stdout_url
        stdout.connect()

        stderr = self.WebSocket(self.manager, self._handler,
                                self._lxd_client.websocket_url)
        stderr.resource = stderr_url
        stderr.connect()

        return self
コード例 #26
0
    def init(self):
        """ Initializes the PB WS Client"""

        self.pb = Pushbullet(cfg.PUSHBULLET_KEY, cfg.PUSHBULLET_SECRET)
        self.manager = WebSocketManager()
        self.alarm = None