Last active
December 15, 2015 22:29
-
-
Save tcurvelo/5333050 to your computer and use it in GitHub Desktop.
AWS 'Hello World!'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import time | |
import hmac | |
import hashlib | |
import base64 | |
import urllib | |
secret_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" | |
def search(keywords): | |
params = { | |
"AWSAccessKeyId": "XXXXXXXXXXXXXXXXXXXX", | |
"AssociateTag": "", | |
"Keywords": urllib.quote(keywords), | |
"Operation": "ItemSearch", | |
"ResponseGroup": urllib.quote("ItemAttributes,Images"), | |
"SearchIndex": "", | |
"Service": "AWSECommerceService", | |
# timestamp must be ISO 8601, GMT, and url-safe | |
"Timestamp": time.strftime("%Y-%m-%dT%H%%3A%M%%3A%SZ", time.gmtime()), | |
"Version": "2009-11-01" | |
} | |
# parameters must be sorted | |
sorted = params.keys() | |
sorted.sort() | |
to_sign = "GET\necs.amazonaws.com\n/onca/xml\n" | |
# make a url string with params ('¶m=value') | |
params_req = "" | |
first = True | |
for j in sorted: | |
if(first): | |
params_req += "%s=%s" % (j, params[j]) | |
first = False | |
else: | |
params_req += "&%s=%s" % (j, params[j]) | |
to_sign += params_req | |
# sign string with SHA256 | |
signature = hmac.new( | |
secret_key, msg=to_sign, digestmod=hashlib.sha256 | |
).digest() | |
signature = base64.b64encode(signature).decode() | |
# url encode signature | |
signature = urllib.quote(signature) | |
# make request URL, including signature | |
url_request = "http://ecs.amazonaws.com/onca/xml?%s&Signature=%s" % ( | |
params_req, signature | |
) | |
return urllib.urlopen(url_request) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment