Learning Python Network Programming

(Sean Pound) #1

APIs in Action


Here, we have used the findall() method of the root element. This method will
provide us with a list of all the direct children of the root element that match the
specified tag, which in this case is .


And this will solve our problem of just extracting the text of the error message.
Now, let's update our error handling.


Handling errors


We can go back and add this to our s3_client.py file, but let's include a little more
information in the output, and structure the code to allow re-use. Add the following
function to the file underneath the download_file() function:


def handle_error(response):
output = 'Status code: {}\n'.format(response.status_code)
root = ET.fromstring(response.text)
code = root.find('Code').text
output += 'Error code: {}\n'.format(code)
message = root.find('Message').text
output += 'Message: {}\n'.format(message)
print(output)

You'll notice that we have used a new function here, namely, root.find().
This works in the same way as findall() except that it only returns the first
matching element, as opposed to a list of all matching elements.


Then, replace each instance of xml_pprint(r.text) in your file with
handle_error(r) and then run the client again with the incorrect access
secret. Now, you will see a more informative error message:


$ python3.4 s3_client.py create_bucket failbucket.example.com


Status code: 403


Error code: SignatureDoesNotMatch


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


Further enhancements


That's as far as we're going to take our client. We've written a command line
program that can perform essential operations, such as creating buckets and
uploading and downloading objects on the Amazon S3 service. There are still
plenty of operations that can be implemented, and these can be found in the S3
documentation; operations such as listing buckets' contents, deleting objects, and
copying objects.

Free download pdf