How to transfer media stream from SocketSever to Falcon API with Python

184 Views Asked by At

I am developping a small python server that aims to receive a MP4 stream as a socket server and I want to transfer the data to another client that connect with a GET request with Falcon.

Here my API server that send the media:

class StreamResource:
    def on_get(self, req, resp):
        #Throw error rfile is not defined
        resp.stream = hlserver.socket_list[0].rfile.iter_content(io.DEFAULT_BUFFER_SIZE)

app = falcon.API()
app.add_route('/livestream.mp4', StreamResource())

Here my socket server that receive the media

class MyTCPHandler(socketserver.StreamRequestHandler):

    def setup(self):
      print(self.client_address[0],' connected')
      socket_list.append(self)

    def handle(self):
        #Have to keep this to prevent client to be disconnected
        self.data = self.request.recv(1024).strip()

    def finish(self):
      print(self.client_address[0],' disconnected')
      socket_list.remove(self)



class ThreadedSocketServer(object):

    def __init__(self, interval=1):
    
        self.interval = interval

        thread = threading.Thread(target=self.run, args=())
        thread.daemon = True                            # Daemonize thread
        thread.start()                                  # Start the execution

    def run(self):
      HOST, PORT = "localhost", 8001

      # Create the server, binding to localhost on port 9999
      server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)

      # Activate the server; this will keep running until you
      # interrupt the program with Ctrl-C
      server.serve_forever()

So my main goal is to transfer data from the MyTCPHandler.handle to the StreamResource.on_get.

  1. I don't know what is the best way to do this. Is it possible to pass a stream refererence or should I copy the bytes chunk in a buffer or anything else?
  2. I tried to use the exemple from socketserver documentation on StreamRequestHandler. They are using the rfile attribute but when I do the same I have AttributeError: 'MyTCPHandler' object has no attribute 'rfile'
  3. Also if I set the MyTCPHandler.handle method empty, I get the client immediatly disconnected everytime after connection

Thank you for your help

0

There are 0 best solutions below