如何在一定时间后停止python udp_listener

时间:2014-06-20 06:37:44

标签: python time udp

我在python中创建了UDP监听器,我希望在一段时间后停止。但是由于阻塞调用recvfrom()我无法做到..

import socket
import time
UDP_IP = "127.0.0.1"
UDP_PORT = 1337
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
cur_time = time.time()
count = 0;
while True:
  if int(time.time() - cur_time) > 1:
      break
  data, addr = sock.recvfrom(1500)
  print "received message:", data
  count = count+ 1
  print "received packets:",count;
  sock.sendto(data, addr)

0 个答案:

没有答案