[Python] 纯文本查看 复制代码
@server.before_request
def handle_before_request():
print('请求:(%s)%s' % (request.method, request.url))
print('请求路径:%s' % request.path)
url = request.url.replace('http://127.0.0.1:56789', URL)
headers = {'Host': 'api.getfiddler.com'}
keys = [
'Host',
'Api-Version',
'Authorization',
'Accept-Encoding',
'Traceparent',
'Content-Type',
'Content-Length',
'Connection',
'Sec-Ch-Ua',
'Accept',
'Sec-Ch-Ua-Mobile',
'User-Agent',
'Sec-Ch-Ua-Platform',
'Sec-Fetch-Site',
'Sec-Fetch-Mode',
'Sec-Fetch-Dest',
'Accept-Encoding',
'Accept-Language'
]
for key in keys:
if request.headers.get(key) is not None:
headers[key] = request.headers.get(key)
if 'Host' in headers:
headers['Host'] = 'api.getfiddler.com'
res = {}
if request.method == "GET":
res = requests.get(url, headers=headers)
elif request.method == "POST":
data = request.get_json()
res = requests.post(url, json=data, headers=headers)
g.res_headers = dict(res.headers)
print('响应头(%s):%s' % (request.path, res.headers))
# 最开始['/versions', '/users'],后续拿到一个接口就把该接口加上
if request.path not in ['/versions', '/users', '/users/sign-in', '/trials/Everywhere/availability', '/push-notifications-configuration',
'/composer-collections',
'/snapshots',
'/events',
'/rulesets'
]:
r = None
try:
r = res.json()
except Exception as e:
...
if r is not None:
print('响应数据-json(%s):%s' % (request.path, r))
print('----------------------------------------------------------------------------')
# h = dict(res.headers)
# del h['Content-Encoding']
# del h['Vary']
# return res.content, 200, h
return jsonify(r), 200, {
'Content-Type': 'application/json; charset=utf-8',
# 'Transfer-Encoding': 'chunked',
'Connection': 'keep-alive',
'Date': 'Fri, 05 Apr 2024 10:21:02 GMT',
'Server': 'Kestrel',
# 'Content-Encoding': 'gzip',
# 'Vary': 'Accept-Encoding',
'X-Date': 'Fri, 05 Apr 2024 10:21:02 GMT',
'Signature': 'SignedHeaders=content-type;x-date, Signature=AAAAWzBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABNsAzGwa7Q3iTZFqv3xYHemw/qxkwk0sIC/usJVi7713VJv0B1JbfuiDXxHfScNyyQjkuaHKtwbn5qUeHjFwpGYeSjYg07lS2j6uB6+K9EvBaf20D/Ra5CEA54/wqToSaXObvZFcZktqPq1rJ6ZbzEo4fljlk/ys8UzMje/YD0lz',
'api-supported-versions': '1.0',
'X-Cache': 'Miss from cloudfront',
'Via': '1.1 88cabd6b8652306789c6bc8090fbcb1a.cloudfront.net (CloudFront)',
'X-Amz-Cf-Pop': 'FRA56-P6',
'Alt-Svc': 'h3=":443"; ma=86400',
'X-Amz-Cf-Id': '9-7P1_bxpKtz2Vfu8sxbarAPcigAEdVVOiVWgzICZuYgZvZm3Z_crA=='
}
else:
print('响应数据-text(%s):%s' % (request.path, res.text))
print('----------------------------------------------------------------------------')
return res.text