简体   繁体   English

Python中的azure.servicebus消息ValueError

[英]azure.servicebus Message ValueError in Python

I'm playing around with Microsoft Azure to send messages to a subscription in the Cloud via a Topic. 我正在与Microsoft Azure一起玩,以通过主题将消息发送到云中的订阅。 But ran into issues with Microsofts python sdk, specifically a ValueError when deserializing a message from the cloud. 但是微软的python sdk遇到了问题,特别是在反序列化来自云的消息时出现ValueError。

This is my code 这是我的代码

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)

This is the error: 这是错误:

Traceback (most recent call last):
  File "C:/Users/user/PycharmProjects/test/helloworld.py", line 59, in <module>
msg = bus_service.receive_subscription_message('topic', 'AllMessages', peek_lock=False)
  File "D:\Program Files\Anaconda2\lib\site-packages\azure\servicebus\servicebusservice.py", line 976, in receive_subscription_message
timeout)
  File "D:\Program Files\Anaconda2\lib\site-packages\azure\servicebus\servicebusservice.py", line 764, in read_delete_subscription_message
return _create_message(response, self)
  File "D:\Program Files\Anaconda2\lib\site-packages\azure\servicebus\_serialization.py", line 101, in _create_message
elif str(int(float(value))) == value:
ValueError: could not convert string to float: max-age=31536000

Process finished with exit code 1

I went into the class and had a look: 我上课看了一下:

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)

Any ideas what I could do to resolve this? 有什么想法可以解决吗?

This is a bug is version 0.20.1, see Change Log in Pypi. 这是0.20.1版的错误,请参阅Pypi中的更改日志。 From 0.20.2 released 2016-06-28, the bug is fixed. 从2016年6月28日发布的0.20.2起,此错误已修复。 https://pypi.python.org/pypi/azure-servicebus https://pypi.python.org/pypi/azure-servicebus

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

Thank you, 谢谢,

I had to change Microsofts _serialization.py file to get it to work. 我必须更改Microsoft的_serialization.py文件才能使其正常工作。 Wrapping the offending block in a try/except block: 将有问题的块包装在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

Seems to work so far.. 到目前为止似乎工作正常。

So, Microsoft... Any chance of a job?... 那么,微软...有工作机会吗?...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Python 中导入 azure.servicebus 错误 - Import azure.servicebus error in Python Python azure sdk - 发生异常:ModuleNotFoundError 没有名为“azure.servicebus”的模块 - Python azure sdk - Exception has occurred: ModuleNotFoundError No module named 'azure.servicebus' ImportError:无法从“azure.servicebus”导入名称“ServiceBusClient” - ImportError: cannot import name 'ServiceBusClient' from 'azure.servicebus' Azure Function 服务总线批处理消息 - python - Azure Function Servicebus batch process messages - python Azure 服务总线 - 获取 python 中的所有可用主题 - Azure Servicebus - get all available topics in python 如何在 Python MagicMock 中模拟 Azure ServiceBus 接收器 - How to mock Azure ServiceBus Receiver in Python MagicMock 基于 ServiceBus 触发 Azure 函数并回写不起作用(Python) - Triggering Azure Function based on ServiceBus and writing back not working (Python) Python azure-servicebus 为 ServiceBusClient 和 ServiceBusMessage 抛出循环导入错误 - Python azure-servicebus throws circular import error for ServiceBusClient and ServiceBusMessage Azure ServiceBus 在 Python 中使用 async/await 似乎不起作用 - Azure ServiceBus using async/await in Python seems not to work Azure 函数 - Python - ServiceBus 输出绑定 - 设置自定义属性 - Azure Function - Python - ServiceBus Output Binding - Setting Custom Properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM