Ejemplo n.º 1
0
    def test_pack_exceptions(self):
        for (name, obj, exception) in pack_exception_test_vectors:
            obj_repr = repr(obj)
            print("\tTesting %s: object %s" %
                  (name, obj_repr if len(obj_repr) < 24 else obj_repr[0:24] + "..."))

            with self.assertRaises(exception):
                umsgpack.packb(obj)
Ejemplo n.º 2
0
 def test_pack_exceptions(self):
     for (name, obj, exception) in pack_exception_test_vectors:
         obj_repr = repr(obj)
         print("\tTesting %s: object %s" % (name, obj_repr if len(obj_repr) < 24 else obj_repr[0:24] + "..."))
         try:
             umsgpack.packb(obj)
         except Exception as e:
             self.assertTrue(isinstance(e, exception))
Ejemplo n.º 3
0
def packet_send(string):
	random_id = hash(string) % 768 + randint(0, 256)	# get and ID by hashing the string
	packet_num = 1							# starting number (from data)
	for piece in [string[x:x+40] for x in range(0,len(string),40)]:			# divide string, create and send packets
		packet = {"id": random_id, "pn": packet_num, "dt": piece}			# compose data packet
		xbee.send("tx", dest_addr=XBEE_MESH_DESC, data=packb(packet))		# serialize and send
		packet_num += 1														# increase packet count
	packet = {"id": random_id, "pn": 0, "dt": packet_num}					# compose header packet
	xbee.send("tx", dest_addr=XBEE_MESH_DESC, data=packb(packet))			# serialize and send
Ejemplo n.º 4
0
def password_hash(word, salt=None, iterations=config.pbkdf2_iterations):
    if salt is None:
        salt = random.read(16)
    elif len(salt) > 16:
        _, salt, iterations = umsgpack.unpackb(salt)

    word = umsgpack.packb(word)

    rawhash = PBKDF2(word, salt, iterations).read(32)

    return umsgpack.packb([rawhash, salt, iterations])
Ejemplo n.º 5
0
def aes_encrypt(word, key=config.aes_key, iv=None):
    if iv is None:
        iv = random.read(16)

    word = umsgpack.packb(word)
    mod = len(word) % 16
    if mod != 0:
        word += '\0' * (16-mod)

    aes = AES.new(key, AES.MODE_CBC, iv)
    ciphertext = aes.encrypt(word)

    return umsgpack.packb([ciphertext, iv])
Ejemplo n.º 6
0
def getToken():
    print "Requesting Token"
    print ":".join("{0:x}".format(ord(c)) for c in umsgpack.packb(['T']))
    
    s.send(umsgpack.packb(['T']))
    token = umsgpack.unpackb(s.recv(1024))
    print "Token Recived:",token
    crc = token.pop()
    if struct.unpack('<H', crc)[0] == crc16.crc16(''.join(token)):
        print "Token Valid - CRC passed"
    else:
        print "Token INVALID", crc16.crc16(token[1]), struct.unpack('<H', crc)[0]
    
    return token[1]
Ejemplo n.º 7
0
 def _sendResponse(self, response, msgID, address):
     if self.noisy:
         log.msg("sending response for msg id %s to %s" % (b64encode(msgID), address))
     timeout = reactor.callLater(self._waitTimeout, self._removeStaleId, msgID)
     txdata = '\x01%s%s' % (msgID, umsgpack.packb(response))
     self._outstanding[msgID] = (txdata, timeout)
     self.transport.write(txdata, address)
Ejemplo n.º 8
0
    def _http_request_func(self, path, payload):
        url_api = '%s%s' % (self.base_uri, path)
        http_headers = {
            'Content-Type': 'application/x-msgpack',
        }
        mp_payload = ''.join(map(chr, umsgpack.packb(payload)))

        try:
            pool = urllib3.PoolManager(timeout=3.0)
            req = pool.urlopen(
                'POST',
                url_api,
                headers=http_headers,
                body=mp_payload,
            )
            return json.loads(req.data.decode('utf-8'))

        except urllib3.exceptions.MaxRetryError:
            fallback = self._fallback  # or .... or ...

            if fallback:
                IkaUtils.dprint(
                    '%s: Remote API Error. Falling back to local mode' % self)
                return self._local_request_func(path, payload)

        raise Exception(
            'API Error: Failed to connect to API endpoint. (No fallback)')
Ejemplo n.º 9
0
    def test_invalid_peer_type(self):
        created = base.create(self.btctxstore, self.wif, "peers", None)

        # repack to eliminate namedtuples and simulate io
        repacked = umsgpack.unpackb(umsgpack.packb(created))

        self.assertIsNone(peers.read(self.btctxstore, repacked))
Ejemplo n.º 10
0
    def build_delay_message(self,
                            task_id: str = None,
                            function_name: str = None,
                            args: List[str] = None,
                            kwargs: Dict = None) -> str:
        """
        If values are not specified, it will be taken from class atributes
        
        :return: message as a string 
        :rtype: str
        """
        if task_id is None:
            task_id = uuid.uuid4().hex

        if function_name is None:
            function_name = self.function_name

        if args is None:
            args = self.args

        if kwargs is None:
            kwargs = self.kwargs

        return msgpack.packb(
            dict(task_id=task_id,
                 function=function_name,
                 args=args,
                 kwargs=kwargs),
            use_bin_type=True)
