简体   繁体   English

如何在一个端口上侦听并同时从另一个端口广播? 蟒蛇

[英]How to listen on a port and broadcast from another at the same time? Python

I have a python script which works very well at listening for a UDP packet on a port. 我有一个Python脚本,可以很好地侦听端口上的UDP数据包。 I already know how to broadcast a UDP packet but the design of my software would require me to be able to listen for a UDP packet and broadcast them at the same time. 我已经知道如何广播UDP数据包,但是我的软件设计要求我能够侦听UDP数据包并同时广播它们。

How can I listen on one port for UDP packets, whilst the program continues, ultimatly resulting broadcasting of packets? 在程序继续进行的同时,如何在一个端口上侦听UDP数据包,最终广播数据包?

Simply put, I would like my listening loop.... 简而言之,我想听一下循环...。

    print("Listening on port: " + str(self.recvPort))
    while 1:
        data = inSoc.recv(65536)  # 16 bytes
        # Recived some data from the server, Can PROCCESS NOW!
    if not data:
        pass
    else:
        print("Data has arrived!")
        print("[Data] : ", data)

... to continuously run in the background while the script continues its other tasks, one being to broadcast out on a different port. ...在脚本继续执行其他任务的同时在后台连续运行,其中一个任务是在其他端口上广播。

I know this should be relational to threading, _thread or async however I am unable to get these to work? 我知道这应该与线程,_thread或异步有关,但是我无法使它们正常工作?

I named the loop an async def but it failed due to not having an await 我将循环命名为async def但由于没有await而失败

You wouldn't inherently need threading to solve this. 您本来就不需要线程来解决此问题。 I think you should have a look at select https://docs.python.org/2/library/select.html 我认为您应该看看选择 https://docs.python.org/2/library/select.html

then you could do something like: 那么您可以执行以下操作:

while 1:
    read, _, _ = select.select([inSoc],[],[], 0)
    #if insoc has something for you to read
    for soc in read:
        data = soc.recv(65536)
    #do something else

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python听2端口相同的文件 - python listen 2 port same file 如何从Python中的另一个线程广播到socketio? - How to broadcast to socketio from another thread in Python? python:如何同时监听鼠标点击和按键 - python: how to listen for mouse clicks and keypress at the same time 如何在python中同时监听命名管道和套接字 - How to listen named pipe and socket at the same time in python 如何编写可以同时监听端口的多线程kivy游戏(在rasp Pi上) - how to write a multithread kivy game(on rasp Pi) that can listen to a port at the same time 如何在python + linux中并行监听端口 - How to listen port parallel in python + linux UDP广播后要侦听哪个端口? - Which port to listen to after UDP Broadcast? 如何让python脚本监听来自另一个脚本的输入 - How to get a python script to listen for inputs from another script 如何在不知道使用套接字的 Python 中来自哪个 IP 的情况下侦听端口上的通信? - How to listen for a communication on a port without knowing what IP its coming from in python using sockets? Pocketsphinx + Gstreamer竞赛条件? Pocketsphinx无法在Python脚本中同时收听音频和录音吗? - Pocketsphinx + Gstreamer Race Condition? Pocketsphinx can't listen to audio + record from it at the same time in Python script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM