# -*- coding: utf-8 -*-import requestsimport datetimeimport hashlibfrom hashlib import sha1import hmacimport base64GMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'def getSimpleSign(source, SecretId, SecretKey) :dateTime = datetime.datetime.utcnow().strftime(GMT_FORMAT)auth = "hmac id=\\"" + SecretId + "\\", algorithm=\\"hmac-sha1\\", headers=\\"date source\\", signature=\\""signStr = "date: " + dateTime + "\\n" + "source: " + sourcesign = hmac.new(SecretKey, signStr, hashlib.sha1).digest()sign = base64.b64encode(sign)sign = auth + sign + "\\""return sign, dateTimeSecretId = 'your SecretId' # 密钥对的 SecretIdSecretKey = 'your SecretKey' # 密钥对的 SecretKeyurl = 'http://service-xxxxxx-1234567890.ap-guangzhou.apigateway.myqcloud.com/release/xxx' # 用户 API 的访问路径#header = {}header = { 'Host':'service-xxxxxx-1234567890.ap-guangzhou.apigateway.myqcloud.com', # 用户 API 所在服务的域名'Accept': 'text/html, */*; q=0.01','X-Requested-With': 'XMLHttpRequest','User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36','Accept-Encoding': 'gzip, deflate, sdch','Accept-Language': 'zh-CN,zh;q=0.8,ja;q=0.6'}Source = 'xxxxxx' # 签名水印值,可填写任意值sign, dateTime = getSimpleSign(Source, SecretId, SecretKey)header['Authorization'] = signheader['Date'] = dateTimeheader['Source'] = Source# 如果是微服务 API,Header 中需要添加'X-NameSpace-Code'、'X-MicroService-Name'两个字段,通用 API 不需要添加。header['X-NameSpace-Code'] = 'testmic'header['X-MicroService-Name'] = 'provider-demo'print headerr = requests.get(url, headers=header)print rprint r.text
# -*- coding: utf-8 -*-import base64import datetimeimport hashlibimport hmacimport requestsGMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'def getSimpleSign(source, SecretId, SecretKey):dateTime = datetime.datetime.utcnow().strftime(GMT_FORMAT)auth = "hmac id=\\"" + SecretId + "\\", algorithm=\\"hmac-sha1\\", headers=\\"date source\\", signature=\\""signStr = "date: " + dateTime + "\\n" + "source: " + sourcesign = hmac.new(SecretKey.encode(), signStr.encode(), hashlib.sha1).digest()sign = base64.b64encode(sign).decode()sign = auth + sign + "\\""return sign, dateTimeSecretId = 'your SecretId' # 密钥对的 SecretIdSecretKey = 'your SecretKey' # 密钥对的 SecretKeyurl = 'http://service-xxxxxx-1234567890.ap-guangzhou.apigateway.myqcloud.com/release/xxx' # 用户 API 的访问路径header = {'Host': 'service-xxxxxx-1234567890.ap-guangzhou.apigateway.myqcloud.com', # 用户 API 所在服务的域名'Accept': 'text/html, */*; q=0.01','X-Requested-With': 'XMLHttpRequest','User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36','Accept-Encoding': 'gzip, deflate, sdch','Accept-Language': 'zh-CN,zh;q=0.8,ja;q=0.6'}Source = 'xxxxxx' # 签名水印值,可填写任意值sign, dateTime = getSimpleSign(Source, SecretId, SecretKey)header['Authorization'] = signheader['Date'] = dateTimeheader['Source'] = Sourcer = requests.get(url, headers=header)print(r.text)
本页内容是否解决了您的问题?