# -*- coding: utf-8 -*-
import uuid
from urllib import unquote,quote
import time, datetime
from hashlib import sha1
import hmac
import base64
import urllib2
import urllib
import cookielib
import json
import httplib
import re
import requests
import pytz
#uuid='3D99B500-FED0-11E8-ADC2-43E0E4AD0724'
import uuid
uuid = uuid.uuid1()
tz = pytz.timezone('utc')
print tz
now = datetime.datetime.now(tz)
sdate = now.strftime("%Y-%m-%dT%H:%M:%SZ")
print sdate
sdate = quote(sdate)
print sdate
ydate = sdate
sdate = sdate.replace('%', '%25')
yunid='LTAIb9zHh7U0Us3e'
yunsecret='VbG8qFvhNEfWzilgNh0IAdeqXyhua2'
def _init(a,b):
DescribeRegions=b
regionid=a
header='GET&%2F&'
myurl="AccessKeyId=%s&Action=%s&Format=JSON&RegionId=%s&SignatureMethod=HMAC-SHA1&SignatureNonce=%s&SignatureVersion=1.0&TimeStamp=%s&Version=2014-05-26" % (yunid,DescribeRegions,regionid,uuid,ydate)
print myurl
myurl=quote(myurl)
myurl=header+myurl
myurl=myurl.encode('utf-8')
print myurl
my_sign = hmac.new('VbG8qFvhNEfWzilgNh0IAdeqXyhua2&', myurl, sha1).digest()
my_sign = base64.b64encode(my_sign)
print my_sign
my_sign=my_sign.encode('utf-8')
return my_sign
mysign=_init('region1','DescribeRegions')
head='http://ecs.aliyuncs.com/?'
url='SignatureVersion=1.0&Format=JSON&TimeStamp=%s&RegionId=%s&AccessKeyId=%s&SignatureMethod=HMAC-SHA1&Version=2014-05-26&Signature=%s&Action=%s&SignatureNonce=%s'%(ydate,'region1',yunid,mysign,'DescribeRegions',uuid)
myurl=head+url
s=requests.session()
print s.get(myurl).content
mysign=_init('cn-hangzhou','DescribeInstances')
url='SignatureVersion=1.0&Format=JSON&TimeStamp=%s&RegionId=%s&AccessKeyId=%s&SignatureMethod=HMAC-SHA1&Version=2014-05-26&Signature=%s&Action=%s&SignatureNonce=%s'%(ydate,'cn-hangzhou',yunid,mysign,'DescribeInstances',uuid)
myurl=head+url
s=requests.session()
print s.get(myurl).content