├── License ├── README.md ├── aliyun-api-gateway-demo-sign2 ├── ClientDemo.py ├── __init__.py └── com │ ├── __init__.py │ └── aliyun │ ├── __init__.py │ └── api │ ├── __init__.py │ └── gateway │ ├── __init__.py │ └── sdk │ ├── __init__.py │ ├── auth │ ├── __init__.py │ ├── md5_tool.py │ ├── sha_hmac256.py │ ├── signature_composer.py │ └── url_encoder.py │ ├── client.py │ ├── common │ ├── __init__.py │ └── constant.py │ ├── http │ ├── __init__.py │ ├── request.py │ └── response.py │ └── util │ ├── DateUtil.py │ ├── ParamUtil.py │ ├── UUIDUtil.py │ └── __init__.py └── aliyun-api-gateway-demo-sign3 ├── ClientDemo.py └── com ├── __init__.py └── aliyun ├── __init__.py └── api ├── __init__.py └── gateway ├── __init__.py └── sdk ├── __init__.py ├── auth ├── __init__.py ├── md5_tool.py ├── sha_hmac256.py ├── signature_composer.py └── url_encoder.py ├── client.py ├── common ├── __init__.py └── constant.py ├── http ├── __init__.py ├── request.py └── response.py └── util ├── DateUtil.py ├── ParamUtil.py ├── UUIDUtil.py └── __init__.py /License: -------------------------------------------------------------------------------- 1 | Copyright 1999-2015 Alibaba Group Holding Ltd. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### 1、本示例的 Python 版本为2.7 和 3.11 2 | ### 2、本demo主要是为了提供签名方法,调用示例可以参考 ClientDemo.py 文件 3 | ### 3、使用注意事项: 4 | - 含有中文和空格的query, body在请求时需要对值进行urlencode处理,编码为utf-8. 5 | - 参数参与签名时,必须使用原文签名,不能用urlencode后字符串的进行签名.所以请在签名之后再对query、body的值做urlencode. 6 | - [签名文档](https://help.aliyun.com/document_detail/29475.html) 7 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign2/ClientDemo.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | from com.aliyun.api.gateway.sdk import client 3 | from com.aliyun.api.gateway.sdk.http import request 4 | from com.aliyun.api.gateway.sdk.common import constant 5 | 6 | host = "http://test-cn-qingdao.alicloudapi.com" 7 | url = "/api/billing/test/123243?queryparam=query1" 8 | 9 | cli = client.DefaultClient(app_key="appKey", app_secret="appSecret") 10 | 11 | # GET 12 | # req = request.Request(host=host,protocol=constant.HTTP, url=url, method="GET", time_out=30000) 13 | # print cli.execute(req) 14 | 15 | 16 | #post body stream 17 | 18 | # import json 19 | # req_post = request.Request(host=host, protocol=constant.HTTP, url=url, method="POST", time_out=30000) 20 | # body = {} 21 | # body["name"] = "testName1111111" 22 | # body["address"] = "testAddress" 23 | # body["email"] = "testemail@123.com" 24 | # req_post.set_body(bytearray(source=json.dumps(body), encoding="utf8")) 25 | # req_post.set_content_type(constant.CONTENT_TYPE_STREAM) 26 | # print cli.execute(req_post) 27 | 28 | 29 | #post form 30 | 31 | req_post = request.Request(host=host, protocol=constant.HTTP, url=url, method="POST", time_out=30000) 32 | bodyMap = {} 33 | bodyMap["bodyForm1"] = "fwefwef" 34 | bodyMap["bodyForm2"] = "ffwefwef" 35 | req_post.set_body(bodyMap) 36 | req_post.set_content_type(constant.CONTENT_TYPE_FORM) 37 | print cli.execute(req_post) -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign2/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliyun/api-gateway-demo-sign-python/4a031a68408e1156511c8f7a44a0731f58488850/aliyun-api-gateway-demo-sign2/__init__.py -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign2/com/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliyun/api-gateway-demo-sign-python/4a031a68408e1156511c8f7a44a0731f58488850/aliyun-api-gateway-demo-sign2/com/__init__.py -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign2/com/aliyun/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliyun/api-gateway-demo-sign-python/4a031a68408e1156511c8f7a44a0731f58488850/aliyun-api-gateway-demo-sign2/com/aliyun/__init__.py -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign2/com/aliyun/api/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliyun/api-gateway-demo-sign-python/4a031a68408e1156511c8f7a44a0731f58488850/aliyun-api-gateway-demo-sign2/com/aliyun/api/__init__.py -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign2/com/aliyun/api/gateway/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliyun/api-gateway-demo-sign-python/4a031a68408e1156511c8f7a44a0731f58488850/aliyun-api-gateway-demo-sign2/com/aliyun/api/gateway/__init__.py -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign2/com/aliyun/api/gateway/sdk/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliyun/api-gateway-demo-sign-python/4a031a68408e1156511c8f7a44a0731f58488850/aliyun-api-gateway-demo-sign2/com/aliyun/api/gateway/sdk/__init__.py -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign2/com/aliyun/api/gateway/sdk/auth/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliyun/api-gateway-demo-sign-python/4a031a68408e1156511c8f7a44a0731f58488850/aliyun-api-gateway-demo-sign2/com/aliyun/api/gateway/sdk/auth/__init__.py -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign2/com/aliyun/api/gateway/sdk/auth/md5_tool.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # Licensed to the Apache Software Foundation (ASF) under one 3 | # or more contributor license agreements. See the NOTICE file 4 | # distributed with this work for additional information 5 | # regarding copyright ownership. The ASF licenses this file 6 | # to you under the Apache License, Version 2.0 (the 7 | # "License"); you may not use this file except in compliance 8 | # with the License. You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, 13 | # software distributed under the License is distributed on an 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | # KIND, either express or implied. See the License for the 16 | # specific language governing permissions and limitations 17 | # under the License. 18 | # coding=utf-8 19 | 20 | """ 21 | MD5 tools module. 22 | 23 | Created on 9/28/2015 24 | 25 | @author: alex 26 | """ 27 | 28 | import hashlib 29 | import base64 30 | 31 | 32 | def _get_md5(content): 33 | m = hashlib.md5() 34 | m.update(buffer(content)) 35 | return m.digest() 36 | 37 | 38 | def get_md5_base64_str(content): 39 | return base64.encodestring(_get_md5(content)).strip() 40 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign2/com/aliyun/api/gateway/sdk/auth/sha_hmac256.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # Licensed to the Apache Software Foundation (ASF) under one 3 | # or more contributor license agreements. See the NOTICE file 4 | # distributed with this work for additional information 5 | # regarding copyright ownership. The ASF licenses this file 6 | # to you under the Apache License, Version 2.0 (the 7 | # "License"); you may not use this file except in compliance 8 | # with the License. You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, 13 | # software distributed under the License is distributed on an 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | # KIND, either express or implied. See the License for the 16 | # specific language governing permissions and limitations 17 | # under the License. 18 | # coding=utf-8 19 | 20 | 21 | import hmac 22 | import hashlib 23 | import base64 24 | 25 | 26 | def sign(source, secret): 27 | h = hmac.new(secret, source, hashlib.sha256) 28 | signature = base64.encodestring(h.digest()).strip() 29 | return signature 30 | 31 | 32 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign2/com/aliyun/api/gateway/sdk/auth/signature_composer.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # Licensed to the Apache Software Foundation (ASF) under one 3 | # or more contributor license agreements. See the NOTICE file 4 | # distributed with this work for additional information 5 | # regarding copyright ownership. The ASF licenses this file 6 | # to you under the Apache License, Version 2.0 (the 7 | # "License"); you may not use this file except in compliance 8 | # with the License. You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, 13 | # software distributed under the License is distributed on an 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | # KIND, either express or implied. See the License for the 16 | # specific language governing permissions and limitations 17 | # under the License. 18 | # coding=utf-8 19 | 20 | from com.aliyun.api.gateway.sdk.common import constant 21 | from com.aliyun.api.gateway.sdk.auth import sha_hmac256 22 | from com.aliyun.api.gateway.sdk.util import DateUtil 23 | import time 24 | 25 | 26 | def build_sign_str(uri=None, method=None, headers=None, body=None): 27 | lf = '\n' 28 | string_to_sign = [] 29 | string_to_sign.append(method) 30 | 31 | string_to_sign.append(lf) 32 | if constant.HTTP_HEADER_ACCEPT in headers and headers[constant.HTTP_HEADER_ACCEPT]: 33 | string_to_sign.append(headers[constant.HTTP_HEADER_ACCEPT]) 34 | 35 | string_to_sign.append(lf) 36 | if constant.HTTP_HEADER_CONTENT_MD5 in headers and headers[constant.HTTP_HEADER_CONTENT_MD5]: 37 | string_to_sign.append(headers[constant.HTTP_HEADER_CONTENT_MD5]) 38 | 39 | string_to_sign.append(lf) 40 | if constant.HTTP_HEADER_CONTENT_TYPE in headers and headers[constant.HTTP_HEADER_CONTENT_TYPE]: 41 | string_to_sign.append(headers[constant.HTTP_HEADER_CONTENT_TYPE]) 42 | 43 | string_to_sign.append(lf) 44 | if constant.HTTP_HEADER_DATE in headers and headers[constant.HTTP_HEADER_DATE]: 45 | string_to_sign.append(headers[constant.HTTP_HEADER_DATE]) 46 | 47 | string_to_sign.append(lf) 48 | string_to_sign.append(_format_header(headers=headers)) 49 | string_to_sign.append(_build_resource(uri=uri, body=body)) 50 | 51 | return ''.join(string_to_sign) 52 | 53 | def _build_resource(uri="", body={}): 54 | if uri.__contains__("?"): 55 | uri_array = uri.split("?") 56 | uri = uri_array[0] 57 | query_str = uri_array[1] 58 | if not body: 59 | body = {} 60 | if query_str: 61 | query_str_array = query_str.split("&") 62 | for query in query_str_array: 63 | query_array = query.split("=") 64 | if query_array[0] not in body: 65 | body[query_array[0]] = query_array[1] 66 | 67 | resource = [] 68 | resource.append(uri) 69 | if body: 70 | resource.append("?") 71 | param_list = body.keys() 72 | param_list.sort() 73 | first = True 74 | for key in param_list: 75 | if not first: 76 | resource.append("&") 77 | first = False 78 | 79 | if body[key]: 80 | resource.append(key) 81 | resource.append("=") 82 | resource.append(body[key]) 83 | else: 84 | resource.append(key) 85 | 86 | if resource is None: 87 | return '' 88 | 89 | return "".join(str(x) for x in resource) 90 | 91 | 92 | def convert_utf8(input_string): 93 | if isinstance(input_string, unicode): 94 | input_string = input_string.encode('utf-8') 95 | return input_string 96 | 97 | 98 | def _format_header(headers={}): 99 | lf = '\n' 100 | temp_headers = [] 101 | if len(headers) > 0: 102 | header_list = headers.keys() 103 | header_list.sort() 104 | signature_headers = [] 105 | for k in header_list: 106 | if k.startswith("X-Ca-"): 107 | temp_headers.append(k) 108 | temp_headers.append(":") 109 | temp_headers.append(str(headers[k])) 110 | temp_headers.append(lf) 111 | signature_headers.append(k) 112 | headers[constant.X_CA_SIGNATURE_HEADERS] = ','.join(signature_headers) 113 | return ''.join(temp_headers) 114 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign2/com/aliyun/api/gateway/sdk/auth/url_encoder.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # Licensed to the Apache Software Foundation (ASF) under one 3 | # or more contributor license agreements. See the NOTICE file 4 | # distributed with this work for additional information 5 | # regarding copyright ownership. The ASF licenses this file 6 | # to you under the Apache License, Version 2.0 (the 7 | # "License"); you may not use this file except in compliance 8 | # with the License. You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, 13 | # software distributed under the License is distributed on an 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | # KIND, either express or implied. See the License for the 16 | # specific language governing permissions and limitations 17 | # under the License. 18 | # coding=utf-8 19 | 20 | import urllib 21 | import sys 22 | 23 | 24 | def get_encode_str(params): 25 | list_params = sorted(params.iteritems(), key=lambda d: d[0]) 26 | encode_str = urllib.urlencode(list_params) 27 | 28 | if sys.stdin.encoding is None: 29 | res = urllib.quote(encode_str.decode('cp936').encode('utf8'), '') 30 | else: 31 | res = urllib.quote(encode_str.decode(sys.stdin.encoding).encode('utf8'), '') 32 | res = res.replace("+", "%20") 33 | res = res.replace("*", "%2A") 34 | res = res.replace("%7E", "~") 35 | return res 36 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign2/com/aliyun/api/gateway/sdk/client.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # Licensed to the Apache Software Foundation (ASF) under one 3 | # or more contributor license agreements. See the NOTICE file 4 | # distributed with this work for additional information 5 | # regarding copyright ownership. The ASF licenses this file 6 | # to you under the Apache License, Version 2.0 (the 7 | # "License"); you may not use this file except in compliance 8 | # with the License. You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, 13 | # software distributed under the License is distributed on an 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | # KIND, either express or implied. See the License for the 16 | # specific language governing permissions and limitations 17 | # under the License. 18 | # coding=utf-8 19 | 20 | import json 21 | from com.aliyun.api.gateway.sdk.util import UUIDUtil, DateUtil 22 | from com.aliyun.api.gateway.sdk.http.request import Request 23 | from com.aliyun.api.gateway.sdk.http.response import Response 24 | from com.aliyun.api.gateway.sdk.common import constant 25 | from com.aliyun.api.gateway.sdk.auth import md5_tool, signature_composer, sha_hmac256 26 | 27 | 28 | class DefaultClient: 29 | def __init__(self, app_key=None, app_secret=None, time_out=None): 30 | self.__app_key = app_key 31 | self.__app_secret = app_secret 32 | self.__time_out = time_out 33 | pass 34 | 35 | def execute(self, request=None): 36 | try: 37 | headers = self.build_headers(request) 38 | 39 | response = Response(host=request.get_host(), url=request.get_url(), method=request.get_method(), 40 | headers=headers, protocol=request.get_protocol(), content_type=request.get_content_type(), 41 | content=request.get_body(), time_out=request.get_time_out()) 42 | if response.get_ssl_enable(): 43 | return response.get_https_response() 44 | else: 45 | return response.get_http_response() 46 | except IOError: 47 | raise 48 | except AttributeError: 49 | raise 50 | 51 | def build_headers(self, request=None): 52 | headers = dict() 53 | header_params = request.get_headers() 54 | headers[constant.X_CA_TIMESTAMP] = DateUtil.get_timestamp() 55 | headers[constant.X_CA_KEY] = self.__app_key 56 | 57 | body = request.get_body(); 58 | 59 | headers[constant.X_CA_NONCE] = UUIDUtil.get_uuid() 60 | 61 | if request.get_content_type(): 62 | headers[constant.HTTP_HEADER_CONTENT_TYPE] = request.get_content_type() 63 | else: 64 | headers[constant.HTTP_HEADER_CONTENT_TYPE] = constant.CONTENT_TYPE_JSON 65 | 66 | if constant.HTTP_HEADER_ACCEPT in header_params \ 67 | and header_params[constant.HTTP_HEADER_ACCEPT]: 68 | headers[constant.HTTP_HEADER_ACCEPT] = header_params[constant.HTTP_HEADER_ACCEPT] 69 | else: 70 | headers[constant.HTTP_HEADER_ACCEPT] = constant.CONTENT_TYPE_JSON 71 | 72 | if constant.POST == request.get_method() and constant.CONTENT_TYPE_STREAM == request.get_content_type(): 73 | headers[constant.HTTP_HEADER_CONTENT_MD5] = md5_tool.get_md5_base64_str(request.get_body()) 74 | str_to_sign = signature_composer.build_sign_str(uri=request.get_url(), method=request.get_method(), 75 | headers=headers) 76 | else: 77 | str_to_sign = signature_composer.build_sign_str(uri=request.get_url(), method=request.get_method(), 78 | headers=headers, body=body) 79 | 80 | headers[constant.X_CA_SIGNATURE] = sha_hmac256.sign(str_to_sign, self.__app_secret) 81 | 82 | return headers 83 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign2/com/aliyun/api/gateway/sdk/common/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliyun/api-gateway-demo-sign-python/4a031a68408e1156511c8f7a44a0731f58488850/aliyun-api-gateway-demo-sign2/com/aliyun/api/gateway/sdk/common/__init__.py -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign2/com/aliyun/api/gateway/sdk/common/constant.py: -------------------------------------------------------------------------------- 1 | SYSTEM_HEADERS = ( 2 | X_CA_SIGNATURE, X_CA_SIGNATURE_HEADERS, X_CA_TIMESTAMP, X_CA_NONCE, X_CA_KEY 3 | ) = ( 4 | 'X-Ca-Signature', 'X-Ca-Signature-Headers', 'X-Ca-Timestamp', 'X-Ca-Nonce', 'X-Ca-Key' 5 | ) 6 | 7 | HTTP_HEADERS = ( 8 | HTTP_HEADER_ACCEPT, HTTP_HEADER_CONTENT_MD5, 9 | HTTP_HEADER_CONTENT_TYPE, HTTP_HEADER_USER_AGENT, HTTP_HEADER_DATE 10 | ) = ( 11 | 'Accept', 'Content-MD5', 12 | 'Content-Type', 'User-Agent', 'Date' 13 | ) 14 | 15 | HTTP_PROTOCOL = ( 16 | HTTP, HTTPS 17 | ) = ( 18 | 'http', 'https' 19 | ) 20 | 21 | HTTP_METHOD = ( 22 | GET, POST, PUT, DELETE, HEADER 23 | ) = ( 24 | 'GET', 'POST', 'PUT', 'DELETE', 'HEADER' 25 | ) 26 | 27 | CONTENT_TYPE = ( 28 | CONTENT_TYPE_FORM, CONTENT_TYPE_STREAM, 29 | CONTENT_TYPE_JSON, CONTENT_TYPE_XML, CONTENT_TYPE_TEXT 30 | ) = ( 31 | 'application/x-www-form-urlencoded', 'application/octet-stream', 32 | 'application/json', 'application/xml', 'application/text' 33 | ) 34 | 35 | BODY_TYPE = ( 36 | FORM, STREAM 37 | ) = ( 38 | 'FORM', 'STREAM' 39 | ) 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign2/com/aliyun/api/gateway/sdk/http/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliyun/api-gateway-demo-sign-python/4a031a68408e1156511c8f7a44a0731f58488850/aliyun-api-gateway-demo-sign2/com/aliyun/api/gateway/sdk/http/__init__.py -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign2/com/aliyun/api/gateway/sdk/http/request.py: -------------------------------------------------------------------------------- 1 | from com.aliyun.api.gateway.sdk.common import constant 2 | 3 | class Request: 4 | content_md5 = "Content-MD5" 5 | content_length = "Content-Length" 6 | content_type = "Content-Type" 7 | 8 | def __init__(self, host=None, protocol=constant.HTTP, headers={}, url=None, method=None, time_out=None): 9 | self.__host = host 10 | self.__url = url 11 | self.__method = method 12 | self.__time_out = time_out 13 | self.__headers = headers 14 | self.__body = None 15 | self.__content_type = None 16 | self.__query_str = None 17 | self.__protocol = protocol 18 | 19 | def get_protocol(self): 20 | return self.__protocol 21 | 22 | def set_protocol(self, protocol): 23 | self.__protocol = protocol 24 | 25 | def get_method(self): 26 | return self.__method 27 | 28 | def set_method(self, method): 29 | self.__method = method 30 | 31 | def get_host(self): 32 | return self.__host 33 | 34 | def set_host(self, host): 35 | self.__host = host 36 | 37 | def get_url(self): 38 | return self.__url 39 | 40 | def set_url(self, url): 41 | self.__url = url 42 | 43 | def get_time_out(self): 44 | return self.__time_out 45 | 46 | def set_time_out(self, time_out): 47 | self.__time_out = time_out 48 | 49 | def get_content_type(self): 50 | return self.__content_type 51 | 52 | def set_content_type(self, content_type): 53 | self.__content_type = content_type 54 | 55 | def get_headers(self): 56 | return self.__headers 57 | 58 | def set_headers(self, headers={}): 59 | self.__headers = headers 60 | 61 | def get_query_str(self): 62 | return self.__query_str 63 | 64 | def set_query_str(self, query_str=None): 65 | self.__query_str = query_str 66 | 67 | def set_body(self, body): 68 | self.__body = body 69 | 70 | def get_body(self): 71 | return self.__body 72 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign2/com/aliyun/api/gateway/sdk/http/response.py: -------------------------------------------------------------------------------- 1 | from com.aliyun.api.gateway.sdk.http.request import Request 2 | 3 | import httplib 4 | import urllib 5 | from com.aliyun.api.gateway.sdk.common import constant 6 | 7 | 8 | class Response(Request): 9 | def __init__(self, host=None, url=None, method=constant.GET, headers={}, protocol=constant.HTTP, content_type=None, content=None, port=None, 10 | key_file=None, cert_file=None, time_out=None): 11 | Request.__init__(self, host=host, protocol=protocol, url=url, headers=headers, method=method, time_out=time_out) 12 | self.__ssl_enable = False 13 | if protocol == constant.HTTPS: 14 | self.__ssl_enable = True 15 | self.__key_file = key_file 16 | self.__cert_file = cert_file 17 | self.__port = port 18 | self.__connection = None 19 | self.set_body(content) 20 | self.set_content_type(content_type) 21 | 22 | def set_ssl_enable(self, enable): 23 | self.__ssl_enable = enable 24 | 25 | def get_ssl_enable(self): 26 | return self.__ssl_enable 27 | 28 | def get_response(self): 29 | if self.get_ssl_enabled(): 30 | return self.get_https_response() 31 | else: 32 | return self.get_http_response() 33 | 34 | def get_response_object(self): 35 | if self.get_ssl_enabled(): 36 | return self.get_https_response_object() 37 | else: 38 | return self.get_http_response_object() 39 | 40 | def parse_host(self): 41 | proto, rest = urllib.splittype(self.get_host()) 42 | host, rest = urllib.splithost(rest) 43 | host, port = urllib.splitport(host) 44 | return host 45 | 46 | def get_http_response(self): 47 | if self.__port is None or self.__port == "": 48 | self.__port = 80 49 | try: 50 | self.__connection = httplib.HTTPConnection(self.parse_host(), self.__port) 51 | self.__connection.connect() 52 | post_data = None 53 | if self.get_content_type() == constant.CONTENT_TYPE_FORM and self.get_body(): 54 | post_data = urllib.urlencode(self.get_body()) 55 | else: 56 | post_data = self.get_body() 57 | self.__connection.request(method=self.get_method(), url=self.get_url(), body=post_data, 58 | headers=self.get_headers()) 59 | response = self.__connection.getresponse() 60 | return response.status, response.getheaders(), response.read() 61 | except Exception as e: 62 | return None, None, None 63 | finally: 64 | self.__close_connection() 65 | 66 | def get_http_response_object(self): 67 | if self.__port is None or self.__port == "": 68 | self.__port = 80 69 | try: 70 | self.__connection = httplib.HTTPConnection(self.parse_host(self.get_host()), self.__port) 71 | self.__connection.connect() 72 | self.__connection.request(method=self.get_method(), url=self.get_url(), body=self.get_body(), 73 | headers=self.get_headers()) 74 | response = self.__connection.getresponse() 75 | return response.status, response.getheaders(), response.read() 76 | except Exception as e: 77 | return None, None, None 78 | finally: 79 | self.__close_connection() 80 | 81 | def get_https_response(self): 82 | try: 83 | self.__port = 443 84 | self.__connection = httplib.HTTPSConnection(self.parse_host(), self.__port, 85 | cert_file=self.__cert_file, 86 | key_file=self.__key_file) 87 | self.__connection.connect() 88 | post_data = None 89 | if self.get_content_type() == constant.CONTENT_TYPE_FORM and self.get_body(): 90 | post_data = urllib.urlencode(self.get_body()) 91 | else: 92 | post_data = self.get_body() 93 | self.__connection.request(method=self.get_method(), url=self.get_url(), body=post_data, 94 | headers=self.get_headers()) 95 | response = self.__connection.getresponse() 96 | return response.status, response.getheaders(), response.read() 97 | except Exception as e: 98 | return None, None, None 99 | finally: 100 | self.__close_connection() 101 | 102 | def get_https_response_object(self): 103 | if self.__port is None or self.__port == "": 104 | self.__port = 443 105 | try: 106 | self.__port = 443 107 | self.__connection = httplib.HTTPSConnection(self.get_host(), self.__port, cert_file=self.__cert_file, 108 | key_file=self.__key_file) 109 | self.__connection.connect() 110 | self.__connection.request(method=self.get_method(), url=self.get_url(), body=self.get_body(), 111 | headers=self.get_headers()) 112 | response = self.__connection.getresponse() 113 | return response.status, response.getheaders(), response.read() 114 | except Exception as e: 115 | return None, None, None 116 | finally: 117 | self.__close_connection() 118 | 119 | def __close_connection(self): 120 | try: 121 | if self.__connection is not None: 122 | self.__connection.close() 123 | except Exception as e: 124 | pass 125 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign2/com/aliyun/api/gateway/sdk/util/DateUtil.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | TIME_ZONE = "GMT" 4 | FORMAT_ISO_8601 = "%Y-%m-%dT%H:%M:%SZ" 5 | FORMAT_RFC_2616 = "%a, %d %b %Y %X GMT" 6 | 7 | 8 | def get_iso_8061_date(): 9 | return time.strftime(FORMAT_ISO_8601, time.gmtime()) 10 | 11 | 12 | def get_rfc_2616_date(): 13 | return time.strftime(FORMAT_RFC_2616, time.gmtime()) 14 | 15 | 16 | def get_timestamp(): 17 | return str(int(time.time() * 1000)) 18 | 19 | 20 | if __name__ == "__main__": 21 | print get_iso_8061_date() 22 | print get_rfc_2616_date() 23 | print get_timestamp() 24 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign2/com/aliyun/api/gateway/sdk/util/ParamUtil.py: -------------------------------------------------------------------------------- 1 | import urllib 2 | import sys 3 | 4 | 5 | def percent_encode(encode_str): 6 | encode_str = str(encode_str) 7 | if sys.stdin.encoding is None: 8 | res = urllib.quote(encode_str.decode('cp936').encode('utf8'), '') 9 | else: 10 | res = urllib.quote(encode_str.decode(sys.stdin.encoding).encode('utf8'), '') 11 | res = res.replace('+', '%20') 12 | res = res.replace('*', '%2A') 13 | res = res.replace('%7E', '~') 14 | return res 15 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign2/com/aliyun/api/gateway/sdk/util/UUIDUtil.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | import uuid 4 | 5 | 6 | def get_uuid(): 7 | return str(uuid.uuid4()) 8 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign2/com/aliyun/api/gateway/sdk/util/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliyun/api-gateway-demo-sign-python/4a031a68408e1156511c8f7a44a0731f58488850/aliyun-api-gateway-demo-sign2/com/aliyun/api/gateway/sdk/util/__init__.py -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign3/ClientDemo.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | from com.aliyun.api.gateway.sdk import client 3 | from com.aliyun.api.gateway.sdk.http import request 4 | from com.aliyun.api.gateway.sdk.common import constant 5 | 6 | host = "http://test-cn-qingdao.alicloudapi.com" 7 | url = "/api/path" 8 | 9 | cli = client.DefaultClient(app_key="your appKey", app_secret="your appSecret") 10 | 11 | 12 | # demo of [HTTP GET] type request 13 | # req = request.Request(host=host, protocol=constant.HTTP, url=url, method="GET", time_out=30000) 14 | # print(cli.execute(req)) 15 | 16 | 17 | # demo of [HTTP POST body stream] type request 18 | # import json 19 | # req_post = request.Request(host=host, protocol=constant.HTTP, url=url, method="POST", time_out=30000) 20 | # body = {"name": "testName", "address": "testAddress", "email": "testemail@123.com"} 21 | # req_post.set_body(bytearray(source=json.dumps(body), encoding="utf8")) 22 | # req_post.set_content_type(constant.CONTENT_TYPE_STREAM) 23 | # print(cli.execute(req_post)) 24 | 25 | 26 | # demo of [HTTP POST form] type request 27 | req_post = request.Request(host=host, protocol=constant.HTTP, url=url, method="POST", time_out=30000) 28 | bodyMap = {"param1": "value1", "param2": "value2"} 29 | req_post.set_body(bodyMap) 30 | req_post.set_content_type(constant.CONTENT_TYPE_FORM) 31 | print(cli.execute(req_post)) 32 | 33 | 34 | # demo of [HTTPS POST form] type request 35 | # req_post = request.Request(host=host, protocol=constant.HTTPS, url=url, method="POST", time_out=30000) 36 | # bodyMap = {"param1": "value1", "param2": "value2"} 37 | # req_post.set_body(bodyMap) 38 | # req_post.set_content_type(constant.CONTENT_TYPE_FORM) 39 | # print(cli.execute(req_post)) 40 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign3/com/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliyun/api-gateway-demo-sign-python/4a031a68408e1156511c8f7a44a0731f58488850/aliyun-api-gateway-demo-sign3/com/__init__.py -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign3/com/aliyun/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliyun/api-gateway-demo-sign-python/4a031a68408e1156511c8f7a44a0731f58488850/aliyun-api-gateway-demo-sign3/com/aliyun/__init__.py -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign3/com/aliyun/api/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliyun/api-gateway-demo-sign-python/4a031a68408e1156511c8f7a44a0731f58488850/aliyun-api-gateway-demo-sign3/com/aliyun/api/__init__.py -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign3/com/aliyun/api/gateway/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliyun/api-gateway-demo-sign-python/4a031a68408e1156511c8f7a44a0731f58488850/aliyun-api-gateway-demo-sign3/com/aliyun/api/gateway/__init__.py -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign3/com/aliyun/api/gateway/sdk/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliyun/api-gateway-demo-sign-python/4a031a68408e1156511c8f7a44a0731f58488850/aliyun-api-gateway-demo-sign3/com/aliyun/api/gateway/sdk/__init__.py -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign3/com/aliyun/api/gateway/sdk/auth/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliyun/api-gateway-demo-sign-python/4a031a68408e1156511c8f7a44a0731f58488850/aliyun-api-gateway-demo-sign3/com/aliyun/api/gateway/sdk/auth/__init__.py -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign3/com/aliyun/api/gateway/sdk/auth/md5_tool.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # Licensed to the Apache Software Foundation (ASF) under one 3 | # or more contributor license agreements. See the NOTICE file 4 | # distributed with this work for additional information 5 | # regarding copyright ownership. The ASF licenses this file 6 | # to you under the Apache License, Version 2.0 (the 7 | # "License"); you may not use this file except in compliance 8 | # with the License. You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, 13 | # software distributed under the License is distributed on an 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | # KIND, either express or implied. See the License for the 16 | # specific language governing permissions and limitations 17 | # under the License. 18 | # coding=utf-8 19 | 20 | """ 21 | MD5 tools module. 22 | 23 | Created on 9/28/2015 24 | 25 | @author: alex 26 | """ 27 | 28 | import hashlib 29 | import base64 30 | 31 | 32 | def _get_md5(content): 33 | m = hashlib.md5() 34 | m.update(memoryview(content)) 35 | return m.digest() 36 | 37 | 38 | def get_md5_base64_str(content): 39 | return base64.encodebytes(_get_md5(content)).strip().decode(encoding="utf-8") 40 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign3/com/aliyun/api/gateway/sdk/auth/sha_hmac256.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # Licensed to the Apache Software Foundation (ASF) under one 3 | # or more contributor license agreements. See the NOTICE file 4 | # distributed with this work for additional information 5 | # regarding copyright ownership. The ASF licenses this file 6 | # to you under the Apache License, Version 2.0 (the 7 | # "License"); you may not use this file except in compliance 8 | # with the License. You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, 13 | # software distributed under the License is distributed on an 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | # KIND, either express or implied. See the License for the 16 | # specific language governing permissions and limitations 17 | # under the License. 18 | # coding=utf-8 19 | 20 | 21 | import hmac 22 | import hashlib 23 | import base64 24 | 25 | 26 | def sign(source, secret): 27 | h = hmac.new(secret.encode(encoding='utf-8'), source.encode(encoding='utf-8'), hashlib.sha256) 28 | signature = base64.encodebytes(h.digest()).strip() 29 | return signature.decode(encoding='utf-8') 30 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign3/com/aliyun/api/gateway/sdk/auth/signature_composer.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # Licensed to the Apache Software Foundation (ASF) under one 3 | # or more contributor license agreements. See the NOTICE file 4 | # distributed with this work for additional information 5 | # regarding copyright ownership. The ASF licenses this file 6 | # to you under the Apache License, Version 2.0 (the 7 | # "License"); you may not use this file except in compliance 8 | # with the License. You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, 13 | # software distributed under the License is distributed on an 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | # KIND, either express or implied. See the License for the 16 | # specific language governing permissions and limitations 17 | # under the License. 18 | # coding=utf-8 19 | 20 | from com.aliyun.api.gateway.sdk.common import constant 21 | 22 | 23 | def build_sign_str(uri=None, method=None, headers=None, body=None): 24 | lf = '\n' 25 | string_to_sign = [method, lf] 26 | 27 | if constant.HTTP_HEADER_ACCEPT in headers and headers[constant.HTTP_HEADER_ACCEPT]: 28 | string_to_sign.append(headers[constant.HTTP_HEADER_ACCEPT]) 29 | 30 | string_to_sign.append(lf) 31 | if constant.HTTP_HEADER_CONTENT_MD5 in headers and headers[constant.HTTP_HEADER_CONTENT_MD5]: 32 | string_to_sign.append(headers[constant.HTTP_HEADER_CONTENT_MD5]) 33 | 34 | string_to_sign.append(lf) 35 | if constant.HTTP_HEADER_CONTENT_TYPE in headers and headers[constant.HTTP_HEADER_CONTENT_TYPE]: 36 | string_to_sign.append(headers[constant.HTTP_HEADER_CONTENT_TYPE]) 37 | 38 | string_to_sign.append(lf) 39 | if constant.HTTP_HEADER_DATE in headers and headers[constant.HTTP_HEADER_DATE]: 40 | string_to_sign.append(headers[constant.HTTP_HEADER_DATE]) 41 | 42 | string_to_sign.append(lf) 43 | string_to_sign.append(_format_header(headers=headers)) 44 | string_to_sign.append(_build_resource(uri=uri, body=body)) 45 | 46 | return ''.join(string_to_sign) 47 | 48 | 49 | def _build_resource(uri="", body={}): 50 | if uri.__contains__("?"): 51 | uri_array = uri.split("?") 52 | uri = uri_array[0] 53 | query_str = uri_array[1] 54 | if not body: 55 | body = {} 56 | if query_str: 57 | query_str_array = query_str.split("&") 58 | for query in query_str_array: 59 | query_array = query.split("=") 60 | if query_array[0] not in body: 61 | body[query_array[0]] = query_array[1] 62 | 63 | resource = [uri] 64 | if body: 65 | resource.append("?") 66 | param_list = list(body.keys()) 67 | param_list.sort() 68 | first = True 69 | for key in param_list: 70 | if not first: 71 | resource.append("&") 72 | first = False 73 | 74 | if body[key]: 75 | resource.append(key) 76 | resource.append("=") 77 | resource.append(body[key]) 78 | else: 79 | resource.append(key) 80 | 81 | if resource is None: 82 | return '' 83 | 84 | return "".join(str(x) for x in resource) 85 | 86 | 87 | def convert_utf8(input_string): 88 | if isinstance(input_string, str): 89 | input_string = input_string.encode('utf-8') 90 | return input_string 91 | 92 | 93 | def _format_header(headers={}): 94 | lf = '\n' 95 | temp_headers = [] 96 | if len(headers) > 0: 97 | header_list = list(headers.keys()) 98 | header_list.sort() 99 | signature_headers = [] 100 | for k in header_list: 101 | if k.startswith("X-Ca-"): 102 | temp_headers.append(k) 103 | temp_headers.append(":") 104 | temp_headers.append(str(headers[k])) 105 | temp_headers.append(lf) 106 | signature_headers.append(k) 107 | headers[constant.X_CA_SIGNATURE_HEADERS] = ','.join(signature_headers) 108 | return ''.join(temp_headers) 109 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign3/com/aliyun/api/gateway/sdk/auth/url_encoder.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # Licensed to the Apache Software Foundation (ASF) under one 3 | # or more contributor license agreements. See the NOTICE file 4 | # distributed with this work for additional information 5 | # regarding copyright ownership. The ASF licenses this file 6 | # to you under the Apache License, Version 2.0 (the 7 | # "License"); you may not use this file except in compliance 8 | # with the License. You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, 13 | # software distributed under the License is distributed on an 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | # KIND, either express or implied. See the License for the 16 | # specific language governing permissions and limitations 17 | # under the License. 18 | # coding=utf-8 19 | 20 | from urllib.parse import urlencode, quote 21 | import sys 22 | 23 | 24 | def get_encode_str(params): 25 | list_params = sorted(params.iteritems(), key=lambda d: d[0]) 26 | encode_str = urlencode(list_params) 27 | 28 | if sys.stdin.encoding is None: 29 | res = quote(encode_str.decode('cp936').encode('utf8'), '') 30 | else: 31 | res = quote(encode_str.decode(sys.stdin.encoding).encode('utf8'), '') 32 | res = res.replace("+", "%20") 33 | res = res.replace("*", "%2A") 34 | res = res.replace("%7E", "~") 35 | return res 36 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign3/com/aliyun/api/gateway/sdk/client.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # Licensed to the Apache Software Foundation (ASF) under one 3 | # or more contributor license agreements. See the NOTICE file 4 | # distributed with this work for additional information 5 | # regarding copyright ownership. The ASF licenses this file 6 | # to you under the Apache License, Version 2.0 (the 7 | # "License"); you may not use this file except in compliance 8 | # with the License. You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, 13 | # software distributed under the License is distributed on an 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | # KIND, either express or implied. See the License for the 16 | # specific language governing permissions and limitations 17 | # under the License. 18 | # coding=utf-8 19 | 20 | 21 | from com.aliyun.api.gateway.sdk.util import UUIDUtil, DateUtil 22 | from com.aliyun.api.gateway.sdk.http.response import Response 23 | from com.aliyun.api.gateway.sdk.common import constant 24 | from com.aliyun.api.gateway.sdk.auth import md5_tool, signature_composer, sha_hmac256 25 | 26 | 27 | class DefaultClient: 28 | def __init__(self, app_key=None, app_secret=None, time_out=None): 29 | self.__app_key = app_key 30 | self.__app_secret = app_secret 31 | self.__time_out = time_out 32 | pass 33 | 34 | def execute(self, request=None): 35 | try: 36 | headers = self.build_headers(request) 37 | 38 | response = Response(host=request.get_host(), url=request.get_url(), method=request.get_method(), 39 | headers=headers, protocol=request.get_protocol(), content_type=request.get_content_type(), 40 | content=request.get_body(), key_file=request.get_key_file(), 41 | cert_file=request.get_cert_file(), time_out=request.get_time_out()) 42 | if response.get_ssl_enable(): 43 | return response.get_https_response() 44 | else: 45 | return response.get_http_response() 46 | except IOError: 47 | raise 48 | except AttributeError: 49 | raise 50 | 51 | def build_headers(self, request=None): 52 | headers = dict() 53 | header_params = request.get_headers() 54 | headers[constant.X_CA_TIMESTAMP] = DateUtil.get_timestamp() 55 | headers[constant.X_CA_KEY] = self.__app_key 56 | 57 | body = request.get_body() 58 | 59 | headers[constant.X_CA_NONCE] = UUIDUtil.get_uuid() 60 | 61 | if request.get_content_type(): 62 | headers[constant.HTTP_HEADER_CONTENT_TYPE] = request.get_content_type() 63 | else: 64 | headers[constant.HTTP_HEADER_CONTENT_TYPE] = constant.CONTENT_TYPE_JSON 65 | 66 | if constant.HTTP_HEADER_ACCEPT in header_params \ 67 | and header_params[constant.HTTP_HEADER_ACCEPT]: 68 | headers[constant.HTTP_HEADER_ACCEPT] = header_params[constant.HTTP_HEADER_ACCEPT] 69 | else: 70 | headers[constant.HTTP_HEADER_ACCEPT] = constant.CONTENT_TYPE_JSON 71 | 72 | if constant.POST == request.get_method() and constant.CONTENT_TYPE_STREAM == request.get_content_type(): 73 | headers[constant.HTTP_HEADER_CONTENT_MD5] = md5_tool.get_md5_base64_str(request.get_body()) 74 | str_to_sign = signature_composer.build_sign_str(uri=request.get_url(), method=request.get_method(), 75 | headers=headers) 76 | else: 77 | str_to_sign = signature_composer.build_sign_str(uri=request.get_url(), method=request.get_method(), 78 | headers=headers, body=body) 79 | 80 | headers[constant.X_CA_SIGNATURE] = sha_hmac256.sign(str_to_sign, self.__app_secret) 81 | 82 | return headers 83 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign3/com/aliyun/api/gateway/sdk/common/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliyun/api-gateway-demo-sign-python/4a031a68408e1156511c8f7a44a0731f58488850/aliyun-api-gateway-demo-sign3/com/aliyun/api/gateway/sdk/common/__init__.py -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign3/com/aliyun/api/gateway/sdk/common/constant.py: -------------------------------------------------------------------------------- 1 | SYSTEM_HEADERS = ( 2 | X_CA_SIGNATURE, X_CA_SIGNATURE_HEADERS, X_CA_TIMESTAMP, X_CA_NONCE, X_CA_KEY 3 | ) = ( 4 | 'X-Ca-Signature', 'X-Ca-Signature-Headers', 'X-Ca-Timestamp', 'X-Ca-Nonce', 'X-Ca-Key' 5 | ) 6 | 7 | HTTP_HEADERS = ( 8 | HTTP_HEADER_ACCEPT, HTTP_HEADER_CONTENT_MD5, 9 | HTTP_HEADER_CONTENT_TYPE, HTTP_HEADER_USER_AGENT, HTTP_HEADER_DATE 10 | ) = ( 11 | 'Accept', 'Content-MD5', 12 | 'Content-Type', 'User-Agent', 'Date' 13 | ) 14 | 15 | HTTP_PROTOCOL = ( 16 | HTTP, HTTPS 17 | ) = ( 18 | 'http', 'https' 19 | ) 20 | 21 | HTTP_METHOD = ( 22 | GET, POST, PUT, DELETE, HEADER 23 | ) = ( 24 | 'GET', 'POST', 'PUT', 'DELETE', 'HEADER' 25 | ) 26 | 27 | CONTENT_TYPE = ( 28 | CONTENT_TYPE_FORM, CONTENT_TYPE_STREAM, 29 | CONTENT_TYPE_JSON, CONTENT_TYPE_XML, CONTENT_TYPE_TEXT 30 | ) = ( 31 | 'application/x-www-form-urlencoded', 'application/octet-stream', 32 | 'application/json', 'application/xml', 'application/text' 33 | ) 34 | 35 | BODY_TYPE = ( 36 | FORM, STREAM 37 | ) = ( 38 | 'FORM', 'STREAM' 39 | ) 40 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign3/com/aliyun/api/gateway/sdk/http/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliyun/api-gateway-demo-sign-python/4a031a68408e1156511c8f7a44a0731f58488850/aliyun-api-gateway-demo-sign3/com/aliyun/api/gateway/sdk/http/__init__.py -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign3/com/aliyun/api/gateway/sdk/http/request.py: -------------------------------------------------------------------------------- 1 | from com.aliyun.api.gateway.sdk.common import constant 2 | 3 | 4 | class Request: 5 | content_md5 = "Content-MD5" 6 | content_length = "Content-Length" 7 | content_type = "Content-Type" 8 | 9 | def __init__(self, host=None, protocol=constant.HTTP, headers=None, url=None, method=None, time_out=None): 10 | if headers is None: 11 | headers = {} 12 | self.__host = host 13 | self.__url = url 14 | self.__method = method 15 | self.__time_out = time_out 16 | self.__headers = headers 17 | self.__body = None 18 | self.__content_type = None 19 | self.__query_str = None 20 | self.__protocol = protocol 21 | self.__key_file = None 22 | self.__cert_file = None 23 | 24 | def get_protocol(self): 25 | return self.__protocol 26 | 27 | def set_protocol(self, protocol): 28 | self.__protocol = protocol 29 | 30 | def get_method(self): 31 | return self.__method 32 | 33 | def set_method(self, method): 34 | self.__method = method 35 | 36 | def get_host(self): 37 | return self.__host 38 | 39 | def set_host(self, host): 40 | self.__host = host 41 | 42 | def get_url(self): 43 | return self.__url 44 | 45 | def set_url(self, url): 46 | self.__url = url 47 | 48 | def get_time_out(self): 49 | return self.__time_out 50 | 51 | def set_time_out(self, time_out): 52 | self.__time_out = time_out 53 | 54 | def get_content_type(self): 55 | return self.__content_type 56 | 57 | def set_content_type(self, content_type): 58 | self.__content_type = content_type 59 | 60 | def get_headers(self): 61 | return self.__headers 62 | 63 | def set_headers(self, headers=None): 64 | if headers is None: 65 | headers = {} 66 | self.__headers = headers 67 | 68 | def get_query_str(self): 69 | return self.__query_str 70 | 71 | def set_query_str(self, query_str=None): 72 | self.__query_str = query_str 73 | 74 | def set_body(self, body): 75 | self.__body = body 76 | 77 | def get_body(self): 78 | return self.__body 79 | 80 | def set_key_file(self, key_file): 81 | self.__key_file = key_file 82 | 83 | def get_key_file(self): 84 | return self.__key_file 85 | 86 | def set_cert_file(self, cert_file): 87 | self.__cert_file = cert_file 88 | 89 | def get_cert_file(self): 90 | return self.__cert_file 91 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign3/com/aliyun/api/gateway/sdk/http/response.py: -------------------------------------------------------------------------------- 1 | from com.aliyun.api.gateway.sdk.http.request import Request 2 | 3 | import http.client 4 | from com.aliyun.api.gateway.sdk.common import constant 5 | from urllib.parse import urlsplit, urlencode 6 | import ssl 7 | 8 | class Response(Request): 9 | def __init__(self, host=None, url=None, method=constant.GET, headers={}, protocol=constant.HTTP, content_type=None, 10 | content=None, port=None, 11 | key_file=None, cert_file=None, time_out=None): 12 | Request.__init__(self, host=host, protocol=protocol, url=url, headers=headers, method=method, time_out=time_out) 13 | self.__ssl_enable = False 14 | if protocol == constant.HTTPS: 15 | self.__ssl_enable = True 16 | self.set_key_file(key_file) 17 | self.set_cert_file(cert_file) 18 | self.__port = port 19 | self.__connection = None 20 | self.set_body(content) 21 | self.set_content_type(content_type) 22 | 23 | def set_ssl_enable(self, enable): 24 | self.__ssl_enable = enable 25 | 26 | def get_ssl_enable(self): 27 | return self.__ssl_enable 28 | 29 | def get_response(self): 30 | if self.get_ssl_enable(): 31 | return self.get_https_response() 32 | else: 33 | return self.get_http_response() 34 | 35 | def get_response_object(self): 36 | if self.get_ssl_enable(): 37 | return self.get_https_response_object() 38 | else: 39 | return self.get_http_response_object() 40 | 41 | def parse_host(self): 42 | res = urlsplit(self.get_host()) 43 | if res.hostname is None: 44 | return "" 45 | return res.hostname 46 | 47 | def get_http_response(self): 48 | if self.__port is None or self.__port == "": 49 | self.__port = 80 50 | try: 51 | self.__connection = http.client.HTTPConnection(self.parse_host(), self.__port, self.get_time_out()) 52 | self.__connection.connect() 53 | post_data = None 54 | if self.get_content_type() == constant.CONTENT_TYPE_FORM and self.get_body(): 55 | post_data = urlencode(self.get_body()) 56 | else: 57 | post_data = self.get_body() 58 | self.__connection.request(method=self.get_method(), url=self.get_url(), body=post_data, 59 | headers=self.get_headers()) 60 | response = self.__connection.getresponse() 61 | return response.status, response.getheaders(), response.read().decode(encoding="utf-8") 62 | except Exception as e: 63 | print(e) 64 | return None, None, None 65 | finally: 66 | self.__close_connection() 67 | 68 | def get_http_response_object(self): 69 | if self.__port is None or self.__port == "": 70 | self.__port = 80 71 | try: 72 | self.__connection = http.client.HTTPConnection(self.parse_host(), self.__port, self.get_time_out()) 73 | self.__connection.connect() 74 | self.__connection.request(method=self.get_method(), url=self.get_url(), body=self.get_body(), 75 | headers=self.get_headers()) 76 | response = self.__connection.getresponse() 77 | return response.status, response.getheaders(), response.read() 78 | except Exception as e: 79 | return None, None, None 80 | finally: 81 | self.__close_connection() 82 | 83 | def get_https_response(self): 84 | try: 85 | self.__port = 443 86 | context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) 87 | self.__connection = http.client.HTTPSConnection(self.parse_host(), self.__port, context=context, 88 | timeout=self.get_time_out()) 89 | self.__connection.connect() 90 | post_data = None 91 | if self.get_content_type() == constant.CONTENT_TYPE_FORM and self.get_body(): 92 | post_data = urlencode(self.get_body()) 93 | else: 94 | post_data = self.get_body() 95 | self.__connection.request(method=self.get_method(), url=self.get_url(), body=post_data, 96 | headers=self.get_headers()) 97 | response = self.__connection.getresponse() 98 | return response.status, response.getheaders(), response.read() 99 | except Exception as e: 100 | return None, None, None 101 | finally: 102 | self.__close_connection() 103 | 104 | def get_https_response_object(self): 105 | if self.__port is None or self.__port == "": 106 | self.__port = 443 107 | try: 108 | self.__port = 443 109 | self.__connection = http.client.HTTPSConnection(self.parse_host(), self.__port, cert_file=self.__cert_file, 110 | key_file=self.__key_file, timeout=self.get_time_out()) 111 | self.__connection.connect() 112 | self.__connection.request(method=self.get_method(), url=self.get_url(), body=self.get_body(), 113 | headers=self.get_headers()) 114 | response = self.__connection.getresponse() 115 | return response.status, response.getheaders(), response.read() 116 | except Exception as e: 117 | return None, None, None 118 | finally: 119 | self.__close_connection() 120 | 121 | def __close_connection(self): 122 | try: 123 | if self.__connection is not None: 124 | self.__connection.close() 125 | except Exception as e: 126 | pass 127 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign3/com/aliyun/api/gateway/sdk/util/DateUtil.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | TIME_ZONE = "GMT" 4 | FORMAT_ISO_8601 = "%Y-%m-%dT%H:%M:%SZ" 5 | FORMAT_RFC_2616 = "%a, %d %b %Y %X GMT" 6 | 7 | 8 | def get_iso_8061_date(): 9 | return time.strftime(FORMAT_ISO_8601, time.gmtime()) 10 | 11 | 12 | def get_rfc_2616_date(): 13 | return time.strftime(FORMAT_RFC_2616, time.gmtime()) 14 | 15 | 16 | def get_timestamp(): 17 | return str(int(time.time() * 1000)) 18 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign3/com/aliyun/api/gateway/sdk/util/ParamUtil.py: -------------------------------------------------------------------------------- 1 | import urllib 2 | import sys 3 | 4 | 5 | def percent_encode(encode_str): 6 | encode_str = str(encode_str) 7 | if sys.stdin.encoding is None: 8 | res = urllib.quote(encode_str.decode('cp936').encode('utf8'), '') 9 | else: 10 | res = urllib.quote(encode_str.decode(sys.stdin.encoding).encode('utf8'), '') 11 | res = res.replace('+', '%20') 12 | res = res.replace('*', '%2A') 13 | res = res.replace('%7E', '~') 14 | return res 15 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign3/com/aliyun/api/gateway/sdk/util/UUIDUtil.py: -------------------------------------------------------------------------------- 1 | import uuid 2 | 3 | 4 | def get_uuid(): 5 | return str(uuid.uuid4()) 6 | -------------------------------------------------------------------------------- /aliyun-api-gateway-demo-sign3/com/aliyun/api/gateway/sdk/util/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliyun/api-gateway-demo-sign-python/4a031a68408e1156511c8f7a44a0731f58488850/aliyun-api-gateway-demo-sign3/com/aliyun/api/gateway/sdk/util/__init__.py --------------------------------------------------------------------------------