代理模拟 ec2.describe_regions() (AWS)
Proxy to mock ec2.describe_regions() (AWS)
我正在用 Flask 做一个简单的代理来模拟 AWS 的调用 describe_regions()。
Flask 服务器有以下代码:
from __future__ import unicode_literals
from flask import Flask
from flask import Response
from flask import stream_with_context
# from httpretty import HTTPretty, register_uri
import httpretty
import requests
from flask import request
import time
RESPONSE = u"""<DescribeRegionsResponse xmlns="http://ec2.amazonaws.com/doc/2015-10-01/">
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
<regionInfo>
<item>
<regionName>us-east-1</regionName>
<regionEndpoint>ec2.us-east-1.amazonaws.com</regionEndpoint>
</item>
<item>
<regionName>eu-west-1</regionName>
<regionEndpoint>ec2.eu-west-1amazonaws.com</regionEndpoint>
</item>
</regionInfo>
</DescribeRegionsResponse>"""
app = Flask(__name__)
@app.route('/<path:url>', methods=['GET', 'PUT', 'POST', 'DELETE', 'HEAD', 'PATCH', 'OPTIONS', 'CONNECT'])
def home(url):
return Response(RESPONSE, mimetype='text/xml')
if __name__ == '__main__':
app.run(debug=True)
那我有下面的代码来测试一下。我使用 Boto3 为 AWS 调用 API。
from boto3.session import Session
import os
credentials = {
'aws_access_key_id': 'sadasdasda',
'aws_secret_access_key': 'dasdasdasd'
}
os.environ["HTTP_PROXY"] = 'http://localhost:5000/'
os.environ["HTTPS_PROXY"] = 'http://localhost:5000/'
session_boto3 = Session(**credentials)
ec2 = session_boto3.client('ec2', 'eu-west-1', verify=False)
regions = ec2.describe_regions()
print regions
问题是:Flask 服务器收到请愿书,但响应不喜欢 Boto3,我收到以下回溯错误:
Traceback (most recent call last):
File "/pruebas_mock/prueba.py", line 82, in <module>
regions = ec2.describe_regions()
File "/mock_aws/local/lib/python2.7/site-packages/botocore/client.py", line 228, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/client.py", line 475, in _make_api_call
operation_model, request_dict)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/endpoint.py", line 117, in make_request
return self._send_request(request_dict, operation_model)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/endpoint.py", line 146, in _send_request
success_response, exception):
File "/mock_aws/local/lib/python2.7/site-packages/botocore/endpoint.py", line 219, in _needs_retry
caught_exception=caught_exception)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/hooks.py", line 226, in emit
return self._emit(event_name, kwargs)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/hooks.py", line 209, in _emit
response = handler(**kwargs)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/retryhandler.py", line 183, in __call__
if self._checker(attempts, response, caught_exception):
File "/mock_aws/local/lib/python2.7/site-packages/botocore/retryhandler.py", line 250, in __call__
caught_exception)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/retryhandler.py", line 273, in _should_retry
return self._checker(attempt_number, response, caught_exception)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/retryhandler.py", line 313, in __call__
caught_exception)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/retryhandler.py", line 222, in __call__
return self._check_caught_exception(attempt_number, caught_exception)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/retryhandler.py", line 355, in _check_caught_exception
raise caught_exception
botocore.vendored.requests.exceptions.SSLError: [Errno 1] _ssl.c:510: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
Flask应该怎么响应?
谢谢。
默认情况下,boto3(以及所有其他 AWS SDK)将使用 SSL 连接到服务。您的代理 Flask 服务器似乎没有使用 SSL,因此您可以在代理中使用 SSL 或告诉 boto3 不要为您的代理服务器使用 SSL:
ec2 = session_boto3.client('ec2', 'eu-west-1', use_ssl=False, verify=False)
verify
参数告诉 boto3 不要尝试验证 SSL 证书,但它仍会尝试通过 SSL 进行连接。 use_ssl=False
告诉它使用纯 HTTP 与您的端点通信。
我正在用 Flask 做一个简单的代理来模拟 AWS 的调用 describe_regions()。
Flask 服务器有以下代码:
from __future__ import unicode_literals
from flask import Flask
from flask import Response
from flask import stream_with_context
# from httpretty import HTTPretty, register_uri
import httpretty
import requests
from flask import request
import time
RESPONSE = u"""<DescribeRegionsResponse xmlns="http://ec2.amazonaws.com/doc/2015-10-01/">
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
<regionInfo>
<item>
<regionName>us-east-1</regionName>
<regionEndpoint>ec2.us-east-1.amazonaws.com</regionEndpoint>
</item>
<item>
<regionName>eu-west-1</regionName>
<regionEndpoint>ec2.eu-west-1amazonaws.com</regionEndpoint>
</item>
</regionInfo>
</DescribeRegionsResponse>"""
app = Flask(__name__)
@app.route('/<path:url>', methods=['GET', 'PUT', 'POST', 'DELETE', 'HEAD', 'PATCH', 'OPTIONS', 'CONNECT'])
def home(url):
return Response(RESPONSE, mimetype='text/xml')
if __name__ == '__main__':
app.run(debug=True)
那我有下面的代码来测试一下。我使用 Boto3 为 AWS 调用 API。
from boto3.session import Session
import os
credentials = {
'aws_access_key_id': 'sadasdasda',
'aws_secret_access_key': 'dasdasdasd'
}
os.environ["HTTP_PROXY"] = 'http://localhost:5000/'
os.environ["HTTPS_PROXY"] = 'http://localhost:5000/'
session_boto3 = Session(**credentials)
ec2 = session_boto3.client('ec2', 'eu-west-1', verify=False)
regions = ec2.describe_regions()
print regions
问题是:Flask 服务器收到请愿书,但响应不喜欢 Boto3,我收到以下回溯错误:
Traceback (most recent call last):
File "/pruebas_mock/prueba.py", line 82, in <module>
regions = ec2.describe_regions()
File "/mock_aws/local/lib/python2.7/site-packages/botocore/client.py", line 228, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/client.py", line 475, in _make_api_call
operation_model, request_dict)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/endpoint.py", line 117, in make_request
return self._send_request(request_dict, operation_model)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/endpoint.py", line 146, in _send_request
success_response, exception):
File "/mock_aws/local/lib/python2.7/site-packages/botocore/endpoint.py", line 219, in _needs_retry
caught_exception=caught_exception)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/hooks.py", line 226, in emit
return self._emit(event_name, kwargs)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/hooks.py", line 209, in _emit
response = handler(**kwargs)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/retryhandler.py", line 183, in __call__
if self._checker(attempts, response, caught_exception):
File "/mock_aws/local/lib/python2.7/site-packages/botocore/retryhandler.py", line 250, in __call__
caught_exception)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/retryhandler.py", line 273, in _should_retry
return self._checker(attempt_number, response, caught_exception)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/retryhandler.py", line 313, in __call__
caught_exception)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/retryhandler.py", line 222, in __call__
return self._check_caught_exception(attempt_number, caught_exception)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/retryhandler.py", line 355, in _check_caught_exception
raise caught_exception
botocore.vendored.requests.exceptions.SSLError: [Errno 1] _ssl.c:510: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
Flask应该怎么响应?
谢谢。
默认情况下,boto3(以及所有其他 AWS SDK)将使用 SSL 连接到服务。您的代理 Flask 服务器似乎没有使用 SSL,因此您可以在代理中使用 SSL 或告诉 boto3 不要为您的代理服务器使用 SSL:
ec2 = session_boto3.client('ec2', 'eu-west-1', use_ssl=False, verify=False)
verify
参数告诉 boto3 不要尝试验证 SSL 证书,但它仍会尝试通过 SSL 进行连接。 use_ssl=False
告诉它使用纯 HTTP 与您的端点通信。