azure.servicebusPython中的消息值错误

2024-05-13 01:24:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我在玩微软Azure,通过一个主题向云中的订阅发送消息。但是microsoftspythonsdk遇到了问题,特别是在从云端反序列化消息时出现了ValueError。在

这是我的密码

bus_service = ServiceBusService(
    service_namespace='"<namegoeshere>"',
    shared_access_key_name='"<nameofkeygoeshere>"',
    shared_access_key_value='"<keyvaluegoeshere>"')

bus_service.create_topic('topic')
bus_service.create_subscription('topic', 'AllMessages')
msg = Message("HelloWorld")
bus_service.send_topic_message('topic', msg)

// at this point I can see the message arrive in my Azure portal

// then it crashes when I try to retrieve the message I just sent
msg = bus_service.receive_subscription_message('topic', 'AllMessages', peek_lock=False)
print(msg.body)

这是错误:

^{pr2}$

我走进教室看了看:

def _create_message(response, service_instance):
    ''' Create message from response.

    response:
        response from service bus cloud server.
    service_instance:
        the service bus client.
'''
respbody = response.body
custom_properties = {}
broker_properties = None
message_type = None
message_location = None

# gets all information from respheaders.
for name, value in response.headers:
    if name.lower() == 'brokerproperties':
        broker_properties = json.loads(value)
    elif name.lower() == 'content-type':
        message_type = value
    elif name.lower() == 'location':
        message_location = value
    elif name.lower() not in ['content-type',
                              'brokerproperties',
                              'transfer-encoding',
                              'server',
                              'location',
                              'date']:

        if '"' in value:
            value = value[1:-1]
            try:
                custom_properties[name] = datetime.strptime(
                    value, '%a, %d %b %Y %H:%M:%S GMT')
            except ValueError:
                custom_properties[name] = value
        else:  # only int, float or boolean
            if value.lower() == 'true':
                custom_properties[name] = True
            elif value.lower() == 'false':
                custom_properties[name] = False
            # int('3.1') doesn't work so need to get float('3.14') first
            elif str(int(float(value))) == value:    # <---- Exception !
                custom_properties[name] = int(value)
            else:
                custom_properties[name] = float(value)

有什么办法解决这个问题吗?在


Tags: nameinmessagetopicvalueresponsetypeservice
2条回答

这是一个0.20.1版本的错误,请参阅Pypi中的更改日志。从2016年6月28日发布的0.20.2开始,该漏洞已修复。https://pypi.python.org/pypi/azure-servicebus

参考问题:https://github.com/Azure/azure-sdk-for-python/issues/669

谢谢你

我不得不更换微软_序列化.py文件让它工作。在try/except块中包装有问题的块:

if '"' in value:
    value = value[1:-1]
    try:
        custom_properties[name] = datetime.strptime(
            value, '%a, %d %b %Y %H:%M:%S GMT')
    except ValueError:
        custom_properties[name] = value
else:  # only int, float or boolean
    try:
        if value.lower() == 'true':
            custom_properties[name] = True
        elif value.lower() == 'false':
            custom_properties[name] = False
        # int('3.1') doesn't work so need to get float('3.14') first
        elif str(int(float(value))) == value:
            custom_properties[name] = int(value)
        else:
            custom_properties[name] = float(value)
    except ValueError:
        custom_properties[name] = value

到目前为止似乎有效。。在

所以,微软。。。有工作的机会吗?。。。在

相关问题 更多 >