def srvc_ask(identity, address, message):
    identity = '{}{}'.format('id_', identity)
    address = address
    pack_msg = umsgpack.packb(message)
    context, socket = setup_ask_socket(address, identity)
    log.debug('Client %s started\n' % identity)
    poll = zmq.Poller()
    poll.register(socket, zmq.POLLIN)

    socket.send(pack_msg)
    log.debug('Req from client %s sent.\n' % identity)

    response = None
    received_reply = False
    while not received_reply:
        sockets = dict(poll.poll(1000))
        if socket in sockets:
            if sockets[socket] == zmq.POLLIN:
                response = socket.recv()
                log.debug('Client %s received reply: %s\n' % (identity, response))
                received_reply = True

    socket.close()
    context.term()
    return response
Ejemplo n.º 12
0
 def process(self, message):
     datagram, host, port =  umsgpack.unpackb(message[0])
     reply = self.processAuth(datagram, host, port)
     logger.info("[Radiusd] :: Send radius response: %s" % repr(reply))
     if self.config.system.debug:
         logger.debug(reply.format_str())
     self.pusher.push(umsgpack.packb([reply.ReplyPacket(),host,port]))
Ejemplo n.º 13
0
 def save_cache(self):
     """Write current in-memory config to cache file."""
     LOG.info("Writing settings to cache file '%s'.", self.cache_file)
     with open(self.cache_file, "wb") as stream:
         dicts = [Subscription.Subscription.encode_subscription(s) for s in self.subscriptions]
         packed = umsgpack.packb(dicts)
         stream.write(packed)
Ejemplo n.º 14
0
    def __non_len_string(self):
        # type: (InternalMessage) -> bytes
        """Returns a :py:class:`bytes` object containing the entire message,
        excepting the total length header

        Raises:

            TypeError: If any of the arguments are not serializable. This
                        means your objects must be one of the following:

                        - :py:class:`bool`
                        - :py:class:`float`
                        - :py:class:`int` (if ``2**64 > x > -2**63``)
                        - :py:class:`str`
                        - :py:class:`bytes`
                        - :py:class:`unicode`
                        - :py:class:`tuple`
                        - :py:class:`list`
                        - :py:class:`dict` (if all keys are
                            :py:class:`unicode`)
        """
        if not self.__string:
            try:
                self.__string = packb(self.packets)
            except UnsupportedTypeException as e:
                raise TypeError(*e.args)
        return self.__string
Ejemplo n.º 15
0
def read(btctxstore, message):
    # FIXME make sure body does not contain dicts

    if not isinstance(message, list):
        return None
    if len(message) != 5:
        return None
    msg = Message(*message)
    if not isinstance(msg.sender, bytes):
        return None
    if len(msg.sender) != 20:
        return None
    if not isinstance(msg.version, int):
        return None
    if msg.version < 0:
        return None
    # token and body must be checked by caller
    if not isinstance(msg.rawsig, bytes):
        return None
    if len(msg.rawsig) != 65:
        return None

    # verify signature
    address = node_id_to_address(msg.sender)
    signature = base64.b64encode(msg.rawsig)
    data = binascii.hexlify(umsgpack.packb([msg.version, msg.token, msg.body]))
    if btctxstore.verify_signature(address, signature, data):
        return msg
    return None
Ejemplo n.º 16
0
    def test_pack_composite(self):
        for (name, obj, data) in composite_test_vectors:
            obj_repr = repr(obj)
            print("\tTesting %s: object %s" %
                  (name, obj_repr if len(obj_repr) < 24 else obj_repr[0:24] + "..."))

            self.assertEqual(umsgpack.packb(obj), data)
Ejemplo n.º 17
0
    def run(self):
        """
        Continuously monitor the A/D
        :return:
        """
        value = None
        while True:
            try:
                for entry in self.pin_states:
                    if entry['enabled']:
                        if entry['mode'] == 'analog':
                            value = ADC.read(entry['pin'])
                            value = round(value, 4)

                        elif entry['mode'] == 'sonar':
                            value = ADC.read_raw(entry['pin'])
                            value = self.convert_to_distance(value)

                        digital_reply_msg = umsgpack.packb({u"command": "analog_read", u"pin": entry['pin'],
                                                            u"value": str(value)})

                        envelope = ("B" + self.board_num).encode()
                        self.publisher.send_multipart([envelope, digital_reply_msg])
                time.sleep(0.05)
            except KeyboardInterrupt:
                sys.exit(0)
Ejemplo n.º 18
0
    def run(self):
        """
        Retrieve sonar data and send report
        :return:
        """

        if not self._inited:
            self.cancel()

        end = time.time() + 600.0

        while time.time() < end:
            x = self.read()
            if x:
                # calculate round trip time
                x = x / 1000000.0 * 34030.0

                # calculate distance and round it off
                x /= 2
                x = round(x, 2)

                # publish the data

                digital_reply_msg = umsgpack.packb(
                    {u"command": "digital_read", u"pin": str(self._trig), u"value": str(x)}
                )

                envelope = ("B" + self.board_num).encode()
                self.publisher.send_multipart([envelope, digital_reply_msg])
                time.sleep(0.03)
