當前位置: 首頁>>代碼示例>>Python>>正文


Python encoder.Encoder類代碼示例

本文整理匯總了Python中krpc.encoder.Encoder的典型用法代碼示例。如果您正苦於以下問題:Python Encoder類的具體用法?Python Encoder怎麽用?Python Encoder使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Encoder類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: connect

def connect(address=DEFAULT_ADDRESS, rpc_port=DEFAULT_RPC_PORT, stream_port=DEFAULT_STREAM_PORT, name=None):
    """
    Connect to a kRPC server on the specified IP address and port numbers. If
    stream_port is None, does not connect to the stream server.
    Optionally give the kRPC server the supplied name to identify the client (up
    to 32 bytes of UTF-8 encoded text).
    """
    assert rpc_port != stream_port

    # Connect to RPC server
    rpc_connection = Connection(address, rpc_port)
    rpc_connection.connect(retries=10, timeout=0.1)
    rpc_connection.send(Encoder.RPC_HELLO_MESSAGE)
    rpc_connection.send(Encoder.client_name(name))
    client_identifier = rpc_connection.receive(Decoder.GUID_LENGTH)

    # Connect to Stream server
    if stream_port is not None:
        stream_connection = Connection(address, stream_port)
        stream_connection.connect(retries=10, timeout=0.1)
        stream_connection.send(Encoder.STREAM_HELLO_MESSAGE)
        stream_connection.send(client_identifier)
        ok_message = stream_connection.receive(Decoder.OK_LENGTH)
        assert ok_message == Decoder.OK_MESSAGE
    else:
        stream_connection = None

    return Client(rpc_connection, stream_connection)
開發者ID:rosalesr,項目名稱:krpc,代碼行數:28,代碼來源:__init__.py

示例2: test_encode_message_delimited

 def test_encode_message_delimited(self):
     request = krpc.schema.KRPC.Request()
     request.service = 'ServiceName'
     request.procedure = 'ProcedureName'
     data = Encoder.encode_delimited(request, self.types.as_type('KRPC.Request'))
     expected = '1c'+'0a0b536572766963654e616d65120d50726f6365647572654e616d65'
     self.assertEqual(expected, hexlify(data))
開發者ID:Kerbal007,項目名稱:krpc,代碼行數:7,代碼來源:test_encoder.py

示例3: test_encode_message

 def test_encode_message(self):
     call = self.types.procedure_call_type.python_type()
     call.service = 'ServiceName'
     call.procedure = 'ProcedureName'
     data = Encoder.encode(call, self.types.procedure_call_type)
     expected = '0a0b536572766963654e616d65120d50726f6365647572654e616d65'
     self.assertEqual(expected, hexlify(data))
開發者ID:Loran425,項目名稱:krpc,代碼行數:7,代碼來源:test_encoder.py

示例4: test_encode_class

 def test_encode_class(self):
     typ = self.types.as_type('Class(ServiceName.ClassName)')
     class_type = typ.python_type
     self.assertTrue(issubclass(class_type, ClassBase))
     value = class_type(300)
     self.assertEqual(300, value._object_id)
     data = Encoder.encode(value, typ)
     self.assertEqual('ac02', hexlify(data))
開發者ID:Kerbal007,項目名稱:krpc,代碼行數:8,代碼來源:test_encoder.py

示例5: encode_argument

 def encode_argument(i, value):
     typ = param_types[i]
     if type(value) != typ.python_type:
         # Try coercing to the correct type
         try:
             value = self._types.coerce_to(value, typ)
         except ValueError:
             raise TypeError('%s.%s() argument %d must be a %s, got a %s' % \
                             (service, procedure, i, typ.python_type, type(value)))
     return Encoder.encode(value, typ)
開發者ID:asmigala,項目名稱:krpc,代碼行數:10,代碼來源:client.py

示例6: _build_request

    def _build_request(self, service, procedure, args,
                       param_names, param_types, return_type): #pylint: disable=unused-argument
        """ Build a KRPC.Request object """

        request = krpc.schema.KRPC.Request(service=service, procedure=procedure)

        for i, (value, typ) in enumerate(itertools.izip(args, param_types)):
            if isinstance(value, DefaultArgument):
                continue
            if not isinstance(value, typ.python_type):
                try:
                    value = self._types.coerce_to(value, typ)
                except ValueError:
                    raise TypeError('%s.%s() argument %d must be a %s, got a %s' % \
                                    (service, procedure, i, typ.python_type, type(value)))
            request.arguments.add(position=i, value=Encoder.encode(value, typ))

        return request
開發者ID:chippydip,項目名稱:krpc,代碼行數:18,代碼來源:client.py

示例7: _send_request

 def _send_request(self, request):
     """ Send a KRPC.Request object to the server """
     data = Encoder.encode_delimited(request, self._request_type)
     self._rpc_connection.send(data)
開發者ID:602p,項目名稱:krpc,代碼行數:4,代碼來源:client.py

示例8: _run_test_encode_value

 def _run_test_encode_value(self, typ, cases):
     for decoded, encoded in cases:
         data = Encoder.encode(decoded, self.types.as_type(typ))
         self.assertEqual(encoded, hexlify(data))
開發者ID:Kerbal007,項目名稱:krpc,代碼行數:4,代碼來源:test_encodedecode.py

示例9: test_encode_class_none

 def test_encode_class_none(self):
     typ = self.types.as_type('Class(ServiceName.ClassName)')
     value = None
     data = Encoder.encode(value, typ)
     self.assertEqual('00', hexlify(data))
開發者ID:Kerbal007,項目名稱:krpc,代碼行數:5,代碼來源:test_encoder.py

示例10: test_encode_value_delimited

 def test_encode_value_delimited(self):
     data = Encoder.encode_delimited(300, self.types.as_type('int32'))
     self.assertEqual('02'+'ac02', hexlify(data))
開發者ID:Kerbal007,項目名稱:krpc,代碼行數:3,代碼來源:test_encoder.py

示例11: test_encode_unicode_string

 def test_encode_unicode_string(self):
     data = Encoder.encode(b'\xe2\x84\xa2'.decode('utf-8'), self.types.as_type('string'))
     self.assertEqual('03e284a2', hexlify(data))
開發者ID:Kerbal007,項目名稱:krpc,代碼行數:3,代碼來源:test_encoder.py

示例12: test_long_client_name

 def test_long_client_name(self):
     message = Encoder.client_name('a'*33)
     self.assertEqual (32, len(message))
     self.assertEqual ('61'*32, hexlify(message))
開發者ID:Kerbal007,項目名稱:krpc,代碼行數:4,代碼來源:test_encoder.py

示例13: test_empty_client_name

 def test_empty_client_name(self):
     message = Encoder.client_name()
     self.assertEqual (32, len(message))
     self.assertEqual ('00'*32, hexlify(message))
開發者ID:Kerbal007,項目名稱:krpc,代碼行數:4,代碼來源:test_encoder.py

示例14: test_client_name

 def test_client_name(self):
     message = Encoder.client_name('foo')
     self.assertEqual (32, len(message))
     self.assertEqual ('666f6f'+'00'*29, hexlify(message))
開發者ID:Kerbal007,項目名稱:krpc,代碼行數:4,代碼來源:test_encoder.py

示例15: test_encode_value

 def test_encode_value(self):
     data = Encoder.encode(300, self.types.uint32_type)
     self.assertEqual('ac02', hexlify(data))
開發者ID:Loran425,項目名稱:krpc,代碼行數:3,代碼來源:test_encoder.py


注:本文中的krpc.encoder.Encoder類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。