如何在 Python 中通过 HTTP 提供来自 UDP 流的数据? [英] How to serve data from UDP stream over HTTP in Python?

查看:26
本文介绍了如何在 Python 中通过 HTTP 提供来自 UDP 流的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正致力于通过网络公开旧系统中的数据.我有一个(传统)服务器应用程序,它通过 UDP 发送和接收数据.该软件使用 UDP 以(近)实时(每 5-10 毫秒更新一次)向给定的一组变量发送顺序更新.因此,我不需要捕获所有 UDP 数据——检索最新更新就足够了.

I am currently working on exposing data from legacy system over the web. I have a (legacy) server application that sends and receives data over UDP. The software uses UDP to send sequential updates to a given set of variables in (near) real-time (updates every 5-10 ms). thus, I do not need to capture all UDP data -- it is sufficient that the latest update is retrieved.

为了通过网络公开这些数据,我正在考虑构建一个轻量级的网络服务器,它可以读取/写入 UDP 数据并通过 HTTP 公开这些数据.

In order to expose this data over the web, I am considering building a lightweight web server that reads/write UDP data and exposes this data over HTTP.

由于我对 Python 的使用经验丰富,我正在考虑使用它.

As I am experienced with Python, I am considering to use it.

问题如下:我如何(连续)从 UDP 读取数据并使用 Python 通过 TCP/HTTP 按需发送它的快照?所以基本上,我正在尝试构建一个一种UDP2HTTP"适配器,用于与旧应用交互,这样我就不需要接触旧代码.

The question is the following: how can I (continuously) read data from UDP and send snapshots of it over TCP/HTTP on-demand with Python? So basically, I am trying to build a kind of "UDP2HTTP" adapter to interface with the legacy app so that I wouldn't need to touch the legacy code.

符合 WSGI 标准的解决方案会更受欢迎.当然,任何提示都非常受欢迎,非常感谢!

A solution that is WSGI compliant would be much preferred. Of course any tips are very welcome and MUCH appreciated!

推荐答案

该软件使用 UDP 以(近)实时(每 5-10 毫秒更新一次)向给定的一组变量发送顺序更新.因此,我不需要捕获所有 UDP 数据——检索最新更新就足够了

The software uses UDP to send sequential updates to a given set of variables in (near) real-time (updates every 5-10 ms). thus, I do not need to capture all UDP data -- it is sufficient that the latest update is retrieved

你必须做的是这个.

第一步.

构建一个 Python 应用程序,用于收集 UDP 数据并将其缓存到文件中.使用 XML、CSV 或 JSON 表示法创建文件.

Build a Python app that collects the UDP data and caches it into a file. Create the file using XML, CSV or JSON notation.

这作为某种守护进程独立运行.这是您的听众或收藏家.

This runs independently as some kind of daemon. This is your listener or collector.

将文件写入一个目录,Apache 或其他 Web 服务器可以从该目录中轻松下载.明智地选择名称和目录路径,就大功告成了.

Write the file to a directory from which it can be trivially downloaded by Apache or some other web server. Choose names and directory paths wisely and you're done.

完成.

如果你想要更好的结果,你可以做更多.你不需要,因为你已经完成了.

If you want fancier results, you can do more. You don't need to, since you're already done.

第 2 步.

构建一个 Web 应用程序,允许某人请求由 UDP 侦听器或收集器收集的此数据.

Build a web application that allows someone to request this data being accumulated by the UDP listener or collector.

为此使用像 Django 这样的网络框架.尽量少写.Django 可以提供由您的侦听器创建的平面文件.

Use a web framework like Django for this. Write as little as possible. Django can serve flat files created by your listener.

你已经完成了.再次.

有些人认为关系数据库很重要.如果是这样,你可以这样做.即使你已经完成了.

Some folks think relational databases are important. If so, you can do this. Even though you're already done.

第 3 步.

修改您的数据集合以创建 Django ORM 可以查询的数据库.这需要一些学习和一些调整才能得到一个整洁、简单的 ORM 模型.

Modify your data collection to create a database that the Django ORM can query. This requires some learning and some adjusting to get a tidy, simple ORM model.

然后编写最终的 Django 应用程序,为侦听器收集并加载到 Django 数据库中的 UDP 数据提供服务.

Then write your final Django application to serve the UDP data being collected by your listener and loaded into your Django database.

这篇关于如何在 Python 中通过 HTTP 提供来自 UDP 流的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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