Ejemplo n.º 19
0
    def post_payload(self, payload, api_key=None):
        if self.dry_run:
            IkaUtils.dprint(
                '%s: Dry-run mode, skipping POST to stat.ink.' % self)
            return

        url_statink_v1_battle = 'https://stat.ink/api/v1/battle'

        if api_key is None:
            api_key = self.api_key

        if api_key is None:
            raise('No API key specified')

        http_headers = {
            'Content-Type': 'application/x-msgpack',
        }

        # Payload data will be modified, so we copy it.
        # It is not deep copy, so only dict object is
        # duplicated.
        payload = payload.copy()
        payload['apikey'] = api_key
        mp_payload_bytes = umsgpack.packb(payload)
        mp_payload = ''.join(map(chr, mp_payload_bytes))

        pool = urllib3.PoolManager()
        req = pool.urlopen('POST', url_statink_v1_battle,
                           headers=http_headers,
                           body=mp_payload,
                           )

        if self.show_response_enabled:
            print(req.data.decode('utf-8'))
Ejemplo n.º 20
0
    def test_invalid_name(self):
        created = signal.create(self.btctxstore, self.wif, "test")

        # repack to eliminate namedtuples and simulate io
        repacked = umsgpack.unpackb(umsgpack.packb(created))

        self.assertIsNone(signal.read(self.btctxstore, repacked, "wrongname"))
Ejemplo n.º 21
0
    def post(self):
        email = self.get_argument('email')
        password = self.get_argument('password')
        if not email or not password:
            self.render('login.html', password_error=u'请输入用户名和密码', email=email)
            return

        if self.db.user.challenge(email, password):
            user = self.db.user.get(email=email, fields=('id', 'email', 'nickname', 'role'))
            if not user:
                self.render('login.html', password_error=u'不存在此邮箱或密码错误', email=email)
                return

            setcookie = dict(
                    expires_days=config.cookie_days,
                    httponly=True,
                    )
            if config.https:
                setcookie['secure'] = True
            self.set_secure_cookie('user', umsgpack.packb(user), **setcookie)
            self.db.user.mod(user['id'], atime=time.time(), aip=self.ip2int)
            
            next = self.get_argument('next', '/my/')
            self.redirect(next)
        else:
            self.evil(+5)
            self.render('login.html', password_error=u'不存在此邮箱或密码错误', email=email)
Ejemplo n.º 22
0
 def trigger_all(self, function, parameter):
     packed = umsgpack.packb(parameter)
     err = tvio_input_trigger_all(self._input,
                                  c_char_p(function.encode("ascii")),
                                  packed,
                                  len(packed))
     _check_error(err)
Ejemplo n.º 23
0
    def report_i2c_data(self, data):
        # create a topic specific to the board number of this board
        envelope = ("B" + self.board_num).encode()

        msg = umsgpack.packb({u"command": "i2c_reply", u"board": self.board_num, u"data": data})

        self.publisher.send_multipart([envelope, msg])
Ejemplo n.º 24
0
 def put_nowait(self, obj):
     if self.lazy_limit and self.last_qsize < self.maxsize:
         pass
     elif self.full():
         raise self.Full
     self.last_qsize = self.redis.rpush(self.name, umsgpack.packb(obj))
     return True
Ejemplo n.º 25
0
    def test_invalid_info_len(self):
        created = base.create(self.btctxstore, self.wif, "info", [])

        # repack to eliminate namedtuples and simulate io
        repacked = umsgpack.unpackb(umsgpack.packb(created))

        self.assertIsNone(info.read(self.btctxstore, repacked))
Ejemplo n.º 26
0
def test_message_serializer_deserialize_completion_response():
    # TODO should start with a packed message and use msgpack to unpack, for now start with builtin form:
    unpacked = {
        '_message': 'CompletionResponse',
        'token': 'thetoken',
        'start': 11,
        'end': 12,
        'limitExceeded': True,
        'options': [
            {'insert': 'insert', 'desc': 'thedescription', 'semantics': 'string', 'extensionId': 'theExtId'},
            {'insert': 'insert2', 'desc': 'thedescription2', 'semantics': 'identifier', 'extensionId': 'theExtId2'}
        ]
    }

    packed = umsgpack.packb(unpacked)

    # and use serializer without unpacker:
    serializer = MessageSerializer()

    msg = serializer.deserialize(packed)

    expected = CompletionResponse(11, 12, True, [CompletionOption('insert', 'thedescription', semantics=SemanticType.string, extensionId='theExtId'),
                                                 CompletionOption('insert2', 'thedescription2', semantics=SemanticType.identifier, extensionId='theExtId2')],
                                  'thetoken')

    # avoid implementation of eq in schema classes, so rely on correct serialization for now:
    assert serializer.serialize(msg) == serializer.serialize(expected)
Ejemplo n.º 27
0
 def wrapper(self, *args, **kwargs):
     if not hasattr(self, '_cache'):
         self._cache = dict()
     key = umsgpack.packb((args, kwargs))
     if key not in self._cache:
         self._cache[key] = fn(self, *args, **kwargs)
     return self._cache[key]
Ejemplo n.º 28
0
    def test_invalid_version_value(self):
        _info = ["invalidversion", None, None, None]
        created = base.create(self.btctxstore, self.wif, "info", _info)

        # repack to eliminate namedtuples and simulate io
        repacked = umsgpack.unpackb(umsgpack.packb(created))

        self.assertIsNone(info.read(self.btctxstore, repacked))
Ejemplo n.º 29
0
    def test_invalid_platform_len(self):
        _info = ["0.0.0", [2, 1, 1], [["127.0.0.1", 1337], "unl", True], []]
        created = base.create(self.btctxstore, self.wif, "info", _info)

        # repack to eliminate namedtuples and simulate io
        repacked = umsgpack.unpackb(umsgpack.packb(created))

        self.assertIsNone(info.read(self.btctxstore, repacked))
