Learning Python Network Programming

(Sean Pound) #1
Chapter 3

First we need to open an interactive Python shell, and then generate the
aforementioned error message again by using the following command:





import requests








import requests_aws4auth








auth = requests_aws4auth.AWS4Auth('', '', 'eu-west-1', '')








r = requests.get('http://s3.eu-west-1.amazonaws.com', auth=auth)





You'll need to replace with your AWS access ID. Print out r.text to make sure
that you get an error message, which is similar to the one that we generated earlier.


Now, we can explore our XML. Convert the XML text into an ElementTree tree.
A handy function for doing this is:





import xml.etree.ElementTree as ET








root = ET.fromstring(r.text)





We now have an ElementTree instance, with root as the root element.


Finding elements


The simplest way of navigating the tree is by using the elements as iterators.
Try doing the following:





for element in root:





... print('Tag: ' + element.tag)


Tag: Code


Tag: Message


Tag: AWSAccessKeyId


Tag: StringToSign


Tag: SignatureProvided


...


Iterating over root returns each of its child elements, and then we print out the tag
of an element by using the tag attribute.


We can apply a filter to the tags that we iterate over by using the following command:





for element in root.findall('Message'):





... print(element.tag + ': ' + element.text)


Message: The request signature we calculated does not match the
signature you provided. Check your key and signing method.

Free download pdf