2016-12-03 9 views
0

Я играю с Microsoft Azure, чтобы отправлять сообщения в подписку в Облаке через тему. Но столкнулся с проблемами с ssk python от Microsoft, в частности ValueError при десериализации сообщения из облака.azure.servicebus Сообщение ValueError в Python

Это мой код

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) 

Это ошибка:

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 

Я вошел в класс и имел вид:

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) 

Любые идеи, что я мог бы сделать разрешить это?

ответ

0

Мне пришлось сменить файл _serialization.py Microsoft, чтобы он работал. Обертывание блок обижая в попытке/за исключением блока:

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 

, кажется, работает до сих пор ..

Итак, Microsoft ... Любой шанс работы ...