当前位置: 首页>>代码示例>>Python>>正文


Python ZMQStream.send_json方法代码示例

本文整理汇总了Python中zmq.eventloop.zmqstream.ZMQStream.send_json方法的典型用法代码示例。如果您正苦于以下问题:Python ZMQStream.send_json方法的具体用法?Python ZMQStream.send_json怎么用?Python ZMQStream.send_json使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在zmq.eventloop.zmqstream.ZMQStream的用法示例。


在下文中一共展示了ZMQStream.send_json方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: SwysSearchRequestHandler

# 需要导入模块: from zmq.eventloop.zmqstream import ZMQStream [as 别名]
# 或者: from zmq.eventloop.zmqstream.ZMQStream import send_json [as 别名]
class SwysSearchRequestHandler(BaseHandler):
    def initialize(self):
        socket = context.socket(zmq.REQ)
        socket.connect(conf.SEARCH_WORKER_ZMQ_ENDPOINT)

        self._zmq_stream = ZMQStream(socket)
        self._zmq_stream.on_recv(self._recv_result, copy=True)

    @tornado.web.asynchronous
    def handle_request_async(self, *args, **kwargs):

        files = self.request.files.get('image', [])

        if len(files) == 0:
            raise Exception("there is no file attached")

        file = files[0]

        temp_file = tempfile.NamedTemporaryFile('wb', delete=False)
        temp_file.write(file.body)

        self._zmq_stream.send_json({'filename': temp_file.name})

    def _recv_result(self, msg):

        result_str = "".join(( part.decode('utf-8') for part in msg ))
        result = json.loads(result_str)['data']

        return self.on_complete(result)
开发者ID:dzharkov,项目名称:swys-server,代码行数:31,代码来源:request_handler.py

示例2: __init__

# 需要导入模块: from zmq.eventloop.zmqstream import ZMQStream [as 别名]
# 或者: from zmq.eventloop.zmqstream.ZMQStream import send_json [as 别名]
class ZmqSocketManager:
    """
    This class manages the zmq socket. There is one opening zmq
    socket, which is for managing the synchronous tasks, such as
    connecting with the SFTP server, disconnecting, listing content
    and so on.
    """

    def __init__(self, sftp):
        loop = ZMQIOLoop.instance()

        ctx = zmq.Context.instance()
        sftp_connection_socket = ctx.socket(zmq.REP)
        sftp_connection_socket.bind("tcp://*:4444")
        self.sftp_connection_stream = ZMQStream(sftp_connection_socket, loop)
        self.sftp_connection_stream.on_recv(self._sftp_connection_stream_receive_callback)

        self.sftp_connection_manager = sftp

    def _sftp_connection_stream_receive_callback(self, msg):
        """
        This is the callback method when the sftp_connection_stream socket receives the message. Every message should
        include one field, which is called "action" representing the purpose of this request. At the end of this method,
        self.sftp_connection_stream.send_json method should be invoked.
        :param msg: received message by sftp_connection_stream
        """
        resp_msg = jsonapi.loads(msg[0])
        # ------------------------------------------------
        # Clean connections action
        # ------------------------------------------------
        if resp_msg[ZmqMessageKeys.ACTION.value] == ZmqMessageValues.CLEAN.value:
            self.sftp_connection_manager.clean_sftp_connections()
            self.sftp_connection_stream.send_json({ZmqMessageKeys.RESULT.value: ZmqMessageValues.SUCCESS.value})
        # ------------------------------------------------
        # Login / Connect action
        # ------------------------------------------------
        elif resp_msg[ZmqMessageKeys.ACTION.value] == ZmqMessageValues.CONNECT.value:
            session_key = resp_msg[ZmqMessageKeys.SESSION_KEY.value]
            user_name = resp_msg[ZmqMessageKeys.USERNAME.value]
            otc = resp_msg[ZmqMessageKeys.OTC.value]
            password = resp_msg[ZmqMessageKeys.PASSWORD.value]
            hostname = resp_msg[ZmqMessageKeys.HOSTNAME.value]
            port = resp_msg[ZmqMessageKeys.PORT.value]
            source = resp_msg[ZmqMessageKeys.SOURCE.value]
            expiry = resp_msg[ZmqMessageKeys.EXPIRY.value]

            try:
                transport = self.sftp_connection_manager.authenticate_sftp_user(user_name, password, otc, hostname, port)
            except SSHException as exce:
                self.sftp_connection_stream.send_json({ZmqMessageKeys.EXCEPTION.value: exce.args[0]})
            else:
                self.sftp_connection_manager.add_sftp_connection(session_key, source, transport, datetime.fromtimestamp(expiry))
                self.sftp_connection_stream.send_json({ZmqMessageKeys.RESULT.value: ZmqMessageValues.SUCCESS.value})
        # ------------------------------------------------
        # File listing action
        # ------------------------------------------------
        elif resp_msg[ZmqMessageKeys.ACTION.value] == ZmqMessageValues.LIST.value:
            # Perform a file listing action here
            session_key = resp_msg[ZmqMessageKeys.SESSION_KEY.value]
            source = resp_msg[ZmqMessageKeys.SOURCE.value]
            path = resp_msg[ZmqMessageKeys.PATH.value] if ZmqMessageKeys.PATH.value in resp_msg else sep
            try:
                content = self.sftp_connection_manager.open_sftp_client(source, session_key).listdir_iter(path)
                data_list = []
                for file_attr in content:
                    if file_attr.filename[0:1] != '.': # Exclude hidden files (files starting with a dot)
                        if stat.S_ISDIR(file_attr.st_mode):
                            data_list.append([file_attr.filename, file_attr.st_size, "folder"])
                        else:
                            data_list.append([file_attr.filename, file_attr.st_size, "file"])
            except PermissionError as error:
                self.sftp_connection_stream.send_json({ZmqMessageKeys.EXCEPTION.value: error.strerror})
            except SFTPError as error:
                self.sftp_connection_stream.send_json({ZmqMessageKeys.EXCEPTION.value: error.args[0]})
            else:
                self.sftp_connection_stream.send_json({ZmqMessageKeys.DATA.value: data_list})
        # ------------------------------------------------
        # Disconnect action
        # ------------------------------------------------
        elif resp_msg[ZmqMessageKeys.ACTION.value] == ZmqMessageValues.DISCONNECT.value:
            session_key = resp_msg[ZmqMessageKeys.SESSION_KEY.value]
            source = resp_msg[ZmqMessageKeys.SOURCE.value]
            self.sftp_connection_manager.remove_sftp_connection(source, session_key)
            self.sftp_connection_stream.send_json({ZmqMessageKeys.RESULT.value: ZmqMessageValues.SUCCESS.value})
        # ------------------------------------------------
        # Delete (file or folder) action
        # ------------------------------------------------
        elif resp_msg[ZmqMessageKeys.ACTION.value] == ZmqMessageValues.DELETE.value:
            session_key = resp_msg[ZmqMessageKeys.SESSION_KEY.value]
            source = resp_msg[ZmqMessageKeys.SOURCE.value]
            path = resp_msg[ZmqMessageKeys.PATH.value]
            try:
                sftp_client = self.sftp_connection_manager.open_sftp_client(source, session_key)
                for item in resp_msg[ZmqMessageKeys.DATA.value]:
                    if item['type'] == 'file':
                        sftp_client.remove(path + sep + item['name'])
                    else:
                        ZmqSocketManager.delete_folder(item['name'], path, sftp_client)
            except PermissionError as error:
                self.sftp_connection_stream.send_json({ZmqMessageKeys.EXCEPTION.value: error.strerror})
