Skip to content

Instantly share code, notes, and snippets.

@roccogalluzzo
Created January 24, 2010 13:06
Show Gist options
  • Save roccogalluzzo/285194 to your computer and use it in GitHub Desktop.
Save roccogalluzzo/285194 to your computer and use it in GitHub Desktop.
def ascii_sorting(l)
convert = lambda text: int(text) if text.isdigit() else text
ascii_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
return sorted(l, key=ascii_key )
def aws_request(operation="ItemSearch"):
conf = ConfigParser.ConfigParser()
conf.read('aws_api.conf')
base_url = conf.get('aws_api', 'url')
access_key= conf.get('aws_api', 'access_key')
secret_key= conf.get('aws_api', 'secret_key')
url_params = {'Operation':operation,'Service':"AWSECommerceService",
'AWSAccessKeyId':access_key,'Version':"2009-10-01",'ItemPage':"1",'Keywords':"Amazon"}
#add the timestamp
url_params['Timestamp'] = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime())
# encode the url params
values = map(lambda x:urllib.quote(x,safe='~'),url_params.values())
keys =map(lambda x:urllib.quote(x,safe='~'),url_params.keys())
#sort in ASCII order
keys = ascii_sorting(keys)
values = map(url_params.get, keys)
# Reconstruct the URL paramters and encode them
url_string = urllib.urlencode( zip(keys,values) )
#String to sign
string_to_sign = """GET
ecs.amazonaws.com
/onca/xml
%s""" % url_string
# Sign the request
signature = hmac.new(secret_key,string_to_sign,hashlib.sha256).digest()
signature = base64.encodestring(signature)
# Encode url safe the signature
signature = urllib.quote(signature ,safe='~')
print "%s?%s&Signature=%s" % (base_url,url_string, signature)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment