简单的网络UDP收听烧瓶或金字塔 [英] Simple Network UDP Listen in Flask or Pyramid

查看:185
本文介绍了简单的网络UDP收听烧瓶或金字塔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个web应用程序来显示通过定期传入的UDP数据包提供的数据。该网站可能会在Flask(可能是金字塔),部署在Nginx下。我怎么能创建一个非常简单的后台任务(基本上只是socket.recv())来监听任何传入的数据包,并将数据推送到全局访问列表?

我可以简单地从main()产生一个线程来做到这一点,还是我需要使用像芹菜或PyRes?



感谢您的任何指导。 您必须使用芹菜,但你很幸运,因为已经有一个集成了芹菜的瓶子扩展 。你需要 pip install flask , pip install flask-celery pip install redis 你需要在你的系统上安装一个redis服务器。


$ b $

  import socket,select,Queue 

从瓶子进口烧瓶
从芹菜进口芹菜

$ b $ def make_celery(app):
芹菜= Celery(app.import_name,broker = app.config ['CELERY_BROKER_URL'])
celery.conf.update(app.config)
TaskBase = celery.Task $ b $ class ContextTask(TaskBase):
abstract = True
def __call __(self,* args,** kwargs):
with app.app_context():
返回TaskBase .__调用__(self,* args,** kwargs)
celery.Task = ContextTask
返回芹菜

app = Flask(__ name__)
app.config.update(
CELERY_BROKER_URL ='redis:// localhost:6379' ,
CELERY_RESULT_BACKEND ='redis:// localhost:6379'

celery = make_celery(app)
socket_queu e = Queue.Queue()


@ celery.task()
def listen_to_udp():

这段代码是从
https://stackoverflow.com/questions/9969259/python-raw-socket-listening-for-udp-packets-only-half-of-the-packets-received

s1 = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s1.bind(('0.0.0.0',1337))
s2 = socket.socket(socket.AF_INET,socket。 SOCK_RAW,socket.IPPROTO_UDP)
s2.bind(('0.0.0.0',1337))
True:
r,w,x = select.select([s1,s2], (),
















$ app_route(/ )
def test_home():
listen_to_udp.delay()
print(socket_queue.get())

if __name__ ==__main__:
#run install.py安装依赖项并创建数据库
app.run(host =0.0.0.0,port = 5000,debug = True)
pre>

I need to create a web app that displays data provided via periodic incoming UDP packets. The site will be probably be in Flask (possibly Pyramid), deployed under Nginx. How can a I create a very simple background task (basically just socket.recv()) to listen for any incoming packets, and push the data into globally accessible lists?

Can I simply spawn a thread from main() to do this, or do I need to use something like Celery or PyRes?

Thanks for any guidance.

解决方案

You will have to use celery but you're in luck because There's already a flask extension that integrates celery. you'll have to pip install flask, pip install flask-celery, pip install redis and you'll need a redis server on your system.

import socket, select, Queue

from flask import Flask
from celery import Celery


def make_celery(app):
    celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'])
    celery.conf.update(app.config)
    TaskBase = celery.Task
    class ContextTask(TaskBase):
        abstract = True
        def __call__(self, *args, **kwargs):
            with app.app_context():
                return TaskBase.__call__(self, *args, **kwargs)
    celery.Task = ContextTask
    return celery

app = Flask(__name__)
app.config.update(
    CELERY_BROKER_URL='redis://localhost:6379',
    CELERY_RESULT_BACKEND='redis://localhost:6379'
)
celery = make_celery(app)
socket_queue = Queue.Queue()


@celery.task()
def listen_to_udp():
    """
    This code was taken from 
    https://stackoverflow.com/questions/9969259/python-raw-socket-listening-for-udp-packets-only-half-of-the-packets-received
    """
    s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s1.bind(('0.0.0.0', 1337))
    s2 = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_UDP)
    s2.bind(('0.0.0.0', 1337))
    while True:
        r, w, x = select.select([s1, s2], [], [])
        for i in r:
            socket_queue.put((i, i.recvfrom(131072)))

@app.route("/")
def test_home():
    listen_to_udp.delay()
    print(socket_queue.get())

if __name__ == "__main__":
    #run install.py to install dependencies and create the database
    app.run(host="0.0.0.0", port=5000, debug=True)

这篇关于简单的网络UDP收听烧瓶或金字塔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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