代码 :
#! /usr/bin/python
import socket
import struct
import time
file_output = open('epg_python_raw.txt','w')
class EPG():
def open1():
file_output = open('epg_python_raw.txt','w')
MCAST_GRP = '238.1.1.1'
MCAST_PORT = 1234
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', MCAST_PORT))
mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
while True:
data = sock.recv(10240)
print data
print >> file_output, data
我的问题是,如何在一段时间后停止此脚本,我将执行限制为 5 分钟并退出。顺便说一句,我是 python 新手,所以你们能给我建议或一些代码。