我已经能够查看botocore发送的PreparedRequest的属性,但是我想知道如何查看发送到AWS的确切请求字符串。我需要确切的请求字符串才能将其与我正在测试AWS调用的另一个应用程序进行比较。
您还可以在boto3中启用调试日志记录。 这将记录所有请求和响应以及许多其他内容。 启用它有点晦涩:
import boto3
boto3.set_stream_logger(name='botocore')
您需要指定botocore
作为记录名称的原因是所有实际请求和响应都发生在botocore层。
所以你可能想要做的是通过代理(mitmproxy、squid)发送请求。然后检查代理发送了什么。由于HTTPS数据是加密的,您必须首先解密它,然后记录响应,再将其加密并发送到AWS。其中一个选项是使用mitmproxy。(安装非常容易)
Open up another terminal and point proxy to mitmproxys port:
export http_proxy=127.0.0.1:8080
export https_proxy=$http_proxy
Then set verify=False
when creating session/client
In [1]: import botocore.session
In [2]: client = botocore.session.Session().create_client('elasticache', verify=False)
Send request and look at the output of mitmproxy
In [3]: client.describe_cache_engine_versions()
The result should be similar to this:
Host: elasticache.us-east-1.amazonaws.com
Accept-Encoding: identity
Content-Length: 53
Content-Type: application/x-www-form-urlencoded
Authorization: AWS4-HMAC-SHA256 Credential=FOOOOOO/20150428/us-east-1/elasticache/aws4_request, SignedHeaders=host;user-agent;x-amz-date, Signature=BAAAAAAR
X-Amz-Date: 20150428T213004Z
User-Agent: Botocore/0.103.0 Python/2.7.6 Linux/3.13.0-49-generic
<?xml version='1.0' encoding='UTF-8'?>
<DescribeCacheEngineVersionsResponse
xmlns="http://elasticache.amazonaws.com/doc/2015-02-02/">
<DescribeCacheEngineVersionsResult>
<CacheEngineVersions>
<CacheEngineVersion>
<CacheParameterGroupFamily>memcached1.4</CacheParameterGroupFamily>
<Engine>memcached</Engine>
<CacheEngineVersionDescription>memcached version 1.4.14</CacheEngineVersionDescription>
<CacheEngineDescription>memcached</CacheEngineDescription>
<EngineVersion>1.4.14</EngineVersion>
import logging
logging.getLogger('botocore').setLevel(logging.DEBUG)
来达到同样的效果。 - Craig Ringer