-
Notifications
You must be signed in to change notification settings - Fork 4
/
cloudfront.py
53 lines (38 loc) · 1.56 KB
/
cloudfront.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from properties import loadcredentials
import time, httplib, xml.dom.minidom
from contextlib import closing
DEFAULT_HOST = 'cloudfront.amazonaws.com'
class CommonParameters:
def __init__(self, method, headers={}):
self.method = method
self.headers = headers
def setAuthHeader(self, credentials):
value = 'AWS ' + credentials.access_key_id + ':' + credentials.sign(self.textToSign())
self.headers['Authorization'] = value
def textToSign(self):
self.headers['Date'] = time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime())
return self.headers['Date']
def createPath(self):
pass
def send_request(parameters, request_body=None):
print '****************'
print '* REQUEST *'
print '****************'
print '%s https://%s%s' % (parameters.method, DEFAULT_HOST, parameters.createPath())
if request_body:
print request_body
print '****************'
print '* RESPONSE *'
print '****************'
parameters.setAuthHeader(loadcredentials())
with closing(httplib.HTTPSConnection(DEFAULT_HOST)) as conn:
conn.putrequest(parameters.method, parameters.createPath())
for name, value in parameters.headers.iteritems():
conn.putheader(name, value)
conn.endheaders()
if request_body:
conn.send(request_body)
response = conn.getresponse()
print response.status, response.getheaders()
response_dom = xml.dom.minidom.parseString(response.read())
print response_dom.toprettyxml()