简体   繁体   English

通过HTTP POST传递二进制文件

[英]Passing Binary file over HTTP POST

I have a local python file that decodes binary files. 我有一个本地python文件,可解码二进制文件。 This python file first reads from the file, opens it as binary and then saves it in a buffer and interprets it. 这个python文件首先从文件中读取文件,将其打开为二进制文件,然后将其保存在缓冲区中并对其进行解释。 Reading it is simply: 读起来很简单:

with open(filepath, 'rb') as f:
    buff = f.read()
read_all(buff)

This works fine locally. 这在本地工作正常。 Now I'd like to setup a Azure Python job where I can send the file, approx. 现在,我想设置一个Azure Python作业,可以在其中发送文件。 100kb, over a HTTP POST and then read the interpreted meta data which my original python script does well. 100kb,通过HTTP POST,然后读取我的原始python脚本效果很好的解释后的元数据。

I've first removed the read function so that I'll now work with the buffer only. 我首先删除了read函数,以便现在仅使用缓冲区。 In my Azure Python Job I have the following, triggered by a HttpRequest 在我的Azure Python作业中,有以下内容,由HttpRequest触发

my_data = reader.read_file(req.get_body())

To test my sending I've tried the following in python 为了测试我的发送,我在python中尝试了以下内容

import requests

url = 'http://localhost:7071/api/HttpTrigger'
files = {'file': open('test', 'rb')}
with open('test', 'rb') as f:
        buff = f.read()

r = requests.post(url, files=files) #Try using files
r = requests.post(url, data=buff) #Try using data

I've also tried in Postman adding the file to the body as a binary and setting the headers to application/octet-stream 我还在Postman中尝试过将文件作为二进制文件添加到主体并将标题设置为application / octet-stream

All this doesn't send the binary file the same way as the original f.read() did. 所有这一切都不会像原始f.read()那样发送二进制文件。 So I'm getting a wrong interpretation of the binary file. 因此,我对二进制文件的解释有误。

What is file.read doing differently to how I'm sending it over as a HTTP Body message? file.read与通过HTTP Body消息发送它的方式有何不同?

Printing out the first line from the local python read file gives. 从本地python读取文件中打印出第一行即可。

b'\n\n\xfe\xfe\x00\x00\x00\x00\\\x18,A\x18\x00\x00\x00(\x00\x00\x00\x1f\x00\x00\

Whereas printing it out at the req.get_body() shows me 而在req.get_body()上打印出来显示给我

b'\n\n\xef\xbf\xbd\xef\xbf\xbd\x00\x00\x00\x00\\\x18,A\x18\x00\x00\x00(\x00\x00\x00\x1f\x00\

So something is clearly wrong. 所以显然是错误的。 Any help why this could be different? 有什么帮助,为什么这可能有所不同?

Thanks 谢谢

EDIT: 编辑:

I've implemented a similar function in Flask and it works well. 我在Flask中实现了类似的功能,并且效果很好。 The code in flask is simply grabbing the file from a POST. flask中的代码只是从POST获取文件。 No encoding/decoding. 没有编码/解码。

if request.method == 'POST':
      f = request.files['file']
      #f.save(secure_filename(f.filename))
      my_data = reader.read_file(f.read())

Why is the Azure Function different? 为什么Azure功能会有所不同?

You can try UTF-16 to decode and do the further action in your code. 您可以尝试使用UTF-16进行解码,并在代码中执行进一步的操作。

Here is the code for that: 这是该代码:

 with open(path_to_file,'rb') as f: contents = f.read() contents = contents.rstrip("\\n").decode("utf-16") 

Basically after doing re.get_body, perform the below operation: 基本上在完成re.get_body之后,执行以下操作:

contents = contents.rstrip("\n").decode("utf-16")

See if it gives you the same output as your receive in local python file. 查看它是否为您提供与本地python文件中的接收相同的输出。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM