我已经能够查看botocore发送的PreparedRequest的属性,但我想知道如何查看发送给AWS的确切请求字符串。我需要确切的请求字符串才能将它与我正在测试AWS调用的另一个应用程序进行比较。
答案 0 :(得分:25)
您还可以在boto3中启用调试日志记录。这将记录所有请求和响应以及许多其他事情。启用它有点模糊:
import boto3
boto3.set_stream_logger(name='botocore')
您必须指定botocore
作为记录名称的原因是所有实际请求和响应都发生在botocore层。
答案 1 :(得分:4)
所以你可能想做的是通过代理发送你的请求(mitmproxy,squid)。然后检查代理以查找发送的内容。 由于HTTPS数据已加密,因此您必须先对其进行解密,然后记录响应,然后对其进行加密并发送给AWS。其中一个选项是使用mitmproxy。 (它非常容易安装)
打开另一个终端并将代理指向mitmproxys端口:
export http_proxy=127.0.0.1:8080
export https_proxy=$http_proxy
然后在创建会话/客户端时设置verify=False
In [1]: import botocore.session
In [2]: client = botocore.session.Session().create_client('elasticache', verify=False)
发送请求并查看mitmproxy的输出
In [3]: client.describe_cache_engine_versions()
结果应与此类似:
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>