本文整理汇总了Python中Pyro4.message.Message类的典型用法代码示例。如果您正苦于以下问题:Python Message类的具体用法?Python Message怎么用?Python Message使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Message类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testHmacMethod
def testHmacMethod(self):
data = Message(Pyro4.message.MSG_RESULT, b"test", 42, 0, 1, hmac_key=b"test key")
digest = data.hmac()
self.assertTrue(len(digest) > 10)
data = Message(Pyro4.message.MSG_RESULT, b"test", 42, 0, 1)
with self.assertRaises(TypeError):
data.hmac()
示例2: testMessageHeaderDatasize
def testMessageHeaderDatasize(self):
msg = Message(Pyro4.message.MSG_RESULT, b"hello", 12345, 60006, 30003, hmac_key=b"secret")
msg.data_size = 0x12345678 # hack it to a large value to see if it comes back ok
hdr = msg.to_bytes()[:24]
msg = Message.from_header(hdr)
self.assertEqual(Pyro4.message.MSG_RESULT, msg.type)
self.assertEqual(60006, msg.flags)
self.assertEqual(0x12345678, msg.data_size)
self.assertEqual(12345, msg.serializer_id)
self.assertEqual(30003, msg.seq)
示例3: testRecvNoAnnotations
def testRecvNoAnnotations(self):
msg = Message(Pyro4.message.MSG_CONNECT, b"hello", 42, 0, 0)
c = ConnectionMock()
c.send(msg.to_bytes())
msg = Message.recv(c)
self.assertEqual(0, len(c.received))
self.assertEqual(5, msg.data_size)
self.assertEqual(b"hello", msg.data)
self.assertEqual(0, msg.annotations_size)
self.assertEqual(0, len(msg.annotations))
示例4: testAnnotations
def testAnnotations(self):
annotations = {"TEST": b"abcde"}
msg = Message(Pyro4.message.MSG_CONNECT, b"hello", self.ser.serializer_id, 0, 0, annotations, b"secret")
data = msg.to_bytes()
annotations_size = 4 + 2 + 20 + 4 + 2 + 5
self.assertEqual(msg.header_size + 5 + annotations_size, len(data))
self.assertEqual(annotations_size, msg.annotations_size)
self.assertEqual(2, len(msg.annotations))
self.assertEqual(b"abcde", msg.annotations["TEST"])
mac = pyrohmac(b"hello", b"secret", annotations)
self.assertEqual(mac, msg.annotations["HMAC"])
示例5: testRecvAnnotations
def testRecvAnnotations(self):
annotations = {"TEST": b"abcde"}
msg = Message(Pyro4.message.MSG_CONNECT, b"hello", self.ser.serializer_id, 0, 0, annotations, b"secret")
c = ConnectionMock()
c.send(msg.to_bytes())
msg = Message.recv(c, hmac_key=b"secret")
self.assertEqual(0, len(c.received))
self.assertEqual(5, msg.data_size)
self.assertEqual(b"hello", msg.data)
self.assertEqual(b"abcde", msg.annotations["TEST"])
self.assertIn("HMAC", msg.annotations)
示例6: testRecvAnnotations
def testRecvAnnotations(self):
annotations = { b"TEST": b"abcde" }
msg = Message(Pyro4.message.MSG_CONNECT, b"hello", self.ser.serializer_id, 0, 0, annotations)
c = ConnectionMock()
c.send(msg.to_bytes())
msg = Message.recv(c)
self.assertEquals(0, len(c.received))
self.assertEquals(5, msg.data_size)
self.assertEquals(b"hello", msg.data)
self.assertEquals(b"abcde", msg.annotations[b"TEST"])
self.assertTrue(b"HMAC" in msg.annotations)
示例7: testMaxDataSize
def testMaxDataSize(self):
msg = Message(Pyro4.message.MSG_CONNECT, b"hello", 42, 0, 0)
msg.data_size = 0x7fffffff # still within 32 bits signed limits
msg.to_bytes()
msg.data_size = 0x80000000 # overflow, Pyro has a 2 gigabyte message size limitation
with self.assertRaises(ValueError) as ex:
msg.to_bytes()
self.assertEqual("invalid message size (outside range 0..2Gb)", str(ex.exception))
msg.data_size = -42
with self.assertRaises(ValueError) as ex:
msg.to_bytes()
self.assertEqual("invalid message size (outside range 0..2Gb)", str(ex.exception))
示例8: testCompression
def testCompression(self):
data = b"The quick brown fox jumps over the lazy dog."*10
compressed_data = zlib.compress(data)
flags = Pyro4.message.FLAGS_COMPRESSED
msg = Message(Pyro4.message.MSG_INVOKE, compressed_data, 42, flags, 1, hmac_key=b"secret")
self.assertNotEqual(data, msg.data)
data_size = msg.data_size
self.assertLess(data_size, len(data))
msg.decompress_if_needed()
self.assertEqual(data, msg.data)
self.assertEqual(0, msg.flags)
self.assertGreater(msg.data_size, data_size)
示例9: testChecksum
def testChecksum(self):
msg = Message(Pyro4.message.MSG_RESULT, b"test", 42, 0, 1, hmac_key=b"secret")
c = ConnectionMock()
c.send(msg.to_bytes())
# corrupt the checksum bytes
data = c.received
data = data[:msg.header_size - 2] + b'\x00\x00' + data[msg.header_size:]
c = ConnectionMock(data)
try:
Message.recv(c)
self.fail("crash expected")
except Pyro4.errors.ProtocolError as x:
self.assertIn("checksum", str(x))
示例10: testAnnotationsIdLength4
def testAnnotationsIdLength4(self):
try:
msg = Message(Pyro4.message.MSG_CONNECT, b"hello", self.ser.serializer_id, 0, 0, {"TOOLONG": b"abcde"}, b"secret")
_ = msg.to_bytes()
self.fail("should fail, too long")
except Pyro4.errors.ProtocolError:
pass
try:
msg = Message(Pyro4.message.MSG_CONNECT, b"hello", self.ser.serializer_id, 0, 0, {"QQ": b"abcde"}, b"secret")
_ = msg.to_bytes()
self.fail("should fail, too short")
except Pyro4.errors.ProtocolError:
pass
示例11: connectionMade
def connectionMade(self):
"""
Handshake - should replicate Pyro4.Daemon._handshake()
"""
log = logger.debug
if self.state == "server":
log("Connection made with Pyro4Protocol")
log("... attempting handshake")
ser = util.get_serializer("marshal")
data = ser.dumps("ok")
msg = Message(Pyro4.message.MSG_CONNECTOK, data, ser.serializer_id, 0, 1)
self.transport.write(msg.to_bytes())
self.state = "header"
示例12: testMessage
def testMessage(self):
Message(99, b"", self.ser.serializer_id, 0, 0, hmac_key=b"secret") # doesn't check msg type here
self.assertRaises(Pyro4.errors.ProtocolError, Message.from_header, "FOOBAR")
msg = Message(Pyro4.message.MSG_CONNECT, b"hello", self.ser.serializer_id, 0, 0, hmac_key=b"secret")
self.assertEqual(Pyro4.message.MSG_CONNECT, msg.type)
self.assertEqual(5, msg.data_size)
self.assertEqual(b"hello", msg.data)
self.assertEqual(4 + 2 + 20, msg.annotations_size)
mac = pyrohmac(b"hello", b"secret", msg.annotations)
self.assertDictEqual({"HMAC": mac}, msg.annotations)
hdr = msg.to_bytes()[:24]
msg = Message.from_header(hdr)
self.assertEqual(Pyro4.message.MSG_CONNECT, msg.type)
self.assertEqual(4 + 2 + 20, msg.annotations_size)
self.assertEqual(5, msg.data_size)
hdr = Message(Pyro4.message.MSG_RESULT, b"", self.ser.serializer_id, 0, 0, hmac_key=b"secret").to_bytes()[:24]
msg = Message.from_header(hdr)
self.assertEqual(Pyro4.message.MSG_RESULT, msg.type)
self.assertEqual(4 + 2 + 20, msg.annotations_size)
self.assertEqual(0, msg.data_size)
hdr = Message(Pyro4.message.MSG_RESULT, b"hello", 12345, 60006, 30003, hmac_key=b"secret").to_bytes()[:24]
msg = Message.from_header(hdr)
self.assertEqual(Pyro4.message.MSG_RESULT, msg.type)
self.assertEqual(60006, msg.flags)
self.assertEqual(5, msg.data_size)
self.assertEqual(12345, msg.serializer_id)
self.assertEqual(30003, msg.seq)
msg = Message(255, b"", self.ser.serializer_id, 0, 255, hmac_key=b"secret").to_bytes()
self.assertEqual(50, len(msg))
msg = Message(1, b"", self.ser.serializer_id, 0, 255, hmac_key=b"secret").to_bytes()
self.assertEqual(50, len(msg))
msg = Message(1, b"", self.ser.serializer_id, flags=253, seq=254, hmac_key=b"secret").to_bytes()
self.assertEqual(50, len(msg))
# compression is a job of the code supplying the data, so the messagefactory should leave it untouched
data = b"x" * 1000
msg = Message(Pyro4.message.MSG_INVOKE, data, self.ser.serializer_id, 0, 0, hmac_key=b"secret").to_bytes()
msg2 = Message(
Pyro4.message.MSG_INVOKE,
data,
self.ser.serializer_id,
Pyro4.message.FLAGS_COMPRESSED,
0,
hmac_key=b"secret",
).to_bytes()
self.assertEqual(len(msg), len(msg2))
示例13: testHmac
def testHmac(self):
data = Message(Pyro4.message.MSG_RESULT, b"test", 42, 0, 1, hmac_key=b"test key").to_bytes()
c = ConnectionMock(data)
# test checking of different hmacs
try:
Message.recv(c, hmac_key=None)
self.fail("crash expected")
except Pyro4.errors.SecurityError as x:
self.assertIn("hmac key config", str(x))
c = ConnectionMock(data)
try:
Message.recv(c, hmac_key=b"T3ST-K3Y")
self.fail("crash expected")
except Pyro4.errors.SecurityError as x:
self.assertIn("hmac", str(x))
# test that it works again when providing the correct key
c = ConnectionMock(data)
msg = Message.recv(c, hmac_key=b"test key")
self.assertEqual(b"test key", msg.hmac_key)
示例14: testHmac
def testHmac(self):
try:
hk = Pyro4.config.HMAC_KEY
Pyro4.config.HMAC_KEY = b"test key"
data = Message(Pyro4.message.MSG_RESULT, b"test", 42, 0, 1).to_bytes()
c = ConnectionMock(data)
finally:
Pyro4.config.HMAC_KEY = hk
# test checking of different hmacs
try:
Message.recv(c)
self.fail("crash expected")
except Pyro4.errors.SecurityError as x:
self.assertIn("hmac", str(x))
c = ConnectionMock(data)
# test that it works again when resetting the key
try:
hk = Pyro4.config.HMAC_KEY
Pyro4.config.HMAC_KEY = b"test key"
Message.recv(c)
finally:
Pyro4.config.HMAC_KEY = hk
c = ConnectionMock(data)
# test that it doesn't work when no key is set
try:
hk = Pyro4.config.HMAC_KEY
Pyro4.config.HMAC_KEY = b""
Message.recv(c)
self.fail("crash expected")
except Pyro4.errors.SecurityError as x:
self.assertIn("hmac key config", str(x))
finally:
Pyro4.config.HMAC_KEY = hk
示例15: dataReceived
def dataReceived(self, data):
"""
This function must aggregate and standardize the I/O behavior of several Pyro4 functions:
- Daemon.handleRequest
- Message.recv
- Protocol._pyroInvoke
Due to differences between synchronous and asynchronous approaches, twisted-pyro uses states to determine how
to route received data. Because state data need not persist across connections (unlike state information in
many applications), it is attached to the Protocol. These states are:
- server: indicates that a handshake will be required upon connection
- header: waiting on enough data to parse a message header and respond accordingly (idle state for servers)
- annotations: header parsed, waiting on amount of annotation data requested in header
- data: header parsed, waiting on amount of data requested in header
- response: the other end is waiting for data from us (idle state for clients)
"""
log = logger.debug
log("Handling %d bytes of data" % len(data))
self.data += data
if self.state == "header" and len(self.data) >= Message.header_size:
log("... enough data received to process header.")
# Have enough data to process headers
self.request = Message.from_header(self.data[:Message.header_size])
if Pyro4.config.LOGWIRE:
log("wiredata received: msgtype=%d flags=0x%x ser=%d seq=%d data=%r" %
(self.request.type, self.request.flags, self.request.serializer_id, self.request.seq, self.request.data))
if self.required_message_types and self.request.type not in self.required_message_types:
err = "invalid msg type %d received" % self.request.type
logger.error(err)
self._return_error(errors.ProtocolError(err))
if self.request.serializer_id not in \
set([util.get_serializer(ser_name).serializer_id
for ser_name in Pyro4.config.SERIALIZERS_ACCEPTED]):
self._return_error(errors.ProtocolError("message used serializer that is not accepted: %d" % self.request.serializer_id))
self.data = self.data[Message.header_size:]
if self.request.annotations_size:
self.state = "annotations"
else:
self.state = "data"
if self.state == "annotations" and len(self.data) >= self.request.annotations_size:
log("... enough data received to process annotation.")
self.request.annotations = {}
annotations_data = self.data[:self.request.annotations_size]
self.data = self.data[self.request.annotations_size:]
i = 0
while i < self.request.annotations_size:
anno, length = struct.unpack("!4sH", annotations_data[i:i+6])
self.request.annotations[anno] = annotations_data[i+6:i+6+length]
i += 6+length
if b"HMAC" in self.request.annotations and Pyro4.config.HMAC_KEY:
if self.request.annotations[b"HMAC"] != self.request.hmac():
self._return_error(errors.SecurityError("message hmac mismatch"))
elif (b"HMAC" in self.request.annotations) != bool(Pyro4.config.HMAC_KEY):
# Message contains hmac and local HMAC_KEY not set, or vice versa. This is not allowed.
err = "hmac key config not symmetric"
logger.warning(err)
self._return_error(errors.SecurityError(err))
self.state = "data"
if self.state == "data" and len(self.data) >= self.request.data_size:
log("... enough data received to process data.")
# A oneway call can be immediately followed by another call. Otherwise, we should not receive any
# additional data until we have sent a response
if self.request.flags & Pyro4.message.FLAGS_ONEWAY:
if self.request.type == message.MSG_PING:
error_msg = "ONEWAY ping doesn't make sense"
self._return_error(errors.ProtocolError(error_msg))
else:
if len(self.data) > self.request.data_size:
self.transport.loseConnection()
error_msg = "max message size exceeded (%d where max=%d)" % \
(self.request.data_size + self.request.annotations_size, Pyro4.config.MAX_MESSAGE_SIZE)
self._return_error(errors.ProtocolError(error_msg))
# Transfer data to message
self.request.data = self.data[:self.request.data_size]
self.data = self.data[self.request.data_size:]
# Execute message
d = Deferred()
if self.request.type == message.MSG_CONNECT:
raise NotImplementedError("No action provided for MSG_CONNECT")
elif self.request.type == message.MSG_CONNECTOK:
# We only reach this spot if it is a valid message type so we're in client handshake mode. Update
# protocol to support client __call__ execution (i.e. invocation)
self.required_message_types = [message.MSG_CONNECTOK]
raise NotImplementedError("No action provided for MSG_CONNECTOK")
elif self.request.type == message.MSG_CONNECTFAIL:
raise NotImplementedError("No action provided for MSG_CONNECTFAIL")
elif self.request.type == message.MSG_INVOKE:
log("Responding to invoke request.")
# Must be a static method so the Protocol can reset after oneway messages
d.addCallback(self._pyro_remote_call)
#.........这里部分代码省略.........