使用python请求通过POST发送图像时,只有第一个字节到达

2024-04-25 13:08:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在尝试向python flask服务器提供一些PIL图像。发送请求的代码如下所示:

...
for i in range(0,1):  
  ...
  chip_gray = Image.fromarray(chip)
  display(chip_gray) #This displays correctly

  byteIO = io.BytesIO()
  chip_gray.save(byteIO, format='PNG')
  byteArr = byteIO.getvalue()
  print(byteArr) #This prints correctly
  length = len(byteArr)

  url = "http://facerec-server.herokuapp.com/add_custom"

  querystring = {"label": lfw_people.target_names[lfw_people.target[i]]}

  headers = {
      'Content-Type': "image/png",
      'Accept': "*/*",
      'Cache-Control': "no-cache",
      'Host': "facerec-server.herokuapp.com",
      'Accept-Encoding': "gzip, deflate",
      'Connection': "keep-alive",
      'Content-Length': str(length),
      'cache-control': "no-cache"
      }

  response = requests.request("POST", url, headers=headers, params=querystring, data={byteArr})

  print(response.text)

我已经建立了相同的方式,我已经测试了它,并通过邮递员验证了它的工作结构。尽管出于某种原因,这段代码(从GoogleColab运行),只有第一个字节到达服务器。下面是打印出来的内容:

2019-11-26T22:46:41.238471+00:00 app[web.1]: b'\x89'
2019-11-26T22:46:41.238626+00:00 app[web.1]: Host: facerec-server.herokuapp.com
2019-11-26T22:46:41.238628+00:00 app[web.1]: Connection: close
2019-11-26T22:46:41.238630+00:00 app[web.1]: User-Agent: python-requests/2.21.0
2019-11-26T22:46:41.238631+00:00 app[web.1]: Accept-Encoding: gzip, deflate
2019-11-26T22:46:41.238633+00:00 app[web.1]: Accept: */*
2019-11-26T22:46:41.238634+00:00 app[web.1]: Content-Type: image/png
2019-11-26T22:46:41.238635+00:00 app[web.1]: Cache-Control: no-cache
2019-11-26T22:46:41.238640+00:00 app[web.1]: X-Request-Id: dc61fe1e-5616-4b6d-8870-54b519be9f17
2019-11-26T22:46:41.238641+00:00 app[web.1]: X-Forwarded-For: 35.201.212.71
2019-11-26T22:46:41.238643+00:00 app[web.1]: X-Forwarded-Proto: http
2019-11-26T22:46:41.238644+00:00 app[web.1]: X-Forwarded-Port: 80
2019-11-26T22:46:41.238645+00:00 app[web.1]: Via: 1.1 vegur
2019-11-26T22:46:41.238646+00:00 app[web.1]: Connect-Time: 0
2019-11-26T22:46:41.238648+00:00 app[web.1]: X-Request-Start: 1574808401237
2019-11-26T22:46:41.238649+00:00 app[web.1]: Total-Route-Time: 0
2019-11-26T22:46:41.238650+00:00 app[web.1]: Content-Length: 1
2019-11-26T22:46:41.238746+00:00 app[web.1]: CombinedMultiDict([ImmutableMultiDict([('label', 'George W Bush')]), ImmutableMultiDict([])])
...
2019-11-26T22:46:41.239577+00:00 app[web.1]:   File "server.py", line 49, in method_custom
2019-11-26T22:46:41.239579+00:00 app[web.1]:     img = np.array(Image.open(io.BytesIO(r.data)))
2019-11-26T22:46:41.239580+00:00 app[web.1]:   File "/usr/local/lib/python3.6/site-packages/PIL/Image.py", line 2818, in open
2019-11-26T22:46:41.239581+00:00 app[web.1]:     raise IOError("cannot identify image file %r" % (filename if filename else fp))
2019-11-26T22:46:41.239583+00:00 app[web.1]: OSError: cannot identify image file <_io.BytesIO object at 0x7ff04408e0f8>
2019-11-26T22:46:41.240073+00:00 app[web.1]: 10.12.43.130 - - [26/Nov/2019 22:46:41] "[1m[35mPOST /add_custom?label=George+W+Bush HTTP/1.1[0m" 500 -

请注意,内容长度设置为1,并且只有第一个字节到达

以下是读取图像(并与邮递员请求一起使用)的服务器上的代码:

@app.route('/add_custom', methods=['POST'])
    def add_custom():
        print(request.data, flush=True)
        print(request.headers, flush=True)
        print(request.values, flush=True)
        start_time = time.time()
        data = np.array([method_custom(request)], dtype=np.float32)
        index_custom.add(data)
        index_ids_custom.append(request.args.get('label'))
        return Response(response=json.dumps({"time": (time.time() - start_time), "entries": len(index_ids_custom)}), status=200, content_type="application/json")

你们有谁知道这是为什么吗


Tags: imageaddappdataservertimerequestcustom
1条回答
网友
1楼 · 发布于 2024-04-25 13:08:26

因此,您遇到的问题是Content-Length是1,这是服务器用来只读取一个字节的

您能否在客户端代码中打印头以确保Content-Length不是1?另一个选择是停止自己设置Content-Length,因为请求可以为您这样做

现在让我们看一下请求调用:

response = requests.request("POST", url, headers=headers, params=querystring, data={byteArr})

你为什么要包装byteArr?我想你实际上只是想要这里。还要注意requests.request("POST", ...)可以缩短为requests.post(...)

相关问题 更多 >