【发布时间】:2019-03-01 11:27:05
【问题描述】:
我有一个解码二进制文件的本地 python 文件。这个 python 文件首先从文件中读取,将其作为二进制文件打开,然后将其保存在缓冲区中并进行解释。读起来很简单:
with open(filepath, 'rb') as f:
buff = f.read()
read_all(buff)
这在本地运行良好。现在我想设置一个 Azure Python 作业,我可以在其中发送文件,大约。 100kb,通过 HTTP POST,然后读取我的原始 python 脚本做得很好的解释元数据。
我首先删除了读取功能,以便我现在只使用缓冲区。 在我的 Azure Python 作业中,我有以下内容,由 HttpRequest 触发
my_data = reader.read_file(req.get_body())
为了测试我的发送,我在 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
我还尝试在 Postman 中将文件作为二进制文件添加到正文中并将标头设置为 application/octet-stream
所有这些都不会像原来的 f.read() 那样发送二进制文件。所以我对二进制文件的解释是错误的。
file.read 与我将其作为 HTTP 正文消息发送的方式有何不同?
从本地python读取文件中打印出第一行。
b'\n\n\xfe\xfe\x00\x00\x00\x00\\\x18,A\x18\x00\x00\x00(\x00\x00\x00\x1f\x00\x00\
而在 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\
所以显然有些地方是错误的。任何帮助为什么这可能会有所不同?
谢谢
编辑:
我在 Flask 中实现了一个类似的功能,效果很好。 flask 中的代码只是从 POST 中获取文件。没有编码/解码。
if request.method == 'POST':
f = request.files['file']
#f.save(secure_filename(f.filename))
my_data = reader.read_file(f.read())
为什么 Azure Function 不同?
【问题讨论】:
标签: python azure http python-requests azure-functions