示例#1
0
def make_response(result={}, success=True, error_code='', msg='', status=200, request_time=None):
    data = dict(
        success=success,
        msg=msg,
    )
    if not success:
        data['error_code'] = error_code
    else:
        data['result'] = result
    data['request_time'] = request_time
    return Response(
        response=jsondump(data),
        status=status,
        mimetype='application/json')
示例#2
0
文件: common.py 项目: tranquilit/WAPT
def make_response(result={}, success=True, error_code='', msg='', status=200, request_time=None):
    data = dict(
        success=success,
        msg=msg,
    )
    if not success:
        data['error_code'] = error_code
    else:
        data['result'] = result
    data['request_time'] = request_time
    return Response(
        response=jsondump(data),
        status=status,
        mimetype='application/json')
示例#3
0
def make_response_from_exception(exception, error_code='', status=200):
    """Return a error flask http response from an exception object
        success : False
        msg : message from exception
        error_code : classname of exception if not provided
        status: 200 if not provided
    """
    if not error_code:
        error_code = type(exception).__name__.lower()
    data = dict(success=False, error_code=error_code)
    if utils_devel_mode:
        raise exception
    else:
        data['msg'] = u'Error on server: %s' % (exception, )
    return flask.Response(response=jsondump(data),
                          status=status,
                          mimetype='application/json')
示例#4
0
def make_response_from_exception(exception, error_code='', status=200):
    """Return a error flask http response from an exception object
        success : False
        msg : message from exception
        error_code : classname of exception if not provided
        status: 200 if not provided
    """
    if not error_code:
        error_code = type(exception).__name__.lower()
    data = dict(success=False, error_code=error_code)

    logger.info(traceback.format_exc())

    data['msg'] = u'Error on server:\n%s' % (repr(exception))
    return Response(response=jsondump(data),
                    status=status,
                    mimetype='application/json')
示例#5
0
文件: common.py 项目: tranquilit/WAPT
def make_response_from_exception(exception, error_code='', status=200):
    """Return a error flask http response from an exception object
        success : False
        msg : message from exception
        error_code : classname of exception if not provided
        status: 200 if not provided
    """
    if not error_code:
        error_code = type(exception).__name__.lower()
    data = dict(
        success=False,
        error_code=error_code
    )

    logger.debug(traceback.format_exc())

    data['msg'] = u'Error on server:\n%s' % (repr(exception))
    return Response(
        response=jsondump(data),
        status=status,
        mimetype='application/json')
示例#6
0
    def run(self):
        self.config.reload_if_updated()
        with Wapt(config_filename = self.config.config_filename) as tmp_wapt:
            logger.info('Starting socketio on "%s://%s:%s" ...' % (self.config.websockets_proto,self.config.websockets_host,self.config.websockets_port))
            logger.debug('Certificate checking : %s' %  self.config.websockets_verify_cert)
            while True:
                try:
                    connect_params = dict(
                        uuid = tmp_wapt.host_uuid,
                    )
                    host_key = tmp_wapt.get_host_key()
                    host_cert = tmp_wapt.get_host_certificate()

                    if not self.socketio_client and self.config.websockets_host:
                        logger.debug('Creating socketio client')
                        logger.debug('Proxies : %s'%self.config.waptserver.proxies)
                        # bug in socketio... ? we must not pass proxies at all (even None) if we don"t want to switch to polling mode...
                        kwargs = {}
                        if self.config.waptserver.proxies and self.config.waptserver.proxies.get(self.config.websockets_proto,None) is not None:
                            kwargs['proxies'] = self.config.waptserver.proxies


                        signed_connect_params = host_key.sign_claim(connect_params,signer_certificate_chain = host_cert)

                        self.socketio_client = SocketIO(
                                host="%s://%s" % (self.config.websockets_proto,self.config.websockets_host),
                                port=self.config.websockets_port,
                                Namespace = WaptSocketIORemoteCalls,
                                resource=self.config.websockets_root,
                                verify=self.config.websockets_verify_cert,
                                wait_for_connection = False,
                                transport = ['websocket'],
                                ping_interval = self.config.websockets_ping,
                                hurry_interval_in_seconds = self.config.websockets_hurry_interval,
                                params = {'uuid': tmp_wapt.host_uuid, 'login':jsondump(signed_connect_params)},
                                **kwargs)
                        self.socketio_client.get_namespace().wapt = tmp_wapt
                        self.socketio_client.get_namespace().task_manager = self.task_manager

                    if self.socketio_client and self.config.websockets_host:
                        if not self.socketio_client.connected:
                            signed_connect_params = host_key.sign_claim(connect_params,signer_certificate_chain = host_cert)

                            self.socketio_client._http_session.params.update({'uuid': tmp_wapt.host_uuid,'login':jsondump(signed_connect_params)})
                            self.socketio_client.define(WaptSocketIORemoteCalls)
                            self.socketio_client.get_namespace().wapt = tmp_wapt
                            self.socketio_client.get_namespace().task_manager = self.task_manager
                            self.socketio_client.connect('')
                        else:
                            self.socketio_client.emit('wapt_pong')

                        if self.socketio_client.connected:
                            logger.info('Socket IO listening for %ss' % self.config.websockets_check_config_interval )
                            startwait = time.time()
                            self.socketio_client.wait(self.config.websockets_check_config_interval)
                            # QAD workaround for cases where server disconnect but client is between 2 states.
                            # In this case; wait() immediately returns, leading to an indefinite loop eating 1 core.
                            if time.time() - startwait < self.config.websockets_check_config_interval-2:
                                raise Exception('Websocket client seems disconnected. Force Websocket connection to be recreated')
                    elif not self.config.websockets_host:
                        self.socketio_client = None
                        time.sleep(self.config.websockets_retry_delay)
                    else:
                        time.sleep(self.config.websockets_retry_delay)

                    if self.config.reload_if_updated():
                        if self.socketio_client:
                            self.socketio_client.disconnect()
                        raise EWaptException('Configuration changed, force Websocket connection to be recreated')


                except Exception as e:
                    try:
                        logger.info('Error in socket io connection %s' % repr(e))
                        self.config.reload_if_updated()
                        if self.socketio_client:
                            logger.debug('stop sio client')
                            self.socketio_client = None
                    finally:
                        logger.debug('Socket IO Stopped, waiting %ss before retrying' %
                            self.config.websockets_retry_delay)
                        time.sleep(self.config.websockets_retry_delay)