# -*- coding: UTF-8 -*- _author_ = 'zy'

# -*- coding: UTF-8 -*-
_author_ = 'zy'
_date_ = '2019/2/11 0011 22:17'
import time
import uuid
import urllib
import sys
import hashlib
import hmac
import base64
import requests
import json
import ali_api.py as aapi
import datetime

FORMAT_ISO_8601 = "%Y-%m-%dT%H:%M:%SZ"

# 获取请求时间戳
def get_Timestamp():
    return time.strftime(FORMAT_ISO_8601, time.gmtime())

# def get_Timestamp():
#     year=datetime.datetime.now().year
#     month=datetime.datetime.now().month
#     day=datetime.datetime.now().day
#     h=datetime.datetime.now().hour
#     minu=datetime.datetime.now().minute
#     sec=datetime.datetime.now().second
#     #2013-01-10T12:00:00Z
#     if month<10:
#         month='0'+str(month)
#     if day<10:
#         day='0'+str(day)
#     if h<10:
#         h='0'+str(h)
#     if minu<10:
#         minu='0'+str(minu)
#     if sec<10:
#         sec='0'+str(sec)
#     ss=str(year)+'-'+str(month)+'-'+str(day)+'T'+str(h)+':'+str(minu)+':'+str(sec)+'Z'
#     #2013-01-10T12:00:00Z
#     return ss

def get_SignatureNonce():
    return str(uuid.uuid4())
def get_url(accessKeyId ,accessKeySecret,billingCycle,url):
    #get参数在url中,post参数在body中。
    parameters = {
        "Format": "JSON",
        "Version": "2014-05-15",#api版本
        "AccessKeyId": accessKeyId,#账号id
        "SignatureMethod": "HMAC-SHA1",#加密方法
        "Timestamp": get_Timestamp(),#时间戳
        "SignatureVersion": "1.0",
        "SignatureNonce": get_SignatureNonce(),#随机数
        "Action": "QueryInstanceGaapCost",
        "BillingCycle": billingCycle,
        'Signature':aapi.test(accessKeyId,accessKeySecret,billingCycle),
    }
    print(get_Timestamp())
    print(parameters['Signature'])
    result=requests.get(url,data=parameters)

    code = result.status_code
    print('当前状态' + str(result.status_code))
    #content = json.loads(result.text)['data']
    content=result.text

    return content

if __name__ == '__main__':
    accessKeyId = 'a'  # 阿里账号密钥id
    accessKeySecret = 'b'  # 登录用户名
    billingCycle = '2018-07'
    url='https://business.aliyuncs.com/?Action=QueryInstanceGaapCost'
    print(get_url(accessKeyId,accessKeySecret,billingCycle,url))

 

你可能感兴趣的:(心得)