Ejemplo n.º 30
0
    def test_invalid_storage_value_types(self):
        _info = ["0.0.0", [None, 0, 0], None, None]
        created = base.create(self.btctxstore, self.wif, "info", _info)

        # repack to eliminate namedtuples and simulate io
        repacked = umsgpack.unpackb(umsgpack.packb(created))

        self.assertIsNone(info.read(self.btctxstore, repacked))
Ejemplo n.º 31
0
# Request a token
token = getToken()

# Send Hello Message
device_len = struct.pack('<B', len(device_name))
#print len(device_name)
mac_add = ''
for octet in mac_address.split(":"):
    mac_add += struct.pack('<B', int(octet, 16))
v_num = struct.pack('<B', version_num)
send_hello = ['H', device_name, mac_add, v_num]
send_hello.append(
    struct.pack('<H', crc16.crc16(''.join(send_hello) + device_salt + token)))

print send_hello
print ":".join("{0:x}".format(ord(c)) for c in umsgpack.packb(send_hello))
s.send(umsgpack.packb(send_hello))
hello = umsgpack.unpackb(s.recv(1024))
if checkResponce('H', hello):
    print "Hello, PASS"
else:
    print "Hello, FAIL"

# ------------ Send a Heartbeat ------------
print "\nTesting... HeartBeat"

send_heartBeat = ['B', mac_add]
print ''.join(send_heartBeat)
send_heartBeat.append(struct.pack('<H', crc16.crc16(''.join(send_heartBeat))))
print send_heartBeat
s.send(umsgpack.packb(send_heartBeat))
Ejemplo n.º 32
0
 def render(self, data: http.ResponseData) -> bytes:
     if has_msgpack:
         return msgpack.packb(data)
     else:
         return umsgpack.packb(data, force_float_precision=self.precision)
Ejemplo n.º 33
0
f = BigWig(file)
for u in range(1, 1):
    for x in range(1, 1):
        s = random.randint(1, 500)
        r = 10**(u + 3) + s
        print("testing for range ", s, r)
        result, _ = f.getRange('chr1', s, r)
        formatted_result = format_result(result, params)
        # print(formatted_result)
        print("size of formatted result")
        print(sys.getsizeof(formatted_result))

        print("original DF size")
        print(sys.getsizeof(result))
        t1 = time.time()
        ms = umsgpack.packb(formatted_result)
        t1 = time.time() - t1
        t2 = time.time()
        temp = umsgpack.unpackb(ms)
        t2 = time.time() - t2
        # disk = str(10**(u+3)+x) + ".msg.testfile"
        # with open(disk, 'wb') as wr:
        #     wr.write(bytearray(ms))
        #     wr.close()
        print("time to compress to msgpack: ", t1, "read from msgpack: ", t2)
        print("msgpack size: ", sys.getsizeof(ms))
        mst1 = t1
        mst2 = t2
        t1 = time.time()
        js = json.dumps(formatted_result)
        t1 = time.time() - t1
Ejemplo n.º 34
0
 def sync_fetch(task):
     result = self.sync_fetch(task)
     result = Binary(umsgpack.packb(result))
     return result
Ejemplo n.º 35
0
#!/usr/bin/env python

import umsgpack

packed = umsgpack.packb({u'compact': True, u'schema': 0})
unpacked = umsgpack.unpackb(packed)
print packed
print unpacked

with open('test.bin', 'w') as f:
    umsgpack.pack({u'compact': True, u'schema': 0}, f)
    umsgpack.pack([1, 2, 3], f)

with open('test.bin') as f:
    print umsgpack.unpack(f)
    print umsgpack.unpack(f)
Ejemplo n.º 36
0
 def wrapper(*args, **kwargs):
     key = umsgpack.packb((args, kwargs), use_bin_type=True)
     if key not in _cache:
         _cache[key] = f(*args, **kwargs)
     return _cache[key]
Ejemplo n.º 37
0
 def emit(self, inparams, outparams):
     libexport.Emit(self.id, umsgpack.packb(inparams),
                    umsgpack.packb(outparams))
