[英]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.