Python Requests - no response when requesting mjpeg data

1k views Asked by At

I have a simple streaming server providing mjpeg streams (https://blog.miguelgrinberg.com/post/video-streaming-with-flask/page/3). When accessing the mjpeg data via browser it works as expected. I see a webcam stream.

If I do a get request via python (using python requests: http://requests.readthedocs.io/) I neither get any response (also tested with Insomnia REST Client) nor a status code/exception.

requests.get(url)

What I want to do is to write a proxy script (in python) which simply forwards the mjpeg stream.

1

There are 1 answers

1
Him On

The requests library is probably not the right option for forwarding a video stream, since you'll have to fetch a chunk of video, then somehow interpret that as video and then turn that BACK into a stream that you can put on a network. Furthermore, your video is likely NOT being streamed over HTTP, which is the protocol requests.get supports. The best option would be to simply set up your computer as a proxy server: http://www.aboutdebian.com/proxy.htm, that way a packet basically gets written to and read from a socket, just rewriting the high-layer info such as where to send the packet, without having to interpret the guts of the packet.

That said, if you really want to do what you are suggesting, and in the unlikely event that the video is somehow being streamed over HTTP, you want stream=True

stream = requests.get(url, stream=True)
if stream.ok:
    chunk_size = 1024
    for chunk in stream.iter_content(chunk_size=chunk_size):
        interpret_video_and_write_to_socket_somehow(chunk, blocking=True)