为什么来自python中的ngrok的requests.get的原始内容不可读?

时间:2018-04-15 19:36:11

标签: python request

我有一个关于Python 3.6中请求库的get响应中的原始内容的问题。我正在使用ngrok捕获http post并在python上检索它们以执行数据集成和自动化。我的问题是:虽然编码是utf-8,并且内容类型是来自帖子源的application / json,但get响应似乎只显示一个原始不可读的文本体。我尝试使用urllib进行解析。代码非常简单,因为我假设应该给出ngrok文档:

url ="http://localhost:4040/api/requests/http"
bin = requests.get(url=url headers = {'Content-Type': 'application/json ;charset=utf-8'} )
print(bin.json())

这是回复:

{
'uri': '/api/requests/http',
'requests': [
    {
        'uri': '/api/requests/http/5ad38f700000001f',
        'id': '5ad38f700000001f',
        'tunnel_name': 'command_line (http)',
        'remote_addr': '23.96.4.241',
        'start': '2018-04-15T13:44:16-04:00',
        'duration': 16037600,
        'request': {
            'method': 'POST',
            'proto': 'HTTP/1.1',
            'headers': {
                'Content-Length': ['2848'],
                'Content-Type': ['application/json; charset=utf-8'],
                'Expect': ['100-continue'],
                'Request-Context': [
                    'appId=cid-v1:bac45fa9-8f8e-4ddb-8e70-d648fe57a27a'
                ],
                'Request-Id': ['|ohznIKUg+KI=.409af374_22.'],
                'User-Agent': ['Go-http-client/1.1'],
                'X-Forwarded-For': ['23.96.4.241'],
                'X-Ms-Request-Id': ['|ohznIKUg+KI=.409af374_22.'],
                'X-Ms-Request-Root-Id': ['ohznIKUg+KI='],
                'X-Original-Host': ['a35b204d.ngrok.io'],
                'X-Requested-With': ['XMLHttpRequest']
            },
            'uri': '/',
            'raw':
                'UE9TVCAvIEhUVFAvMS4xDQpIb3N0OiBsb2NhbGhvc3QNClVzZXItQWdlb'
                'nQ6IEdvLWh0dHAtY2xpZW50LzEuMQ0KQ29udGVudC1MZW5ndGg6IDI4ND'
                'gNCkNvbnRlbnQtVHlwZTogYXBwbGljYXRpb24vanNvbjsgY2hhcnNldD1'
                '1dGYtOA0KRXhwZWN0OiAxMDAtY29udGludWUNClJlcXVlc3QtQ29udGV4'
                'dDogYXBwSWQ9Y2lkLXYxOmJhYzQ1ZmE5LThmOGUtNGRkYi04ZTcwLWQ2N'
                'DhmZTU3YTI3YQ0KUmVxdWVzdC1JZDogfG9oem5JS1VnK0tJPS40MDlhZj'
                'M3NF8yMi4NClgtRm9yd2FyZGVkLUZvcjogMjMuOTYuNC4yNDENClgtTXM'
                'tUmVxdWVzdC1JZDogfG9oem5JS1VnK0tJPS40MDlhZjM3NF8yMi4NClgt'
                'TXMtUmVxdWVzdC1Sb290LUlkOiBvaHpuSUtVZytLST0NClgtT3JpZ2luY'
                'WwtSG9zdDogYTM1YjIwNGQubmdyb2suaW8NClgtUmVxdWVzdGVkLVdpdG'
                'g6IFhNTEh0dHBSZXF1ZXN0DQoNCnsiRm9ybSI6eyJJZCI6IjEiLCJJbnR'
                'lcm5hbE5hbWUiOiJPcmRlckZvcm0iLCJOYW1lIjoiT3JkZXIgRm9ybSJ9'
                'LCIkdmVyc2lvbiI6NiwiJGV0YWciOiJXL1wiZGF0ZXRpbWUnMjAxOC0wN'
                'C0xNVQxNyUzQTQ0JTNBMTQuNzMzMDI4OFonXCIiLCJFbnRyeSI6eyJBZG'
                '1pbkxpbmsiOiJodHRwczovL3d3dy5jb2duaXRvZm9ybXMuY29tL1hGYWN'
                '0b3JNZWFsczEvMS9lbnRyaWVzLzE3IiwiRGF0ZUNyZWF0ZWQiOiIyMDE4'
                'LTA0LTE1VDE3OjQ0OjE0LjUxM1oiLCJEYXRlU3VibWl0dGVkIjoiMjAxO'
                'C0wNC0xNVQxNzo0NDoxNC41MTNaIiwiRGF0ZVVwZGF0ZWQiOiIyMDE4LT'
                'A0LTE1VDE3OjQ0OjE0LjUxM1oiLCJFZGl0TGluayI6Imh0dHBzOi8vd3d'
                '3LmNvZ25pdG9mb3Jtcy5jb20vWEZhY3Rvck1lYWxzMS9PcmRlckZvcm0j'
                'MWlveUh5WGdRVlJ2dlZpLWRxYi00dW0tZHVWU216VVhsYjZNWW03VU9FQ'
                'SQqIiwiTGFzdFBhZ2VWaWV3ZWQiOm51bGwsIk51bWJlciI6MTcsIk9yZG'
                'VyIjpudWxsLCJPcmlnaW4iOnsiQ2l0eSI6bnVsbCwiQ291bnRyeUNvZGU'
                'iOm51bGwsIklwQWRkcmVzcyI6IjE0OC43NS4xODEuMTMxIiwiSXNJbXBv'
                'cnRlZCI6ZmFsc2UsIlJlZ2lvbiI6bnVsbCwiVGltZXpvbmUiOm51bGwsI'
                'lVzZXJBZ2VudCI6Ik1vemlsbGEvNS4wIChXaW5kb3dzIE5UIDEwLjA7IF'
                'dpbjY0OyB4NjQ7IHJ2OjU5LjApIEdlY2tvLzIwMTAwMTAxIEZpcmVmb3g'
                'vNTkuMCJ9LCJQYXltZW50VG9rZW4iOm51bGwsIlN0YXR1cyI6IlN1Ym1p'
                'dHRlZCIsIlRpbWVzdGFtcCI6IjIwMTgtMDQtMTVUMTc6NDQ6MTQuNTEzW'
                'iIsIlZpZXdMaW5rIjoiaHR0cHM6Ly93d3cuY29nbml0b2Zvcm1zLmNvbS'
                '9YRmFjdG9yTWVhbHMxL09yZGVyRm9ybSNWYWhXbm1HcnZxOUE2SlpPWWh'
                'aZTMtNWJHdWEzSEVEdUZTRjNoRWsxMmI4JCoifSwiSWQiOiIxLTE3Iiwi'
                'T3JnYW5pemF0aW9uIjoib2theSIsIkJha2VkQ2hpY2tlblRlbmRlcnMiO'
                'iI4IiwiQkJRQ2hpY2tlbiI6IjgiLCJMb3dDYXJiQkJRQ2hpY2tlbiI6Ij'
                'AiLCJUZXJpeWFraUNoaWNrZW4iOiIwIiwiTG93Q2FyYlRlcml5YWtpQ2h'
                'pY2tlbiI6IjUiLCJHZW5lcmFsVHNvQ2hpY2tlbiI6IjAiLCJMb3dDYXJi'
                'R2VuZXJhbFRzb0NoaWNrZW4iOiIwIiwiQ2hpY2tlbkJ1cnJpdG9Cb3dsI'
                'joiMCIsIkxvd0NhcmJDaGlja2VuQnVycml0b0Jvd2wiOiIwIiwiU291dG'
                'hXZXN0Q2hpY2tlbkJvd2wiOiIyIiwiRW1haWwiOiJpc2Nvb2xAZ21haWw'
                'uY29tIiwiTmFtZSI6eyJGaXJzdCI6Ikl0cyIsIkZpcnN0QW5kTGFzdCI6'

如果我没有看到任何明显的事情,我很抱歉这是我第一次处理ngrok和web钩子数据。我感谢任何见解和任何讲座。我假设问题可能是编码?

谢谢

1 个答案:

答案 0 :(得分:0)

这是一个base64编码的Http数据包。你可以做到

import binascii
data = bin.json()
packet = binascii.a2b_base64(data["requests"][0]["reuqest"]["raw"]))

你会得到

b'POST / HTTP/1.1\r\nHost: localhost\r\nUser-Agent: Go-http-
client/1.1\r\nContent-Length: 2848\r\nContent-Type: applicat
ion/json; charset=utf-8\r\nExpect: 100-continue\r\nRequest-C
ontext: appId=cid-v1:bac45fa9-8f8e-4ddb-8e70-d648fe57a27a\r
...