1

I built a simple demo project using FastAPI. I would like to POST data to the server in real time (maybe 30fps).

The client:

while True:
    ....
    res = requests.post(URL, files={'input_data' : input_data})
    ....

But, I get the following error:

(MaxRetryError: HTTPConnectionPool(host='~~', port=8000): Max retries exceeded with url)

I think it is caused due to issuing multiple requests. I would like to perform requests in real time. How can I do that?

Chris
  • 18,724
  • 6
  • 46
  • 80
haneul
  • 31
  • 1
  • 5
  • 3
    Use a more suitable protocol than regular http posts; instead, use websockets (supported by FastAPI/Starlette)? https://fastapi.tiangolo.com/advanced/websockets/ – MatsLindh Apr 14 '22 at 09:20

1 Answers1

3

As noted by @MatsLindh in the comments, you should rather use a more suitable protocol - such as WebSockets - than HTTP for such a task. FastAPI/Starlette supports sending and receiving data on a websocket (see the documentation here and here). Below is an example of using websockets to send video frames from a client to a server (assuming this is your task from your comment on 30fps - however, the same appproach could be applied to sending other types of data). OpenCV is used to capture the frames and websockets library is used to connect to the WebSocket server.

server.py

from fastapi import FastAPI, WebSocket, WebSocketDisconnect
import cv2
import numpy as np

app = FastAPI()

@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
    # listen for connections
    await websocket.accept()
    #count = 1
    try:
        while True:
            contents = await websocket.receive_bytes()
            arr = np.frombuffer(contents, np.uint8)
            frame = cv2.imdecode(arr, cv2.IMREAD_UNCHANGED)
            cv2.imshow('frame', frame)
            cv2.waitKey(1)
            #cv2.imwrite("frame%d.png" % count, frame)
            #count += 1
    except WebSocketDisconnect:
        cv2.destroyWindow("frame")
        print("Client disconnected") 

client.py

import websockets
import asyncio
import cv2

camera = cv2.VideoCapture(0, cv2.CAP_DSHOW)

async def main():
    # Connect to the server
    async with websockets.connect('ws://localhost:8000/ws') as ws:
         while True:
            success, frame = camera.read()
            if not success:
                break
            else:
                ret, buffer = cv2.imencode('.png', frame)
                await ws.send(buffer.tobytes())

# Start the connection
asyncio.run(main())
Chris
  • 18,724
  • 6
  • 46
  • 80
  • thank you for answer! I have a question. As in my question code, I want to get the result from the server. is it possible to use async def websocket_endpoint? – haneul Apr 15 '22 at 06:31
  • To return a response from the server you can use, for instance , `await websocket.send_bytes(data)` or `await websocket.send_text(data)`, as documented in the links provided above. To receive the response on client side, you can use `resp = await ws.recv()` – Chris Apr 15 '22 at 06:56
  • is it possible to send data to client from server? I wanna get output in client from server – haneul Apr 18 '22 at 00:23
  • Please have a look at [this answer](https://stackoverflow.com/a/70626324/17865804) as well. – Chris Apr 18 '22 at 07:16