基于 TCP 的 Python 流数据 [英] Python stream data over TCP

查看:24
本文介绍了基于 TCP 的 Python 流数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚遇到了一个严重的网络问题.我正在设置一个虚拟雷达服务器,其中至少有一个 RasPi 作为 ADS-B 接收器.只是为了解释这一切:飞机在公共频率(1080MHz)上传输位置数据,我想显示它.有点像 Flightradar24.

I just ran into a serious networking Problem. I am in the process to setup a Virtual Radar Server with at least one RasPi as ADS-B receiver. Just to explain what this is all about: Airplanes transmit location data on a public frequency (1080MHz) and I want to display that. It's kind like Flightradar24.

在我的 Pi 上,我运行 Malcolm Robb 的 dump1090,它通过 SDR 设备(DVB-T 棒)拦截这些传输,并提供带有解码数据的网络流.如果您只是在 Pi 的端口 30003 上 telnet,则可以观看此流.到目前为止,一切都很好.

On my Pi I run dump1090 from Malcolm Robb which intercepts these transmitions via an SDR device (DVB-T stick) and provides a network stream with the decoded data. You can watch this stream if you just telnet on port 30003 of the Pi. So far, so good.

在第二台机器上运行虚拟雷达服务器(简称 VRS).它主要是一个网络服务器,用于显示在 PI 的描述端口上接收到的数据.在我的本地 LAN 中,这也适用.

On a second machine Virtual Radar Server (short VRS) is running. It is mostly a webserver which displays the data received on the described port of the PI. In my local LAN this works as well.

但知道是棘手的部分.Pi 通常位于没有端口转发的路由器/防火墙后面,因此 VRS 无法连接到该 TCP 流.我已经设法建立反向 SSH 连接以再次在 telnet 中打开该流,但由于这是在 Pi 上运行的 telnet 会话,而只是远程控制的 VRS 无法使用它.

But know comes the tricky part. The Pi is normally situated behind a router/firewall with no port forwarding so VRS can't hook up on that TCP stream. I already managed to establish a reverse SSH connection to open up that stream in telnet again but since this is a telnet session running on the Pi and just remote controlled VRS can't us it.

现在我的想法是在 VRS 机器上拦截来自反向 SSH 的数据并使用 Python 脚本读取它并提供它的新本地 TCP 流.所以理论上 VRS 可以读取它.

Now my idea was to intercept the data from the reverse SSH on the VRS machine and use a Python script to read it and provide a fresh local TCP stream of it. So in theorie VRS could read it.

我的第一个问题是如何拦截连接.我在 Python 中找到了两种方法.您更喜欢哪一个,或者您有更好的主意?

My first problem is how to intercept the connection. I found two ways to do it in Python. Which one would you prefer or do you even have a better idea?

方法一

import os
os.system("ssh pi@localhost -p 13889 telnet localhost 30003

方法二

import subprocess
subprocess.call("ssh pi@localhost -p 13889 telnet localhost 30003")

但是我如何让粗壮的人进一步使用呢?

But how do I get the stout to further wor with?

下一个问题是关于设置 TCP 流.Dump1090 似乎一直在发送数据,但到目前为止我不知道如何在 Python 中设置它.到目前为止,我只有一个客户端-服务器-组合,其中需要首先启动接收服务器.否则,客户端将抛出一个监听端口未打开的异常.有什么办法可以解决这个问题吗?

The next problem is about setting up the TCP stream. Dump1090 seems to send the data all time but so far i could not figure out how to set this up in Python. so far I only got a client-server-combo where the receiving server needs to be started first. Other wise the client will throw an exeption that the listening port is not open. Is there any way to resolve this problem?

也许我正在考虑复杂化的方法.所以我有人有一个更简单的想法,发布它!

Maybe I am thinking ways to copmplicated. So i someone has an easier idea, post it!

弗洛里安

flightradar24.comgithub.com/MalcolmRobb/dump1090virtualradarserver.co.uk/

flightradar24.com github.com/MalcolmRobb/dump1090 virtualradarserver.co.uk/

推荐答案

虽然我不是 ssh 和 tunelling 的高手,但我只会回答第二部分:使用 Try/Except 建立连接有一些延迟.

While I am not a master in ssh and tunelling I will answer only for the second part: use Try/Except with some delay to establish a connection.

示例

import time

connected = False
while not connected:
    try:
        # connect whatever you like
        connected = True
    except IOError: # or any other exception you get from connecting
        time.sleep(5) # or any other value
        print('Could not connect, retrying...')

这篇关于基于 TCP 的 Python 流数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