locked
Python Issue: Can't get queue message stored with urllib.request RRS feed

  • Question

  • I would like to avoid the bulky Python-Azure SDK and just roll my own very simple, very light POST to the Azure Queue Service with SAS. The following code **almost** works. It does create a message in my Azure storage account with a 201 (created) server reply. I can see the message using Azure Storage Explorer. The POST looks fine in Fiddler. So far, so good.

    Just one problem: The string 'Hello from Python!' isn't making it into the entity. Both Azure Storage Explorer and by pulling the message with .NET code show that the message text is empty. Trying to access the message string in code with the .AsString overload produces an error about an illegal base64 or string length. The Azure queue service isn't choking on the XML ... I'm getting a 201 back. What am I missing here ... It **almost** works.

    Here is the code:

    import urllib.request
    body = '<QueueMessage><MessageText>Hello from Python!</MessageText></QueueMessage>'
    postData = body.encode('utf-8')
    headers = {'x-ms-date': 'Tue, 17 Dec 2013 05:47:21 GMT', 'Content-Length': postData.__len__()}
    request = urllib.request.Request('https://MY_ACCOUNT.queue.core.windows.net/MY_QUEUE/messages?MY_SAS_HERE', postData, headers)
    response = urllib.request.urlopen(request)
    


    • Edited by SellRex Tuesday, December 17, 2013 6:21 AM Spelling error in title
    Tuesday, December 17, 2013 6:21 AM

Answers

  • Can you try by converting the actual message text to Base64 Encoded string. So your message body should be something like:

    body = '<QueueMessage><MessageText>SGVsbG8gZnJvbSBQeXRob24=</MessageText></QueueMessage>'

    Hope this helps.


    • Marked as answer by SellRex Tuesday, December 17, 2013 8:33 AM
    • Edited by Gaurav Mantri Tuesday, December 17, 2013 9:31 AM
    Tuesday, December 17, 2013 7:54 AM

All replies

  • Can you try by converting the actual message text to Base64 Encoded string. So your message body should be something like:

    body = '<QueueMessage><MessageText>SGVsbG8gZnJvbSBQeXRob24=</MessageText></QueueMessage>'

    Hope this helps.


    • Marked as answer by SellRex Tuesday, December 17, 2013 8:33 AM
    • Edited by Gaurav Mantri Tuesday, December 17, 2013 9:31 AM
    Tuesday, December 17, 2013 7:54 AM
  • BINGO!

    Gaurav,

    You got it! It works like a charm now. Thank you so much.

    Luke

    Tuesday, December 17, 2013 8:37 AM
  • For anyone who needs the full code sample to use SAS with Azure Queues from Python, this will work. Keep in mind that any SAS is as dangerous as an account key. Only use SSL, and keep the SAS secure with a short expiry time. Thanks again to Gaurav for helping with the base64 message encoding tip.

    import urllib.request
    import base64
    from time import gmtime, strftime
    message = 'Hello from Python!'
    message = message.encode('utf-8')
    message = base64.b64encode(message)
    body = '<QueueMessage><MessageText>' + message.decode('utf-8') + '</MessageText></QueueMessage>'
    body = body.encode('utf-8')
    headers = {'x-ms-date': strftime("%a, %d %b %Y %H:%M:%S GMT", gmtime()), 'Content-Length': body.__len__()}
    #Next line has your SAS on the Url url = 'https://STORAGE_ACCOUNT_NAME.queue.core.windows.net/QUEUE_NAME/messages?sv=XXX&se=XXX&sp=XXX&sig=XXX' request = urllib.request.Request(url, body, headers) response = urllib.request.urlopen(request) print('Server Response: ' + str(response.getcode()))



    • Edited by SellRex Tuesday, December 17, 2013 9:11 AM spelling error
    Tuesday, December 17, 2013 9:10 AM