使用瓶子服务器重新传输内容(例如模拟端口转发)

2024-04-28 17:39:08 发布

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

我在一个内部网络上有许多raspberry PI,通过HTTP成功地传输了一个mjpg提要

PI由运行在集线器上的WEB服务器控制。该中心属于PIs的同一网络,但也可以通过互联网访问

现在-我希望集线器在其自己的Web服务器内中继它从PI获得的流

原则上,在伪代码中,我希望在集线器上运行类似的东西:

@app.get('/device/<id>/stream')
def get_device_stream(rPI):

    url = "http://rPI.ip:rPI.port/stream.jpg"
    req = urllib2.Request(url)
    f = urllib2.urlopen(req, timeout=5)
    return f.read()

显然,这是行不通的,因为实时提要没有内容长度

我可以让这项工作设置一个tcp转发器,但我正在寻找一个解决方案,不需要我创建一个端口转发线程


Tags: 网络服务器webhttpurlstreamgetdevice
1条回答
网友
1楼 · 发布于 2024-04-28 17:39:08

我找到了解决办法。人们需要接收流并将其分解为单个帧,然后将其作为视频馈送再次发送出去。像这样:

def relay_stream():
stream_url = "http://217.7.233.140:80/cgi-bin/faststream.jpg?stream=full&fps=0"

    req = urllib2.Request(stream_url)
    stream = urllib2.urlopen(req, timeout=5)
    bytes = b''
    while True:
        bytes += stream.read(1024)
        a = bytes.find(b'\xff\xd8') #frame starting 
        b = bytes.find(b'\xff\xd9') #frame ending
        if a != -1 and b != -1:
            frame = bytes[a:b+2]
            bytes = bytes[b+2:]
            yield (b' frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

@app.get('/device/<id>/stream')
@error_decorator
def get_device_stream(id):

    response.set_header('Content-type', 'multipart/x-mixed-replace; boundary=frame')
    return relay_stream()

相关问题 更多 >