#.........这里部分代码省略.........
开发者ID:neicnordic,项目名称:sftpbeamer-django_DEPRECATED,代码行数:103,代码来源:backend_process.py

示例3: BaseServer

# 需要导入模块: from zmq.eventloop.zmqstream import ZMQStream [as 别名]
# 或者: from zmq.eventloop.zmqstream.ZMQStream import send_json [as 别名]
class BaseServer(App):
    """Base class for a ZMQ server object that manages a ROUTER socket.

    When a new message arrives on the socket, ZMQ cals the _dispatch()
    method. After validating the message, dispatch() looks for a
    method on the class whose name corresponds to the 'msg_type' (in
    the message's header). This method then gets called as method(**msg),
    with three arguments, header, parent_header and content.

    The method should respond on the stream by calling send_message()
    """

    zmq_port = Int(12345, config=True, help='ZeroMQ port to serve on')
    db_path = Unicode('db.sqlite', config=True, help='''
        Path to the database (sqlite3 file)''')


    def start(self):
        url = 'tcp://*:%s' % int(self.zmq_port)

        self.uuid = str(uuid.uuid4())
        self.ctx = zmq.Context()
        s = self.ctx.socket(zmq.ROUTER)
        s.bind(url)
        self._stream = ZMQStream(s)
        self._stream.on_recv(self._dispatch)
        connect_to_sqlite_db(self.db_path)

    def send_message(self, client_id, msg_type, content=None):
        """Send a message out to a client

        Parameters
        ----------
        client_id : uuid
            Who do you want to send the message to? This is string that
            identifies the client to the ZMQ routing layer. Within our
            messaging protocol, when the server recieves a message, it can
            get the id of the sender from within the message's header -- but
            this is dependent on the fact that the device.Device() code
            puts the same string in the message header that it uses
            to identify the socket to zeromq, in the line
                setsockopt(zmq.IDENTITY, str(self.uuid))
        msg_type : str
            The type of the message
        content : dict
            Content of the message
        Notes
        -----
        For details on the messaging protocol, refer to message.py
        """
        if content is None:
            content = {}

        msg = pack_message(msg_type, self.uuid, content)
        self.log.info('SENDING MESSAGE: %s', msg)

        self._stream.send(client_id, zmq.SNDMORE)
        self._stream.send('', zmq.SNDMORE)
        self._stream.send_json(msg)

    def _validate_msg_dict(self, msg_dict):
        if 'header' not in msg_dict:
            raise ValueError('msg does not contain "header"')
        if 'content' not in msg_dict:
            raise ValueError('msg does not contain "content"')
        if 'sender_id' not in msg_dict['header']:
            raise ValueError('msg header does not contain "sender_id"')
        if 'msg_type' not in msg_dict['header']:
            raise ValueError('msg header does not contain "msg_type"')


    def _dispatch(self, frames):
        """Callback that responds to new messages on the stream

        This is the first point where new messages enter the system. Basically
        we just pack them up and send them on to the correct responder.

        Parameters
        ----------
        stream : ZMQStream
            The stream that we're responding too (probably self._stream)
        messages : list
            A list of messages that have arrived
        """
        # for some reason we seem to be getting double logging
        # inside the event loop, but not outside it. i'm not sure
        # if this is a tornado thing or what, but disabling
        # the parent log handler seems to fix it.
        self.log.parent.handlers = []

        if not len(frames) == 3:
            self.log.error('invalid message received. messages are expected to contain only three frames: %s', str(frames))

        client, _, raw_msg = frames
        # using the PyYaml loader is a hack force regular strings
        # instead of unicode, since you can't send unicode over zmq
        # since json is a subset of yaml, this works
        msg_dict = yaml.load(raw_msg)

        try:
#.........这里部分代码省略.........
开发者ID:mpharrigan,项目名称:msmaccelerator2,代码行数:103,代码来源:baseserver.py


注:本文中的zmq.eventloop.zmqstream.ZMQStream.send_json方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。