python文件流读取二进制_Python使用web.py从POST请求中提取二进制文件

1586010002-jmsa.png

I am developing an API that allows outside clients to send a binary file which will be processed. my web.data() is a string and the function I am calling requires a binary. How do I get it into the correct format? Maybe I have the incorrect Headers? how do I extract the binary data. I am using web.py.

-----------------POST request----------------------------------------------------

import json

import requests

files = {'file':('000038fe4b46c210c37bdde767835007', open('000038fe4b46c210c37bdde767835007', 'rb'))}

headers = {'content-type' : 'application/octet-stream', 'X-Auth-Token':'xxxf'}

r = requests.post('http://XXX:8080/v1/binaries', files = files, headers = header

-----------------------API function------------------------------

def POST(self):

a = web.ctx.env.get("HTTP_X_AUTH_TOKEN", None)

creds = authenticator(a)

postdata = web.data().read()

analysis = atklite.FileAnalysis(data=postdata)

metadata = analysis.return_analysis()

------------------------Traceback--------------------------------

File "/usr/lib/pymodules/python2.7/web/application.py", line 242, in process

return self.handle()

File "/usr/lib/pymodules/python2.7/web/application.py", line 233, in handle

return self._delegate(fn, self.fvars, args)

File "/usr/lib/pymodules/python2.7/web/application.py", line 415, in _delegate

return handle_class(cls)

File "/usr/lib/pymodules/python2.7/web/application.py", line 390, in handle_class

return tocall(*args)

File "/home/XXXXXX/ProcessingCode/bfsapi.py", line 75, in POST

postdata = web.data().read()

AttributeError: 'str' object has no attribute 'read'

Thanks

Sorry if the formatting got all messed up in the Post.

解决方案

Even if it is a binary file, reading raw post data would get you a encoded string. You would need to decode to convert to binary data. You can write to a file as follows:

written = open('binary.file', 'wb')

for chunk in rawdata.chunks():

written.write(chunk)

written.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值