python webhook,传入 gzip 数据-写入文件
对 Python 相当陌生,做了一些练习,现在尝试使用 API 和 webhok。
我的目标:我有一个网络管理系统,每 120 秒可以根据附近客户端的 WiFi 可见性向我发送位置数据
我编写了这个简单的代码并获取传入数据 - 数据经过 gzip 压缩,因此我以二进制形式看到它们
from flask import Flask, request
app = Flask(__name__)
@app.route('/', methods=['POST'])
def index():
print(request.headers)
print(request.data)
return ('This is a website. Port 443 - Https. ', 200, None)
if __name__ == '__main__':
app.run(debug=True, use_reloader=True, ssl_context='adhoc', port=443)
结果是:
主机:e7145a924eea.eu.ngrok.io 用户代理:Java/1.8.0_251 内容长度:1940 接受:text/plain、application/json、application/x-jackson-smile、application/cbor、application/*+json、 / 授权:Basic 11223344 内容编码:gzip 内容类型:application/json X-Ah-Event-Type:位置 X-Ah-Message-Type:LOCATION_CLIENT_CENTRIC X-Forwarded-For:1.2.3.4 X-Forwarded-Proto:https Accept-Encoding:gzip
b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x00\xed\x9cYO\x1bI\x10\x80\xff\xcb<\x93Q\x1fU}\xf8\xad\xaf\xd1"\xe5\x90V<m\x14E^\xf0&H\x04\x10Gv \xa3\x88\xff\xbe5v\x886Am\x9c\xb6{`\x1b\xa4(\x1a\x18\xe39\xbe\xba\xab\xba\xbfv\x9f\x16\x97\x97\xf3\x0f\x8bW\x8b\xaby\x9c_\xcd\xbb\xd9\xd7n\xf1yqzu\xf0\xe5|\x d1\xcd\xba\x97o\x82;\xd8\x7f\xf3\xba\xdb\xbb\xfd\xe4O'\xde\x87\x97\xfb\xe9\xf5\xc1\xfb@\xff\xfd\xbe\x1f\xba\x9b\xbd\xee\xe8\xdb\xf7\x 9c\xfd}\xba\xb8\xd8?...o missis...\x99Tqo\xe8\x91pi
我只是想将二进制流写入文件,但如果我尝试执行类似
with open("D:\\Dati\python\\200_WebApps_and_webhooks\\API.gzip",'wb') as API_data:
print(request.data, file=API_data)
的操作,我收到错误 TypeError:需要一个类似字节的对象,而不是“str”
这个想法是将原始传入数据发送到可以解压以查看 json 格式的文件。
下一步将即时解压并以类似格式进行打印
“bodyRaw”:“{”messageMetaData“:{”eventType“:“LOCATION”,“messageType”:“LOCATION_CLIENT_CENTRIC”},“数据”:“ownerId”:“136502,”observations“:[{”clientMac“:“aaabbbcccddd”,“ipv4”:null,“ipv6”:null,“seenTime”:“2021-01-08T16:43:25.154Z ","seenEpoch":1610124205154,"userName":null,"authMethod":null,"ssid":null,"manufacturer":null,"os":null,"lat":0.0,"lng":0.0,"x":2.916691159714196,"y":1.9132701186769128,"clientMetaData":null,"deviceObservers":[{"apMac":"885BDD8B3E40","rssi":- 74,"locationIdHierarchy":[586271625939276,586271625939269,586271625939267]}]},{"clientMac":"7825AD1136FC","ipv4":null,"ipv6":null,"seenTime":"2021-01-08T16:43:25.163Z","seenEpoch":1610124205163,"userName":null,"authMethod":null,"ssid":null,"制造商”:null,“操作系统”:null,“纬度”:0.0,“经度”:0.0,“x”:13.302300953114472,“y”:8.587561374795419,“clientMetaData”:null,“deviceObservers”:[{“apMac”:“885BDD8B3E40”,“rssi”:-87,“locationIdHierarchy”:[586271625939276,586271625939269,586271625939267]} ...omissis..
提前致谢
Franco
在您的
with
块中,
print
语句无法按预期工作。它用于将内容打印到服务器控制台。
我认为您正在寻找类似以下内容的内容:
with open("D:\\Dati\python\\200_WebApps_and_webhooks\\API.gzip",'wb') as API_data:
API_data.write(request.data)
使用 gzip.open() 打开 .gz 文件,而不是 'open'
import gzip
with gzip.open(filepath,"rt") as API_data: # Unzipping and opening the file in text format
for line in API_data:
print(line) # Printing each line, you can save these lines to a file if you would like
感谢您的提示,我使用两者并编写了此示例代码来整理此问题
from flask import Flask, request
import gzip
app = Flask(__name__)
@app.route('/', methods=['POST'])
def index():
"""
Go to https://localhost to see a message
"""
data = request.data
print("data -> ", data)
plaindata = gzip.decompress(data)
print("decomp data ", plaindata)
return ('This is a website. Port 443 - Https. ', 200, None)
if __name__ == '__main__':
app.run(debug=True, use_reloader=True, ssl_context='adhoc', port=443)
输出为
数据 -> b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xed[\xdbN\xe3H\x10\xfd\x17?g\xac\xbeWu\xde\xecv\xb7\x16i\x06\xa4\x15O;B#o\xf0\x0e\x91 AI`w\x84\xf8\xf7\xadp\xd3\x0e....omissis
分解数据b'{“messageMetaData”:{“eventType”:“LOCATION”,“messageType”:“LOCATION_CLIENT_CENTRIC”},“data”:{“ownerId”:136502,“observations”:[{“clientMac”:“A89CED7E3B77”,....遗漏