AWSElastic'GET'API请求使用URL--Python3.8||AuthAWSSignature改为中文
创始人
2024-09-25 07:01:39
0

以下是使用Python 3.8和AWS签名身份验证对AWS Elastic的GET API请求进行URL编码的示例代码:

import requests
import datetime
import hashlib
import hmac
import urllib.parse

# AWS Key
ACCESS_KEY = 'YOUR_ACCESS_KEY'
SECRET_KEY = 'YOUR_SECRET_KEY'

# Parameter values for the request
method = 'GET'
service = 'ec2'
host = 'ec2.amazonaws.com'
region = 'us-east-1'
endpoint = 'https://ec2.amazonaws.com'

# Define the request parameters
params = {'Action': 'DescribeRegions',
          'Version': '2013-10-15'}

# Define the date and time variable
t = datetime.datetime.utcnow()
amz_date = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d')

# Create the canonical query string
canonical_querystring = ''
for param in sorted(params.items()):
    canonical_querystring += urllib.parse.quote(param[0], safe='') + '=' + urllib.parse.quote(param[1], safe='') + '&'

# Create the canonical headers and signed headers
canonical_headers = 'host:' + host + '\n' + 'x-amz-date:' + amz_date + '\n'
signed_headers = 'host;x-amz-date'
payload_hash = hashlib.sha256(('').encode('utf-8')).hexdigest()

# Create the canonical request and hash it
canonical_request = method + '\n' + '/' + '\n' + canonical_querystring[:-1] + '\n' + canonical_headers + '\n' + signed_headers + '\n' + payload_hash
hashed_canonical_request = hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()

# Create the string to sign
string_to_sign = 'AWS4-HMAC-SHA256' + '\n' + amz_date + '\n' + datestamp + '/' + region + '/' + service + '/' + 'aws4_request' + '\n' + hashed_canonical_request

# Create the signing key with the crucial date
kDate = hmac.new(('AWS4' + SECRET_KEY).encode('utf-8'), datestamp.encode('utf-8'), hashlib.sha256).digest()
kRegion = hmac.new(kDate, region.encode('utf-8'), hashlib.sha256).digest()
kService = hmac.new(kRegion, service.encode('utf-8'), hashlib.sha256).digest()
kSigning = hmac.new(kService, b'aws4_request', hashlib.sha256).digest()

# Create the signature
signature = hmac.new(kSigning, string_to_sign.encode('utf-8'), hashlib.sha256).hexdigest()

# Create the authorization header
authorization_header = '

相关内容

热门资讯

重磅来袭!aapoker透视方... 自定义wepoker到底有没有透视系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用...
一分钟教会你!wepoker永... 一分钟教会你!wepoker永久免费脚本,hhpoker免费透视脚本,微扑克教程(有挂透视)1、点击...
分享一款!wepoker有没有... 分享一款!wepoker有没有挂,hhpoker是正品吗,科技教程(有挂技巧);小薇(透视辅助)致您...
技术分享!aapoker透视怎... 技术分享!aapoker透视怎么用,wepoker看底牌,2025新版教程(有挂技巧);致您一封信;...
必备科技!拱趴大菠萝有挂吗,德... 您好,拱趴大菠萝有挂吗这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在...
一分钟了解!德州私人局怎么透视... 一分钟了解!德州私人局怎么透视,pokemmo手机脚本,2025新版教程(有挂透视);原来确实真的有...
一分钟揭秘!哈糖大菠萝开挂,h... 一分钟揭秘!哈糖大菠萝开挂,hhpoker作弊码,必胜教程(有挂技巧);1.哈糖大菠萝开挂 ai辅助...
今日公布!哈糖大菠萝可以开挂吗... 今日公布!哈糖大菠萝可以开挂吗,wepoker怎么看底牌,详细教程(有挂方法)是由北京得哈糖大菠萝可...
玩家攻略!哈糖大菠萝有挂吗,佛... 玩家攻略!哈糖大菠萝有挂吗,佛手在线大菠萝智能辅助器,2025新版技巧(有挂软件);1分钟了解详细教...
最新技巧!模拟器打开hhpok... 最新技巧!模拟器打开hhpoker,wpk官网下载链接,我来教教你(有挂方法);模拟器打开hhpok...