小编Ric*_*ñoz的帖子

在后台接收UDP数据包(PYTHON)

我想要一个线程在后台等待 UDP 数据包,当未收到数据包时,我希望脚本能够执行其他操作。但是当我启动线程时,脚本会等待 UDP 数据包并停止。

import threading
import socket


def rec_UDP():
    while True:
        # UDP commands for listening
        UDP_PORT = 5005
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.bind(('10.0.0.15', UDP_PORT))
        data, addr = sock.recvfrom(1024)
        print "received message:", data
        return data


# The thread that ables the listen for UDP packets is loaded
listen_UDP = threading.Thread(target=rec_UDP())
listen_UDP.start()

data = 'Nothing received'

while True:
    print 'The packet received is: ' + data
Run Code Online (Sandbox Code Playgroud)

python multithreading udp background python-2.7

5
推荐指数
1
解决办法
8384
查看次数

标签 统计

background ×1

multithreading ×1

python ×1

python-2.7 ×1

udp ×1