Ejemplo n.º 38
0
    def post(self):
        siteconfig = self.db.site.get(1, fields=('regEn', 'MustVerifyEmailEn'))
        regEn = siteconfig['regEn']
        regFlg = False if regEn == 0 else True
        MustVerifyEmailEn = siteconfig['MustVerifyEmailEn']
        email = self.get_argument('email')
        password = self.get_argument('password')

        if not email:
            self.render('register.html', email_error=u'请输入邮箱', regFlg=regFlg)
            return
        if email.count('@') != 1 or email.count('.') == 0:
            self.render('register.html', email_error=u'邮箱格式不正确', regFlg=regFlg)
            return
        if len(password) < 6:
            self.render('register.html',
                        password_error=u'密码需要大于6位',
                        email=email,
                        regFlg=regFlg)
            return

        user = self.db.user.get(email=email,
                                fields=('id', 'email', 'email_verified',
                                        'nickname', 'role'))
        if (user == None):
            if (regEn == 1):
                self.evil(+5)
                try:
                    self.db.user.add(email=email,
                                     password=password,
                                     ip=self.ip2int)
                except self.db.user.DeplicateUser as e:
                    self.evil(+3)
                    self.render('register.html',
                                email_error=u'email地址已注册',
                                regFlg=regFlg)
                    return
                user = self.db.user.get(email=email,
                                        fields=('id', 'email', 'nickname',
                                                'role'))

                setcookie = dict(
                    expires_days=config.cookie_days,
                    httponly=True,
                )
                if config.https:
                    setcookie['secure'] = True
                self.set_secure_cookie('user', umsgpack.packb(user),
                                       **setcookie)

                if siteconfig['MustVerifyEmailEn'] != 1:
                    next = self.get_argument('next', '/my/')
                    self.redirect(next)
                else:
                    self.render('register.html',
                                email_error=u'请验证邮箱后再登陆',
                                regFlg=regFlg)
                future = self.send_mail(user)
                if future:
                    IOLoop.current().add_future(future, lambda x: x)

            else:
                self.render('register.html',
                            email_error=u'管理员关闭注册',
                            regFlg=regFlg)
            return
        else:
            if (MustVerifyEmailEn == 1):
                if (user['email_verified'] != 1):
                    self.render('register.html',
                                email_error=u'email地址未验证,邮件已发送,请验证邮件后登陆')
                    future = self.send_mail(user)
                    if future:
                        IOLoop.current().add_future(future, lambda x: x)
                else:
                    self.render('register.html',
                                email_error=u'email地址已注册',
                                regFlg=regFlg)
            else:
                self.render('register.html',
                            email_error=u'email地址已注册',
                            regFlg=regFlg)
        return
Ejemplo n.º 39
0
 def __serialize(self, msg: any) -> bytearray:
     return bytearray(msgpack.packb(msg))
Ejemplo n.º 40
0
training_total = 0
testing_total = 0

for subject in training_map:
    training_total += len(training_map[subject])

for subject in training_map:
    testing_total += len(testing_map[subject])

print("> Training images:", training_total)
print("> Testing images:", testing_total)

training_out = open("v1_training.dat", "wb")
testing_out = open("v1_testing.dat", "wb")

training_out.write(umsgpack.packb(training_total))
testing_out.write(umsgpack.packb(testing_total))

subject_image_current_id = {s: 0 for s in subject_image_map}


def one_hot(sid):
    oh = [0 for i in range(0, len(subject_image_map))]
    oh[sid] = 1

    return oh


print("> Writing data...")
for i in tqdm(range(0, num_images)):
    (subject, image) = umsgpack.unpack(in_file)
Ejemplo n.º 41
0
def api_msgpack():
    bottle.response.content_type = APP_MSGPACK
    return umsgpack.packb(API_RESPONSE)
Ejemplo n.º 42
0
def encode_msgpack(data):
    try:
        return umsgpack.packb(data)
    except umsgpack.UnsupportedTypeException as e:
        raise ValueError('Cannot convert data to MessagePack ({0})'.format(e))
Ejemplo n.º 43
0
 def dumps(self, obj):
     """ Dump object to bytes array. """
     return umsgpack.packb(obj, ext_handlers=self._ext_dump())
Ejemplo n.º 44
0
 def pack_tx(self, transaction):
     return umsgpack.packb(transaction.as_full_list())
Ejemplo n.º 45
0
    [ "16-bit raw", u"b"*32, b"\xda\x00\x20" + b"b"*32 ],
    [ "16-bit raw", b"b"*32, b"\xda\x00\x20" + b"b"*32 ],
    [ "16-bit raw", u"b"*256, b"\xda\x01\x00" + b"b"*256 ],
    [ "16-bit raw", b"b"*256, b"\xda\x01\x00" + b"b"*256 ],
    [ "16-bit raw", u"c"*65535, b"\xda\xff\xff" + b"c"*65535 ],
    [ "16-bit raw", b"c"*65535, b"\xda\xff\xff" + b"c"*65535 ],
    # 32-bit Raw
    [ "32-bit raw", u"b"*65536, b"\xdb\x00\x01\x00\x00" + b"b"*65536 ],
    [ "32-bit raw", b"b"*65536, b"\xdb\x00\x01\x00\x00" + b"b"*65536 ],
]

CustomType = namedtuple('CustomType', ['x', 'y', 'z'])

ext_handlers = {
    complex: lambda obj: umsgpack.Ext(0x20, struct.pack("ff", obj.real, obj.imag)),
    CustomType: lambda obj: umsgpack.Ext(0x30, umsgpack.packb(list(obj))),
    0x20: lambda ext: complex(*struct.unpack("ff", ext.data)),
    0x30: lambda ext: CustomType(*umsgpack.unpackb(ext.data)),
}

ext_handlers_test_vectors = [
    [ "complex", complex(1, 2), b"\xd7\x20\x00\x00\x80\x3f\x00\x00\x00\x40" ],
    [ "custom type", CustomType(b"abc", 123, True), b"\xd7\x30\x93\xc4\x03\x61\x62\x63\x7b\xc3" ],
]

# These are the only global variables that should be exported by umsgpack
exported_vars_test_vector = [
    "Ext",
    "InvalidString",
    "PackException",
    "UnpackException",
Ejemplo n.º 46
0
 def test_pack_exceptions(self):
     for (name, obj, exception) in pack_exception_test_vectors:
         obj_repr = repr(obj)
         print("\tTesting %s: object %s" % (name, obj_repr if len(obj_repr) < 24 else obj_repr[0:24] + "..."))
         with self.assertRaises(exception):
             umsgpack.packb(obj)
Ejemplo n.º 47
0
import socket
import umsgpack

SERVER_IP = "127.0.0.1"
SERVER_PORT = 5656

outgoing_json = {"username": input("Enter Username: "******"password": input("Enter Password: "******"101" + umsgpack.packb(outgoing_json)

sock.send(output)
print("waiting for response")
response = sock.recv(1024)
print(umsgpack.unpackb(response[3:]))
output = b"112"
sock.send(output)
print("waiting for response")
response = sock.recv(1024)
print(umsgpack.unpackb(response[3:]))
output = b"111"
sock.send(output)
print("waiting for response")
response = sock.recv(1024)
print(umsgpack.unpackb(response[3:]))
import umsgpack


def validate_msgpacked(m_pkt):
    if type(m_pkt) == type([]):
        if len(m_pkt) == 3:
            if type(m_pkt[0]) == type(bytes()) and type(m_pkt[1]) == type(
                    bytes()) and type(m_pkt[2]) == type(bytes()):
                return True
    return False


while True:
    #Read (and deserialize) an encrypted packet from STDIN
    msgpack_pkt = umsgpack.unpack(sys.stdin.buffer)

    if not validate_msgpacked(msgpack_pkt):
        continue

    sender = msgpack_pkt[0]
    receiver = msgpack_pkt[1]
    payload = msgpack_pkt[2]
    nonce = payload[0:24]
    sys.stderr.write(
        "Packet of length {} bytes from {} to {}. NONCE = {}\n".format(
            len(payload), sender.hex(), receiver.hex(), nonce.hex()))

    #Keep the pipe moving along
    sys.stdout.buffer.write(umsgpack.packb(msgpack_pkt))
    sys.stdout.buffer.flush()
Ejemplo n.º 49
0
 def start(self):
     '''
     Take the messages from the queue,
     inspect and identify the operating system,
     then queue the message correspondingly.
     '''
     self._setup_ipc()
     # Start suicide polling thread
     thread = threading.Thread(target=self._suicide_when_without_parent,
                               args=(os.getppid(), ))
     thread.start()
     signal.signal(signal.SIGTERM, self._exit_gracefully)
     # If were are to log all processed syslogs we need to initiate the class
     if self.logger:
         self._setup_log_syslog_transport()
     self.__up = True
     while self.__up:
         # Take messages from the main queue
         # bin_obj = self.sub.recv()
         # msg, address = umsgpack.unpackb(bin_obj, use_list=False)
         try:
             bin_obj = self.sub.recv()
             msg, address = umsgpack.unpackb(bin_obj, use_list=False)
         except zmq.ZMQError as error:
             if self.__up is False:
                 log.info('Exiting on process shutdown')
                 return
             else:
                 log.error(error, exc_info=True)
                 raise NapalmLogsExit(error)
         if six.PY3:
             msg = str(msg, 'utf-8')
         else:
             msg = msg.encode('utf-8')
         log.debug('[%s] Dequeued message from %s: %s', address, msg,
                   time.time())
         dev_os, msg_dict = self._identify_os(msg)
         log.debug('Identified OS: %s', dev_os)
         if self.logger:
             if self.logger_opts.get('send_raw') and dev_os:
                 self._send_log_syslog(dev_os, msg_dict)
             elif self.logger_opts.get('send_unknown') and not dev_os:
                 self._send_log_syslog(UNKNOWN_DEVICE_NAME,
                                       {'message': msg})
         if dev_os and dev_os in self.started_os_proc:
             # Identified the OS and the corresponding process is started.
             # Then send the message in the right queue
             # obj = (msg_dict, address)
             # bin_obj = umsgpack.packb(obj)
             log.debug('Queueing message to %s', dev_os)
             # self.pubs[dev_os].send(bin_obj)
             if six.PY3:
                 dev_os = bytes(dev_os, 'utf-8')
             self.pub.send_multipart(
                 [dev_os, umsgpack.packb((msg_dict, address))])
             # self.os_pipes[dev_os].send((msg_dict, address))
         elif dev_os and dev_os not in self.started_os_proc:
             # Identified the OS, but the corresponding process does not seem to be started.
             log.info(
                 'Unable to queue the message to %s. Is the sub-process started?',
                 dev_os)
         elif not dev_os and self.publisher_opts.get('send_unknown'):
             # OS not identified, but the user requested to publish the message as-is
             log.debug(
                 'Publishing message, although not identified, as requested'
             )
             if six.PY3:
                 dev_os = bytes(UNKNOWN_DEVICE_NAME, 'utf-8')
             else:
                 dev_os = UNKNOWN_DEVICE_NAME
             self.pub.send_multipart(
                 [dev_os,
                  umsgpack.packb(({
                      'message': msg
                  }, address))])
             # self.os_pipes[UNKNOWN_DEVICE_NAME].send(({'message': msg}, address))
         log.info('No action requested. Ignoring.')
Ejemplo n.º 50
0
def pack(dic):
    return base64.b64encode(umsgpack.packb(dic)).decode('UTF-8')
    def run(self):
        try:
            os.unlink(self.SOCK_PATH)
        except OSError:
            if os.path.exists(self.SOCK_PATH):
                raise

        sock_type = socket.AF_UNIX
        if (self.args.tcp):
            sock_type = socket.AF_INET
        sock = socket.socket(sock_type, socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.bind(self.args.lola_addr)
        sock.listen()
        sock.settimeout((self.timeStep - 1) / 1000.0)

        self.print_status()

        conn = None
        imgCounter = self.frametime

        while robot.step(self.timeStep) != -1:
            self.key = self.keyboard.getKey()

            if conn:
                self.updateSensors()
                try:
                    # send sensor data to LoLa client
                    conn.send(umsgpack.packb(self.sensors))
                    data = conn.recv(self.ACTUATOR_PKT_SIZE * 3)
                    if data:
                        self.updateActuators(umsgpack.unpackb(data))
                except TimeoutError:
                    print(AnsiCodes.RED_FOREGROUND +
                          "Timeout while waiting for LoLa actuators." +
                          AnsiCodes.RESET)
                except ConnectionError:
                    conn.close()
                    conn = None
                    print(AnsiCodes.RED_FOREGROUND +
                          "LoLa client disconnected." + AnsiCodes.RESET)

                # send images on average every self.frametime milliseconds in simulation time
                imgCounter -= self.timeStep
                if self.args.camera and imgCounter <= 0:
                    self.topImageServer.send(self.tick,
                                             self.cameraTop.getImage())
                    self.bottomImageServer.send(self.tick,
                                                self.cameraBottom.getImage())
                    imgCounter += self.frametime
            else:
                try:
                    (conn, addr) = sock.accept()
                    conn.send(umsgpack.packb(self.sensors))
                    print(AnsiCodes.GREEN_FOREGROUND +
                          "LoLa client connected." + AnsiCodes.RESET)
                except:
                    conn = None
                    continue

        # remove socket on exit
        os.unlink(self.SOCK_PATH)

        # stop image threads
        if self.args.camera:
            self.topImageServer.stop()
            self.bottomImageServer.stop()
Ejemplo n.º 52
0
    def start(self):
        '''
        Take the messages from the queue,
        inspect and identify the operating system,
        then queue the message correspondingly.
        '''
        # metric counters
        napalm_logs_server_messages_received = Counter(
            "napalm_logs_server_messages_received",
            "Count of messages received from listener processes")
        napalm_logs_server_skipped_buffered_messages = Counter(
            'napalm_logs_server_skipped_buffered_messages',
            'Count of messages skipped as they were already buffered',
            ['device_os'])
        napalm_logs_server_messages_with_identified_os = Counter(
            "napalm_logs_server_messages_with_identified_os",
            "Count of messages with positive os identification", ['device_os'])
        napalm_logs_server_messages_without_identified_os = Counter(
            "napalm_logs_server_messages_without_identified_os",
            "Count of messages with negative os identification")
        napalm_logs_server_messages_failed_device_queuing = Counter(
            "napalm_logs_server_messages_failed_device_queuing",
            "Count of messages per device os that fail to be queued to a device process",
            ['device_os'])
        napalm_logs_server_messages_device_queued = Counter(
            "napalm_logs_server_messages_device_queued",
            "Count of messages queued to device processes", ['device_os'])
        napalm_logs_server_messages_unknown_queued = Counter(
            "napalm_logs_server_messages_unknown_queued",
            "Count of messages queued as unknown")
        self._setup_ipc()
        # Start suicide polling thread
        cleanup = threading.Thread(target=self._cleanup_buffer)
        cleanup.start()
        thread = threading.Thread(target=self._suicide_when_without_parent,
                                  args=(os.getppid(), ))
        thread.start()
        signal.signal(signal.SIGTERM, self._exit_gracefully)
        self.__up = True
        while self.__up:
            # Take messages from the main queue
            try:
                bin_obj = self.sub.recv()
                msg, address = umsgpack.unpackb(bin_obj, use_list=False)
            except zmq.ZMQError as error:
                if self.__up is False:
                    log.info('Exiting on process shutdown')
                    return
                else:
                    log.error(error, exc_info=True)
                    raise NapalmLogsExit(error)
            if six.PY3:
                msg = str(msg, 'utf-8')
            else:
                msg = msg.encode('utf-8')
            log.debug('[%s] Dequeued message from %s: %s', address, msg,
                      time.time())
            napalm_logs_server_messages_received.inc()
            os_list = self._identify_os(msg)

            for dev_os, msg_dict in os_list:
                if dev_os and dev_os in self.started_os_proc:
                    # Identified the OS and the corresponding process is started.
                    # Then send the message in the right queue
                    log.debug('Identified OS: %s', dev_os)
                    log.debug('Queueing message to %s', dev_os)
                    if six.PY3:
                        dev_os = bytes(dev_os, 'utf-8')
                    if self._buffer:
                        message = '{dev_os}/{host}/{msg}'.format(
                            dev_os=dev_os,
                            host=msg_dict['host'],
                            msg=msg_dict['message'])
                        message_key = base64.b64encode(message)
                        if self._buffer[message_key]:
                            log.info(
                                '"%s" seems to be already buffered, skipping',
                                msg_dict['message'])
                            napalm_logs_server_skipped_buffered_messages.labels(
                                device_os=dev_os.decode()).inc()
                            continue
                        log.debug('"%s" is not buffered yet, added',
                                  msg_dict['message'])
                        self._buffer[message_key] = 1
                    self.pub.send_multipart(
                        [dev_os, umsgpack.packb((msg_dict, address))])
                    # self.os_pipes[dev_os].send((msg_dict, address))
                    napalm_logs_server_messages_with_identified_os.labels(
                        device_os=dev_os.decode()).inc()
                    napalm_logs_server_messages_device_queued.labels(
                        device_os=dev_os.decode()).inc()

                elif dev_os and dev_os not in self.started_os_proc:
                    # Identified the OS, but the corresponding process does not seem to be started.
                    log.info(
                        'Unable to queue the message to %s. Is the sub-process started?',
                        dev_os)
                    napalm_logs_server_messages_with_identified_os.labels(
                        device_os=dev_os.decode()).inc()
                    napalm_logs_server_messages_failed_device_queuing.labels(
                        device_os=dev_os.decode()).inc()

                elif not dev_os and self.opts['_server_send_unknown']:
                    # OS not identified, but the user requested to publish the message as-is
                    log.debug(
                        'Unable to identify the OS, sending directly to the publishers'
                    )
                    to_publish = {
                        'ip': address,
                        'host': 'unknown',
                        'timestamp': int(time.time()),
                        'message_details': msg_dict,
                        'os': UNKNOWN_DEVICE_NAME,
                        'error': 'UNKNOWN',
                        'model_name': 'unknown'
                    }
                    self.publisher_pub.send(umsgpack.packb(to_publish))
                    napalm_logs_server_messages_unknown_queued.inc()
                    napalm_logs_server_messages_without_identified_os.inc()
Ejemplo n.º 53
0
 def test_pack_composite(self):
     for (name, obj, data) in composite_test_vectors:
         obj_repr = repr(obj)
         print("\tTesting %s: object %s" % (name, obj_repr if len(obj_repr) < 24 else obj_repr[0:24] + "..."))
         self.assertEqual(umsgpack.packb(obj), data)
Ejemplo n.º 54
0
 def wrapper(*args, **kwargs):
     key = umsgpack.packb((args, kwargs))
     if key not in _cache:
         _cache[key] = f(*args, **kwargs)
     return _cache[key]
Ejemplo n.º 55
0
    def post(self):
        email = self.get_argument('email')
        password = self.get_argument('password')
        siteconfig = self.db.site.get(1, fields=('MustVerifyEmailEn'))
        regFlg = False if self.db.site.get(
            1, fields=('regEn'))['regEn'] == 0 else True
        if not email or not password:
            self.render('login.html',
                        password_error=u'请输入用户名和密码',
                        email=email,
                        regFlg=regFlg)
            return

        user_try = self.db.user.get(email=email,
                                    fields=('id', 'role', 'status'))
        if (user_try):
            if (user_try['status'] != 'Enable') and (user_try['role'] !=
                                                     'admin'):
                self.render('login.html',
                            password_error=u'账号已被禁用,请联系管理员',
                            email=email,
                            regFlg=regFlg)
                return
        else:
            self.render('login.html',
                        password_error=u'不存在此邮箱或密码错误',
                        email=email,
                        regFlg=regFlg)
            return

        if self.db.user.challenge(email, password):
            user = self.db.user.get(email=email,
                                    fields=('id', 'email', 'nickname', 'role',
                                            'email_verified'))
            if not user:
                self.render('login.html',
                            password_error=u'不存在此邮箱或密码错误',
                            email=email,
                            regFlg=regFlg)
                return

            if (siteconfig['MustVerifyEmailEn'] !=
                    0) and (user['email_verified'] == 0):
                self.render('login.html',
                            password_error=u'未验证邮箱,请点击注册重新验证邮箱',
                            email=email,
                            regFlg=regFlg)
                return

            setcookie = dict(
                expires_days=config.cookie_days,
                httponly=True,
            )
            if config.https:
                setcookie['secure'] = True
            self.set_secure_cookie('user', umsgpack.packb(user), **setcookie)
            self.db.user.mod(user['id'], atime=time.time(), aip=self.ip2int)

            # 如果用户MD5不一致就更新MD5
            hash = MD5.new()
            hash.update(password.encode('utf-8'))
            tmp = hash.hexdigest()
            if (self.db.user.get(email=email,
                                 fields=('password_md5'))['password_md5'] !=
                    tmp):
                self.db.user.mod(user['id'], password_md5=tmp)

            next = self.get_argument('next', '/my/')
            self.redirect(next)
        else:
            self.evil(+5)
            self.render('login.html',
                        password_error=u'不存在此邮箱或密码错误',
                        email=email,
                        regFlg=regFlg)
Ejemplo n.º 56
0
 def test_pack_ext_handler(self):
     for (name, obj, data) in ext_handlers_test_vectors:
         obj_repr = repr(obj)
         print("\tTesting %s: object %s" % (name, obj_repr if len(obj_repr) < 24 else obj_repr[0:24] + "..."))
         self.assertEqual(umsgpack.packb(obj, ext_handlers=ext_handlers), data)
Ejemplo n.º 57
0
import sys
import json
import umsgpack

f = open(sys.argv[1], 'r')
data = f.read()
packed = umsgpack.packb(json.loads(data))
print "%s" % packed
f.close()
Ejemplo n.º 58
0
 def _pack_payload(self) -> RawPayload:
     try:
         return RawPayload(umsgpack.packb(self.payload))
     except umsgpack.PackException as exc:
         raise MessageError('Could not pack msgpack payload') from exc
 def packb(self, data):
     """Pack `data` into a :class:`bytes` instance."""
     return umsgpack.packb(self.normalize_datum(data))
Ejemplo n.º 60
0
async def fetch(loop, id, request_data):
    async with aiohttp.ClientSession(loop=loop) as session:
        request_data['id'] = id
        data = umsgpack.packb(request_data)
        r = await session.post('http://130.193.48.105:8080', data=data)
        q = await r.read()